【发布时间】:2020-11-24 02:23:57
【问题描述】:
我有一个从设备读取串口数据的项目,我的操作系统是 windows 10。我读过这个:
2。 SerialPort.Close() freezes application if the USB COM Port in use has been removed
但如果没有来自设备的输入,我的代码可能只能工作 6 小时。目前的解决方案是再次打开表单后关闭表单。
SerialPort myPort = new SerialPort();
private void Scanning_Load(object sender, EventArgs e)
{
myPort.BaudRate = 9600;
myPort.PortName = "COM4";
if (SerialPort.GetPortNames().ToList().Contains(comPortname))
{
if (!myPort.IsOpen)
{
myPort.Open();
}
myPort.DataReceived += getReceivedata;
}
}
private void getReceivedata(object sender,
SerialDataReceivedEventArgs e)
{
SerialPort spL = (SerialPort)sender;
string data_rx = spL.ReadLine();
this.Invoke((MethodInvoker)delegate
{
Console.Write(data_rx);
});
}
【问题讨论】:
标签: c# serial-port interrupt interruption