如何将 SerialPort 接收到的数据链接到主 UI 线程的读取函数?
我想知道如何将通过串口接收的数据链接到在主用户界面中调用它的函数。
我希望 UI 从串行端口发送数据,然后挂起线程或等到通过串行端口线程接收到数据,然后再继续主 UI 线程。
我被告知 thread.suspend() 不是一个安全的函数,我尝试将它与 thread.resume 一起使用,但失败了。
我应该考虑锁/互斥锁吗?
或者事件等待句柄?
我彻底糊涂了!我对线程不熟悉,所以任何帮助将不胜感激!谢谢。
代码如下:
public static int SerialCOM_Read(uint tRegisterAddress, out byte[] tRegisterValue, uint nBytes) {
int retVal = 1;
tRegisterValue = new byte[nBytes];
byte[] nBytesArray = new byte[4];
NBytes = nBytes;
try {
ReadFunction = true;
YetToReceiveReadData = true;
byte[] registerAddress = BitConverter.GetBytes(tRegisterAddress);
int noOfBytesForRegAdd = 1;
if (registerAddress[3] > 0) noOfBytesForRegAdd = 4;
else if (registerAddress[2] > 0) noOfBytesForRegAdd = 3;
else if (registerAddress[1] > 0) noOfBytesForRegAdd = 2;
nBytesArray = BitConverter.GetBytes(nBytes);
{
Append_Start();
Append_Operation_with_NoOfBytesForRegAdd(OPERATION.I2C_READ, noOfBytesForRegAdd);
Append_RegAdd(noOfBytesForRegAdd, tRegisterAddress);
Append_NoOfBytesOfData(nBytesArray);
Send_DataToWrite();
//Need to Suspend Thread here and Continue once the
//ReadData[i] global variable has been assigned.
for(int i=0; i<nBytes; i++)
tRegisterValue[i] = ReadData[i];
}
catch(Exception ex) {}
return retVal;
}
I'd like to know how to link the data received via serialport to the function calling it in the main UI.
I would like the UI to send out data from the serialport, then suspend the thread or wait until data is received via the serialport thread, before continuing the main UI thread.
I am told that thread.suspend() is not a safe function to use, I tried it together with thread.resume, but failed.
Should I be looking at lock/mutex?
Or EventWaitHandles?
I am thoroughly confused! I am not familiar with threading so any help would be much appreciated! Thank you.
Code below:
public static int SerialCOM_Read(uint tRegisterAddress, out byte[] tRegisterValue, uint nBytes)
{
int retVal = 1;
tRegisterValue = new byte[nBytes];
byte[] nBytesArray = new byte[4];
NBytes = nBytes;
try {
ReadFunction = true;
YetToReceiveReadData = true;
byte[] registerAddress = BitConverter.GetBytes(tRegisterAddress);
int noOfBytesForRegAdd = 1;
if (registerAddress[3] > 0) noOfBytesForRegAdd = 4;
else if (registerAddress[2] > 0) noOfBytesForRegAdd = 3;
else if (registerAddress[1] > 0) noOfBytesForRegAdd = 2;
nBytesArray = BitConverter.GetBytes(nBytes);
{
Append_Start();
Append_Operation_with_NoOfBytesForRegAdd(OPERATION.I2C_READ, noOfBytesForRegAdd);
Append_RegAdd(noOfBytesForRegAdd, tRegisterAddress);
Append_NoOfBytesOfData(nBytesArray);
Send_DataToWrite();
//Need to Suspend Thread here and Continue once the
//ReadData[i] global variable has been assigned.
for(int i=0; i<nBytes; i++)
tRegisterValue[i] = ReadData[i];
}
catch(Exception ex) {}
return retVal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要阻塞 UI 线程,那么使用 DataReceived 事件就没有意义。只需直接调用 SerialPort.Read/Line() 即可。这可能导致用户界面响应速度的延迟通常是不受欢迎的,但它肯定更容易上手。
If you are going to block the UI thread then there's no point in using the DataReceived event. Simply call SerialPort.Read/Line() directly. The delays this can cause in the user interface responsiveness are usually undesirable but it is definitely a lot easier to get going.
只需创建一个委托并使用它将数据发送到一个方法,您可以在其中处理从串行端口接收到的数据。我的用例是我通过端口发送数据,我不知道什么时候会收到响应,可能是任何时间,但那时数据到来我应该能够获取它并处理它,所以我执行以下操作。认为这就是您可能想要的
只需处理在接收处理方法中收到的数据即可。
Just create a delegate and use it to send the data to a method where you can process the data recieved from serial port. My use case is i send a data over the port and i dont know when ill get the response back, it might be any time but at that time the data comes i should be able to get it and process it, so i do the following. Think this is what you might want
Just handle the data you recieve in the recieve handling method.