I wrote a program in C on a linux machine that reads and writes to a serial port. It's connected to a Windows XP machine running some emulator that is sending my program data.
In my program I have 2 threads, one for reading the input from the serial port and processing it. And a thread for that connects to a server socket and listens for data that is procesed and then written back on the serial port. Both of these threads are continually looping during execution.
The problem I am having is with the write command.
void writeToPort(unsigned char* buf, int length)
{
int w = 0;
if(fd > 0)
w = write(fd, buf, length);
printf("wrote %d bytes\n", w);
}
and I open the port like this
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
For the first few seconds it is functioning correctly. I've verfied in the hyperterminal on Windows that the bytes I expect are being sent properly. However, at some point the write() call will hang. The other input reading thread continues and reading from the serial port works fine.
Does anyone know why this might be happening? I've tried using mutexes whenever a read or write is done on fd but that didn't seem to make a difference. It also does not seem to matter if the other thread is running/ reading from the port. Any advice or suggestions are welcome, thanks