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.