开发者

How to make the read function not to hang?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜