LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is ComRd thread safe?

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
0 Kudos
Message 1 of 2
(3,145 Views)
Hi Lorraine,

As per the the CVI help, the RS-232 library is thread safe. It says your can call these library functions from different threads in the same process, including calls that operate on the same port. I'm wondering if the data you are reading is the correct data to be read. Are you manually reading the data from the COM port at a specified time, or are you using a callback to determine when to read the data? If possible, it may be best to resort to using the callback so that we receive data when we're told there's data available rather than just polling the COM port and reading whatever's on it. As far as seeing data out of order, is the pointer to the buffer you're using a pointer to some memory address available to all threads? In this case then the
memory might be overwritten by each thread and it can be a conglomeration of all threads returning data. This is because the RS-232 library calls are thread safe, but once you have the data in memory, it doesn't make that memory location thread safe if your pointer to it isn't. In addition, I'm not quite sure how you're using TSQs, but if you are passing pointers to memory in the TSQ, a copy of the pointer is being made, not a copy of the data that's being pointed to. To get around this, you should preallocate the array using malloc and fill the existing array with data to enqueue. This will then copy the array elements and not the pointer to the memory address where the data is located. Hope this helps!

Jeremy L.
National Instruments
Jeremy L.
National Instruments
0 Kudos
Message 2 of 2
(3,145 Views)