简体   繁体   English

在串行通信的情况下,来自 tcgetattr 的错误 9 是什么

[英]what is error 9 from tcgetattr, in case of serial communication

I making ac program to read data from a serial device.我制作交流程序以从串行设备读取数据。 I am getting 5 values from serial device and I have made a logic through which it is automatically updating values.我从串行设备获取 5 个值,并且我已经制定了一个逻辑,通过它它可以自动更新值。 It runs properly but after sometime it hangs and shows error error 9 from tcgetattr .它运行正常,但一段时间后它挂起并显示error 9 from tcgetattr I have used the code from one of the answers to this question http://tinyurl.com/keuxkgz Error is in the function void set_blocking (int fd, int should_block) and int set_interface_attribs (int fd, int speed, int parity) .我使用了这个问题的答案之一的代码http://tinyurl.com/keuxkgz错误在函数void set_blocking (int fd, int should_block)int set_interface_attribs (int fd, int speed, int parity)中。 I am not able to remove it.我无法删除它。 Please help.请帮忙。 Thanks.!谢谢。!

I'm not going to look at external sources. 我不会看外部资源。 However, errno == 9 == EBADF in Linux. 但是, errno == 9 == EBADF Linux中的errno == 9 == EBADF It means your file descriptor ( fd ) is incorrect. 这意味着您的文件描述符( fd )不正确。

Instead of printing just errno , I recommend you use something like 建议您不要使用只打印errno

if (...) {
    const int errnum = errno;
    fprintf(stderr, "%s: tcsetattr() failed: %s (%d)\n", devicepath, strerror(errnum), errnum);
    exit(EXIT_FAILURE);
}

where devicepath is the variable containing the path to the serial device you're using. 其中devicepath是包含您正在使用的串行设备的路径的变量。 (You need to #include <string.h> for strerror() , #include <errno.h> for errno , and #include <stdlib.h> for exit() and EXIT_FAILURE .) (对于strerror() ,您需要#include <string.h> ;对于errno ,需要#include <errno.h> ;对于exit()EXIT_FAILURE ,则需要#include <stdlib.h> 。)

This happened to me due to permission denial to open the serial port.由于拒绝打开串行端口的权限,这发生在我身上。

After I executed the program with superuser privileges, "sudo" the error disappeared.在我以超级用户权限执行程序后,“sudo”错误消失了。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM