Trigger time of datareceive event for Serial Communication

Source: Internet
Author: User

Environment: Windows PC, local virtual com2 connection com3, serial port debugging assistant com2 send data

 

Figure 1

 

1> receivedbytesthreshold is the default value 1; 2> 41 bytes are sent at a time; 3> 6 bytes are read-only each time; 4> datareceive event is triggered twice, 6 bytes (12 bytes) are read twice. 6> after a period of time, two threads exit.

As shown above, if you change the number of bytes to 50, the datareceive event is triggered once and 41 bytes of data are read once.

 

Conclusion: 1> If the Received Buffer data is read once, the datareceive event is triggered only once. If the data is not read once, the event is triggered twice. (This is also true for sending 200 bytes at a time)

Doubt: 1> when is the datareceive event triggered? 2> what are the two threads doing?

Prerequisites: The conclusion is that it is connected to a PC. If it communicates with the lower computer through a serial port, the sending end sends 200 bytes. the receiving end may not necessarily receive more than one frame, when the number of bytes per frame is greater than receivedbytesthreshold, A datareceive event is triggered.

Figure 2

 

1> receivedbytesthreshold is set to 5; 2> send two bytes at a time; 3> send all bytes at a time; 4> the datareceive event is triggered three times and two bytes at a time, when triggered, all bytes of data are read at one time.

 

To sum up, I guess the timing of datareceive event triggeringCodeAs follows:

Private void listenthread. Listen ()

{

While (true)

{

If (the serial port has received data)

{

Dealrcvthread = new thread (New threadstart (dealrcvfunc ))

}

// Thread. Sleep (10 );

}

}

Private void dealrcvfunc ()

{

If (the number of data bytes in the received frame + the number of bytes in the Received Buffer> receivedbytesthreshold)

{

Trigger the first datareceive event;

}

Thread. Sleep (latency );

If (number of bytes in the Received Buffer> receivedbytesthreshold)

{

Trigger the second datareceive event;

}

// Thread. Sleep (latency );

The thread exits;

}

The two thread. Sleep (latency) processes may be in a while loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.