串口被While循环阻塞 [英] Serial Port Blocked by While Loop

查看:101
本文介绍了串口被While循环阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这就是问题,我创建了一个串行端口事件处理程序来处理两个设备之间的通信,因此我已将问题定位为一个while循环,该循环被阻塞!!!我已经删除了它,并且代码继续像我写的那样继续工作.

无论如何,关于while循环,它所做的只是检查数据帧并返回True,这意味着我们已经确认了该消息.

if (ReceivedFlag == true)     //We have received something and we must process it
            {
                ReceivedFlag = false;     //Make the flag false for the next reception

                if ((ReadBufferByte[0] & Mask[0]) == expectedF) // We need to know that our Ack has been received for a 
                {                                               //particular message.......
                    return true;                                //return true for Ack received
                }
                else
                    return false;                               //return false ack not received


            }
            else return false;                                  //Also return false for no packet received



因此,ReadbufferByte来自串行端口,而mask用于检测包含ack头的帧部分. ExpectedF是我们期望在该标头中使用的值...
还需要注意的是,我也有一个TimeOut事件,并且接收Ack非常重要...还有另一种等待接收Ack数据包的方式吗? >解决方案

您可以处理 SerialPort. DataReceived事件 [ ^ ]并缓冲所有传入的数据并检查"Ack"标记.


我确实使用了事件处理程序,但事实是一旦发送出去,请说数据消息,我希望在继续之前收到确认...这就是问题所在,我在等待Ack消息....


哦,顺便说一句,while循环是一个调用上述函数while(!(function));


Hello everyone, so here''s the thing, I create a serial port event handler to handle communications between two devices, so I have localised the problem to a while loop that is blocking!!!! I''ve removed it, and the code continues to work just as I wrote it to.

Anyway more about the while loop, all it does is checks the data frame and returns True which means we have Ack''d the message.

if (ReceivedFlag == true)     //We have received something and we must process it
            {
                ReceivedFlag = false;     //Make the flag false for the next reception

                if ((ReadBufferByte[0] & Mask[0]) == expectedF) // We need to know that our Ack has been received for a 
                {                                               //particular message.......
                    return true;                                //return true for Ack received
                }
                else
                    return false;                               //return false ack not received


            }
            else return false;                                  //Also return false for no packet received



So ReadbufferByte is from the Serial Port, and mask is for detecting the part of the frame that contains the ack header. ExpectedF is the value we expect to be in that header......
What also needs to be noted is that I have a TimeOut event as well, and its important that the Ack be received...Is there another way of waiting for the Ack Packet be received ?????

解决方案

You can handle SerialPort.DataReceived Event[^] and buffer all incoming data and check for "Ack" marker.


I do use the Event Handler, but the thing is that once I send out say a data message, I would like to receive the ack BEFORE moving on...thats where the problem lies in me waiting for the Ack message....


oh btw the while loop is a call to the above function while(!(function));


这篇关于串口被While循环阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆