tags:

views:

37

answers:

2

I'm using socat to create a virtual serial port with:

socat PTY,link=/dev/ttySV0,echo=1 PTY,link=/dev/ttySV1,echo=1

The in my program written in C++, I open the ttySV1 port and start to read.

The read function is in a while, but the problem is that the read function hangs until I send data to the port. Do you know how to make the taht function not to hang and just exit if there is no data in the buffer?

EDIT: I send data to the port using:

echo [data] > /dev/ttySV0
+1  A: 

One way is to use the select() method to see if there is data to be read. This or something else found on google (I searched for "bsd sockets select") should lead you down the right path.

dutt
+1  A: 

You could also use a system read function depending on the operating system you are running. The details should be under man (3) read.

You would have to set O_NONBLOCK using fnctl. This should cause your read to fail if the pipe / FIFO is currently empty. I checked the man pages for Linux but their should be similar behaviors in most POSIX based systems.

CtRanger
Awesome! I was using exactly that read funcion (my OS is Arch Linux). The difference is that the O_NONBLOCK option was missing. Now I put it and it is working like a charm. Thanks!
Tomas
Glad that worked. I love dealing with systems APIs and their hidden options and controls : )
CtRanger