Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将SerialPort接收到的数据链接到主UI线程';s的读取功能?_C#_Thread Safety_Serial Port - Fatal编程技术网

C# 如何将SerialPort接收到的数据链接到主UI线程';s的读取功能?

C# 如何将SerialPort接收到的数据链接到主UI线程';s的读取功能?,c#,thread-safety,serial-port,C#,Thread Safety,Serial Port,我想知道如何将通过serialport接收的数据链接到主UI中调用它的函数 我希望UI从serialport发送数据,然后挂起线程,或者等待通过serialport线程接收到数据,然后再继续主UI线程 我被告知thread.suspend()不是一个可以使用的安全函数,我尝试与thread.resume一起使用,但失败了 我应该看看锁/互斥锁吗 还是事件处理 我完全糊涂了!我不熟悉线程,所以任何帮助将不胜感激!多谢各位 代码如下: 公共静态int-SerialCOM\u读取(uint-tRegi

我想知道如何将通过serialport接收的数据链接到主UI中调用它的函数

我希望UI从serialport发送数据,然后挂起线程,或者等待通过serialport线程接收到数据,然后再继续主UI线程

我被告知thread.suspend()不是一个可以使用的安全函数,我尝试与thread.resume一起使用,但失败了

我应该看看锁/互斥锁吗

还是事件处理

我完全糊涂了!我不熟悉线程,所以任何帮助将不胜感激!多谢各位

代码如下:

公共静态int-SerialCOM\u读取(uint-tRegisterAddress,out字节[]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;
int-retVal=1;
tRegisterValue=新字节[n字节];
字节[]nBytesArray=新字节[4];
n字节=n字节;
试一试{
ReadFunction=true;
YetToReceiveReadData=真;
byte[]registerAddress=BitConverter.GetBytes(tRegisterAddress);
int noofbytes foregadd=1;
如果(registerAddress[3]>0)noofbytes foregad=4;
如果(registerAddress[2]>0)noofbytes foregad=3,则为else;
如果(registerAddress[1]>0)noofbytes foregad=2,则为else;
nBytesArray=位转换器.GetBytes(nBytes);
{
附加_Start();
将\u操作\u附加到\u noofbytes foregad(Operation.I2C\u READ,noofbytes foregad);
追加_RegAdd(noofbytes,tRegisterAddress);
附加_NoOfBytesOfData(nBytesArray);
发送_DataToWrite();
//需要在此处挂起线程,并在
//已分配ReadData[i]全局变量。

对于(int i=0;i如果要阻止UI线程,那么使用DataReceived事件没有意义。只需调用SerialPort.Read/Line()直接。这可能会导致用户界面响应延迟,这通常是不可取的,但确实更容易操作。

如果要阻止UI线程,则使用DataReceived事件没有意义。只需调用SerialPort.Read/Line()直接。这可能导致用户界面响应的延迟通常是不可取的,但确实要容易得多。

只需创建一个委托,并使用它将数据发送到一个可以处理从串行端口接收到的数据的方法。我的用例是我通过端口发送数据,但我不知道什么时候会收到数据回复,可能是任何时候,但当数据到来时,我应该能够获取并处理它,所以我做了以下工作。我想这是你想要的

Public Delegate Sub recieveDelegate() 

Private Sub datareceived1(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPrt.DataReceived

    buffer = buffer & SerialPrt.ReadExisting()

    If InStr(1, rxbuff, EOF) > 0 Then
        Rxstring = rxbuff
        rxbuff = ""
        BeginInvoke(New recieveDelegate(AddressOf recieveHandlingMethod), New Object() {})
    End If
End Sub

只需在recieve处理方法中处理接收到的数据。

只需创建一个委托,并使用它将数据发送到一个方法,在该方法中,您可以处理从串行端口接收到的数据。我的使用案例是通过端口发送数据,我不知道什么时候会得到响应,可能是任何时候,但在该时间,数据会返回能够获取并处理它,所以我做了以下工作。我想这就是你想要的

Public Delegate Sub recieveDelegate() 

Private Sub datareceived1(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPrt.DataReceived

    buffer = buffer & SerialPrt.ReadExisting()

    If InStr(1, rxbuff, EOF) > 0 Then
        Rxstring = rxbuff
        rxbuff = ""
        BeginInvoke(New recieveDelegate(AddressOf recieveHandlingMethod), New Object() {})
    End If
End Sub

只需在recieve处理方法中处理接收到的数据。

您的代码甚至无法编译,您的catch块在try块中,这是无效语法。您是否正在编写WPF或WinForms UI?在任何情况下,出于任何原因挂起UI线程似乎都不是一个好主意。听起来您试图强制执行c的“半双工”模式应用程序上的通信,这通常不是串行端口客户端应用程序所必需的。下面是一个可能会有所帮助的示例。很抱歉,是的,这是无效语法,我错误地剪切和粘贴了代码,漏掉了一个}。我使用的是Windows窗体GUI,感谢链接!)这看起来得到了回答,请关闭您的问题。帮助您解决问题的帖子旁边有一个大的复选标记。您的代码甚至无法编译,您的catch块在try块中,这是无效语法。您是否正在编写WPF或WinForms UI?在这两种情况下,出于任何原因挂起UI线程似乎都不是一个好主意。听起来像是这样正在尝试在应用程序上实施“半双工”通信模式,串行端口客户端应用程序通常不需要这种模式。下面是一个可能会有所帮助的示例。很抱歉,是的,这是无效语法,我错误地剪切和粘贴了代码,漏掉了一个}。我正在使用Windows窗体GUI,谢谢链接!:)这看起来已回答,请关闭您的问题。帮助您解决问题的帖子旁边的大复选标记。谢谢箭鱼,我不确定如何创建代理,因此此信息也确实有帮助!:)谢谢箭鱼,我不确定如何创建代理,因此此信息确实有帮助也是!:)