我目前正在写,执行3种基本功能的应用程序:多线程的串行端口C#
- 发送命令到第三方设备
- 阅读第三方设备
- 分析响应字节响应和写分析的RichTextBox
我的应用程序包含一些与每一个执行的测试,如环路测试的脚本:
public SerialPort comport = new SerialPort();
private void RunTest()
{
byte[] arrayExample = { 0x00, 0x01, 0x02, 0x03 };
// Perform 200 operations and analyze responses
for(int i=0, i<200, i++)
{
// Send byte array to 3rd party device
comport.Write(arrayExample, 0, arrayExample.length);
// Receive response
int bytes = comport.BytesToRead;
byte[] buffer = new byte[bytes];
comport.Read(buffer, 0, bytes);
// Check to see if the device sends back a certain byte array
if(buffer = { 0x11, 0x22 })
{
// Write "test passed" to RichTextBox
LogMessage(LogMsgType.Incoming, "Test Passed");
}
else
{
// Write "test failed" to RichTextBox
LogMessage(LogMsgType.Incoming, "Test Failed");
}
}
}
在当前设置中,我的UI在测试脚本期间没有响应(通常持续2-3分钟)。
正如你所看到的,我没有使用DataReceived
事件。相反,我选择专门调用何时写入/读取串行端口。我这样做的部分原因是因为我需要在写入更多数据之前停止并分析缓冲区响应。有了这种情况,有没有办法仍然多线程这个应用程序?
是的,在工作线程中运行整个RunTest函数。 –
谢谢,我会试试 – Nevets