【发布时间】: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