当我拔下串行电缆时,Linux read() 调用不返回错误 [英] Linux read() call not returning error when i Unplug serial cable

查看:69
本文介绍了当我拔下串行电缆时,Linux read() 调用不返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 和 Linux 的新手.我正在尝试使用 read() 系统调用从 串行端口 读取一些数据,并且工作正常.问题是即使我在代码运行时拔掉串行电缆,read()不返回负值.请帮忙

I am new to C and Linux. I am trying to read some data from the serial port by using the read() system call, and it is working fine. Problem is that read() is not returning a negative value even if I unplug the serial cable while the code is running.Please help

如果场景不清楚,请随时提问.感谢您的时间

Please feel free to ask if the scenario is not clear.Thanks for your time

当我拔下电缆时,它返回零.

It returns a Zero when I unplug the cable.

char cBuff[100];
buffptr = cBuff;
while (( nbytes = read(fd, buffptr, cBuff + sizeof(cBuff) - buffptr - 1)) > 0)
{
  //Do something
}

if(nbytes < 0 )
{
  perror("Serial Read Thread ERROR:");
}
else if(nbytes >= 0 )
{
  //Do something
}

推荐答案

拔掉串口线只有数据不能再到达的效果,但没有真正的连接",伤口不知何故中断.

Unplugging the serial cable has only the effect that no data cannot arrive any longer, but there is no real "connection" that wound be interrupted somehow.

您的串行端口现在只注意到没有进一步的0"值到达,因为线路现在一直保持在1"上.

Your serial port now only notices that no further "0" values arrive as the line now remains on "1" constantly.

因此,驱动程序只会看到没有任何数据,但不会因为插头拔掉而无法看到任何数据.

Consequently, the driver just sees that there aren't any data, but not that there can't be any data as the plug is unplugged.

这篇关于当我拔下串行电缆时,Linux read() 调用不返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆