Hello everyone,
I have an application that reads and writes data to four serial ports. I have created two threads for each serial port, a write thread, a read thread, and a tsq. So I have 4 write threads, 4 read threads, and 4 tsq.
The write thread, does the following. Writes a one byte request to the serial port to ask for position. Reads the three byte response from the serial port. Writes a one byte request for status to the serial port, reads the three byte response form the serial port. Each byte of the three byte responses have a 2 bit header so that I can tell if they are corrupted, out of order, etc. The thread then writes the data received into a tsq. Each write thread does the above for one serial port only.
The read thread reads the data from the tsq and displays it on the gui (actually the position and a summary of the status, but that really isn't important).
My problem is that sometimes in the write thread the three bytes of data read using ComRd has headers that indicate that the data is out of order, or is not the response to previous request for data. I have gone through all the subroutines called by the write thread, and made all the local variables in them ThreadLocalVar. I have not made the paramters to the subroutines ThreadLocalVar, because I did not think that this was necessary, especially since the ni example didn't have it that way.
When I run only one read thread and one write thread, the application works fine with no bytes out or order or missing, no matter what serial port I use. When I run with two or more read and write threads using two or more serial ports, after about 20 minutes the bytes start coming out of order. I think that it may be that ComRd is not reenterant, thread safe or that my local or other variables are not thread safe. I have a break point where the bytes are detected to be out of order (which is right after they are read in using ComRd). Could the ComRd function be giving me data from more than one serial port if two threads called this function at the same time? How thread safe is it?
Any help would be greatly appreciated.
thanks,
Lorraine