Hi,
I have created a program that sends and receives data from one of our custom boards across a serial interface via a polling mechanism. The packet of data going to the board is 16 bytes and the packet I receive back is 20 bytes. The hardware board was designed to return data in 12.5 ms including the time to transmit data in both directions (38400 speed).
My program consists of 2 threads both running at priority 10:
Thread 1 sends to the board. It wakes up every 20ms, takes the mutex, checks to see if the last packet sent was replied to (and notes if it was not), increments some packet counters and then sends the next packet to the board and finally releases the mutex.
Thread 2 is the receiving thread. When it gets characters from the serial driver it takes the mutex, adds the characters to the receive buffer and checks to see if a complete packet was received. If a complete packet was received with no errors it marks the reply packet from the board as being successful. The mutex is then released.
Note there is a lot more going on in terms of validating packet data etc but that’s outside the scope of what I am seeing.
When I run my program, all is well. I’m using about 1-2% of the CPU and I’m not missing any packets. So far so good.
Now I added some stats gathering to my code and every 1000 packets I print out some data to a console under Photon (using the enchanced eide driver from EvanH but one that still does everything via CPU). When the stats print out, everything is fine until the data in the console scrolls. At that point, I print out 3-4 error messages about not receiving packets from my board. What I see confuses me and I hope someone can help explain what might be going on at the time the console scrolls.
In the messages I see below, I print out the number of packets I have sent from the sending thread, the number I have received in the receiving thread, the nanosecond time in the current second and the number of bytes I currently have for the next packet (remember I expect to get 20 bytes) from the serial driver.
processPollingTimeout() - Sent 4000 packets. Missing reply packet 4000 from board at time 440992799. Reply buffer has 10 bytes
processPollingTimeout() - Sent 4001 packets. Missing reply packet 4000 from board at time 450991269. Reply buffer has 12 bytes
processPollingTimeout() - Sent 4002 packets. Missing reply packet 4000 from board at time 465988974. Reply buffer has 14 bytes
So what I see is that I missed 3 packets. But what really puzzles me is that I see that 30ms has elapsed. So my timer fired 3 times and I sent 3 packets from the sending thread. In the mean time all I got back was 4 bytes (10 to 14) for the 1st of the 3 missing packets. This seems really strange to me as I can’t figure out why the serial driver is not sending data for this time period to my receiving thread.
Can anyone offer an idea of what might be happening during this 30 ms time frame and what if anything I can do about it? Note, I tried upping the priority of my receive thread to 11 but that makes no difference as I still miss data when the screen scrolls.
In the long run, the console won’t be on my final system but I am worried that when I add other processes their time slices might cause a similar affect on my system
TIA,
Tim