문제

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
도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top