开发者

Does delaying blocks data receiving

开发者 https://www.devze.com 2023-04-04 08:31 出处:网络
I am working on a project on Visual Studio C#. I am collecting data from a device connected to PC via serial port.

I am working on a project on Visual Studio C#.

I am collecting data from a device connected to PC via serial port. First I send a request command, and wait for response. There is a 1 sec delay of device to response after sending request command. The thing is device may not be reached and may not response sometimes. In order to wait response (if any) and not to sent next data request command early, I make a delay by: Syste开发者_JAVA百科m.Threading.Thread method.

My question is, if I make that delay time longer, do I loose serial port data receiving.

The Delay function I use is:

    private void Delay(byte WaitMiliSec)
    {
        // WaitTime here is increased by a WaitTimer ticking at every 100msec

        WaitTime = 0;
        while (WaitTime < WaitMiliSec)
        {
            System.Threading.Thread.Sleep(25);
            Application.DoEvents();
        }
    }


no - you won't loose any data - the serial-port has it's own buffer which does not depend on your application at all. The OS and the hardware will handle this for your.

I would suggest to refactor the data-send/receive into it's own task/thread. That way you don't need the Application.DoEvents();

If you post some more of your send/receive code I might help you with this.

PS: it seems to me that your code will not work anyhow (WaitTime is allways zero) but I guess it's just a snippet right?

0

精彩评论

暂无评论...
验证码 换一张
取 消