Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top