在c#Arduino中存储多个临时数据

本文关键字:数据 c#Arduino 存储 | 更新日期: 2023-09-27 18:29:25

我是c#的新手,我有一个使用arduino通过LAN中的以太网从某台机器接收"+:0"的表单,如果同一台机器再次发送"+",则"0"将增加。每台电脑将随机向表单发送"+"。问题是,如果机器A已经达到"+:5",而机器B刚刚开始发送数据("+:0"),我如何为机器A存储临时数据(不存储到数据库,只存储在c#中)以访问机器B,直到机器A又被叫回来了?有什么办法吗?

这是我从IP:获取数据的代码

如果机器发送第一个数据,则downloadedString将接受"+:0"

 public AndonForm()
 {
        InitializeComponent();
        _timer.Interval = (500) * (1); 
        _timer.Enabled = true;        
        _timer.Start();
        _timer.Tick += (timerTick); 
  }

    private void timerTick(object sender, EventArgs e)
    {
        GetDataFromArduino("http://192.168.1.200/");
        GetDataFromArduino("http://192.168.1.12/");
        GetDataFromArduino("http://192.168.1.166/");
        GetDataFromArduino("http://192.168.1.6/");
    }
    private void GetDataFromArduino(string ipAddress)
    {
        WebClient client = new WebClient();
        String downloadedString = client.DownloadString(ipAddress);
        if (downloadedString.Contains("SPP"))
        {
            downloadedString = downloadedString.Split(new[] { "<html>'r'n" }, StringSplitOptions.None)[1];
            tempDownloadString = downloadedString.Substring(0, downloadedString.IndexOf(":"));
            tempCount = downloadedString.Split(new[] { ":" }, StringSplitOptions.None)[1];
        }
        else if (downloadedString.Contains("NPK"))
        {
            downloadedString = downloadedString.Split(new[] {"<html>'r'n"}, StringSplitOptions.None)[1];
            tempDownloadString = downloadedString.Substring(0, downloadedString.IndexOf(":"));
            tempCount = downloadedString.Split(new[] { ":" }, StringSplitOptions.None)[1];
            tempCount = tempCount.Substring(0, tempCount.IndexOf("<"));
        }
        else
        {
            downloadedString = downloadedString.Split(new[] {"<html>'r'n"}, StringSplitOptions.None)[1];
            tempDownloadString = downloadedString.Substring(0, downloadedString.IndexOf(":"));

            tempCount = downloadedString.Split(new[] {":"}, StringSplitOptions.None)[1];
            tempCount = tempCount.Substring(0, tempCount.IndexOf("<"));
        }
        if (SPP != null && NPK == null)
        {
            finalDownloadString = tempDownloadString;
            si_DataReceived(tempDownloadString);
            x = tempCount;
        }
        else if (SPP != null && NPK !=null)
        {
            if (x != tempCount || tempDownloadString != finalDownloadString)
            {
                si_DataReceived(tempDownloadString);
                finalDownloadString = tempDownloadString;
                x = tempCount;
            }
        }
        else
        {
            finalDownloadString = tempDownloadString;
            si_DataReceived(tempDownloadString);
            x = tempCount;  
        }
    }

在c#Arduino中存储多个临时数据

您可以使用Dictionary<string, List<string>>。在Dictionary中,关键字字符串应该是ipAddress变量中的ip,而List<string>是Arduino为特定ip发送的所有数据。