【问题标题】:Serialization Data to JSON from COM Port从 COM 端口将数据序列化为 JSON
【发布时间】:2022-08-04 14:37:11
【问题描述】:

我需要在我的 Windows 窗体项目中反序列化来自 COM 端口的数据。但我不知道如何开始。我之前尝试过谷歌,但我发现的信息不适用于我的案例,也没有一个与 COM 端口相关。即将到来的数据有多个我不需要的字符。好吧,我尝试将我找到的一些解决方案调整到我的代码中,但它们都不起作用,我尝试这样做,它创建了文件,但它没有在其中放入任何数据。

            port = new SerialPort(comboBox1.Text,
                                  9600, Parity.None, 8, StopBits.One);
            port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            port.Open();

            void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            {
                SerialPort sp = (SerialPort)sender;
                string indata = sp.ReadExisting().ToString();
                this.Invoke(new EventHandler(ShowData));
                this.Invoke(new EventHandler(Serialization));

                void ShowData(object sender, EventArgs e)
                {
                    dtBox.Text += indata;
                }

                void Serialization(object sender, EventArgs e) 
                { 
                dynamic json = Newtonsoft.Json.JsonConvert.SerializeObject(indata);
                StreamWriter w = new StreamWriter(@\"C:/temp/JSON_TEST.json\", true);
                w.WriteLine(json);
                w.Close();
                }
            }

来自 COM 端口的数据是这样的:

                     ------------------------------------------
                     Date :                   11:33  25/07/2022
                     Machine SN :                  1234509385_9
                     User ID :                           1-Emplo
                     ------------------------------------------
                     ------------ Value People Total -----------
                     Type:                             Data
                     ------------------------------------------
                     This        That               Total
                        7           25                  125
                       53           32                  320
                       87           25                  500
                       95           20                 1000
                      110           35                 3500
                     ------------------------------------------
                                                         Total
                                      137                 5445
                     ------------------------------------------
                                                   Total Amount
                                                          5445
                     ------------------------------------------- 
                 

我也想忽略连字符。有人可以帮助我吗?

谢谢!

    标签: serialization json-deserialization com-port


    【解决方案1】:

    一些伪代码在我的脑海中浮现。

    List<YourThisThatTotalClass> ytttc = new List<YourThisThatTotalClass>();
    bool startCollecting = false;
    
    //Iterate through each line till you find a line starting with `This`
    foreach(var line in json.Split(Environment.Newline))
    {
       if (line.StartWith("This")){
           startCollecting = true;
       }
       else if (startCollecting && line.StartWith("--")){
           break;
       }
       if (startCollecting){ //Store each record in a class
           string[] vals = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); //new char[0] or null will assume whitespace is the delimiter, with StringSplitOptions.RemoveEmptyEntries to be safe
           ytttc.Add(new YourThisThatTotalClass {vals[0], vals[1], vals[2]});  // the class has a Constructor with three input parameters. You may want to cast them to numbers and do validation they're valid values.
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多