iamthemik3
Newbie level 2
I'm working on a cross-platform serial library (in C) to communicate to a USB CDC microcontroller (msp430). I have been noticing that when I run my program in linux, it works fantastically well. However, on Mac OSX or Windows 7/XP, every once i a while a packet of data takes a significant time to be received by my program. Since the behavior is pretty specific, I'll explain the gory details of what I am doing and when the lag occurs.
For flow control reasons and to keep buffer sizes small on the microcontroller, I designed a simple handshaking scheme to transfer data.
Ideally, my PC program and the microcontroller (MCU) would communicate in a packet format as follows:
PC <-- MCU - MCU sends the PC a 4-byte header to the PC
PC <-- MCU - MCU sends a 128-byte payload
PC --> MCU - PC sends a 4-byte acknowledgement that it is ready for the next packet
...repeat...
The above works fine when my program runs on linux (which is enough confirmation for me that it is not a microcontroller issue)
When running the program on OSX or Windows, it is able to recv about a dozen packets correctly and then it does this:
PC <-- MCU - MCU sends the PC a 4-byte header to the PC
PC x-- MCU - MCU transmits the 128-byte payload, but the PC doesn't recv it immediately
.......... - MCU is now waiting for an ACK while the PC program is waiting for the payload
...The MCU times out after several seconds of waiting (as it should). Then the PC finally gets the payload.
So, why does this happen?
Each packet transaction normally happens very quickly. Does the PC get overwhelmed with all the activity? Buffers shouldn't be filling up since I don't allow them to because of the handshaking. Increasing the timeout period on each end doesn't really help either (and a 10 second timeout is way too much anyways)
I have attached the source code for the abstraction layer that handles sending and getting data from the serial device. One thing to note is that I originally wrote it using only POSIX calls to make it easy to port to the other OS's. I rewrote portions for windows to use the native API and it reduced the frequency of these "stalls" occuring but it still messes up sometimes.
View attachment serial_io.zip
Hopefully I explained what I am observing well enough. Let me know if I need to clarify anything.
-Alex
For flow control reasons and to keep buffer sizes small on the microcontroller, I designed a simple handshaking scheme to transfer data.
Ideally, my PC program and the microcontroller (MCU) would communicate in a packet format as follows:
PC <-- MCU - MCU sends the PC a 4-byte header to the PC
PC <-- MCU - MCU sends a 128-byte payload
PC --> MCU - PC sends a 4-byte acknowledgement that it is ready for the next packet
...repeat...
The above works fine when my program runs on linux (which is enough confirmation for me that it is not a microcontroller issue)
When running the program on OSX or Windows, it is able to recv about a dozen packets correctly and then it does this:
PC <-- MCU - MCU sends the PC a 4-byte header to the PC
PC x-- MCU - MCU transmits the 128-byte payload, but the PC doesn't recv it immediately
.......... - MCU is now waiting for an ACK while the PC program is waiting for the payload
...The MCU times out after several seconds of waiting (as it should). Then the PC finally gets the payload.
So, why does this happen?
Each packet transaction normally happens very quickly. Does the PC get overwhelmed with all the activity? Buffers shouldn't be filling up since I don't allow them to because of the handshaking. Increasing the timeout period on each end doesn't really help either (and a 10 second timeout is way too much anyways)
I have attached the source code for the abstraction layer that handles sending and getting data from the serial device. One thing to note is that I originally wrote it using only POSIX calls to make it easy to port to the other OS's. I rewrote portions for windows to use the native API and it reduced the frequency of these "stalls" occuring but it still messes up sometimes.
View attachment serial_io.zip
Hopefully I explained what I am observing well enough. Let me know if I need to clarify anything.
-Alex