c - Pocketbeagle 与 Honeywell HPMA115S0 的串行端口通信问题
问题描述
我想与运行 C 程序的霍尼韦尔 HPMA115S0 传感器通信。目标系统是运行 Debian 的 PocketBeagle。
我可以通过仅设置端口和 BPS 使用“屏幕”实用程序与传感器通信。我还可以使用 python3 和串行库进行通信,因此我排除了任何硬件问题。
但我不能用 C 程序来做。一切似乎都很好,但是当我期待 ACK 时,我什么也没有收到。一个奇怪的方面是,如果我运行 screen 或 python 脚本并关闭它,那么我可以使用我的 C 程序进行正确通信。
我运行 stty 以检查系统启动时的差异,然后在 C 程序之后和屏幕之后,但似乎没有任何原因。我想我必须设置正确的串行掩码。现在我使用:
int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
}
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
cfsetospeed(&tty, 9600);
cfsetispeed(&tty, 9600);
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
}
有什么帮助吗?谢谢!
解决方案
我用 strace 运行了 Python 脚本和 C 程序,我注意到问题出在:
cfsetospeed(&tty, 9600);
cfsetispeed(&tty, 9600);
它应该使用“B9600”而不是“9600”。
正确的形式是:
cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);
推荐阅读
- holtwinters - 霍尔特冬天阿尔法
- android - 我的 Android 应用程序版本超出预期
- django - 西里尔字母变成了问号 | Django 模板 HTML 元标记
- python - OSError: [WinError 126] 在 pyinstaller 中找不到指定的模块
- json - Swift : Codable Struct Always Nil
- javascript - 有没有更好的方法来遍历 React 渲染部分中的数据?
- php - 使用 PHP 在 MySQL 中插入多行
- javascript - 如何动态导入、修改和导出矢量文件?
- python - 如何在 64 位上使用格式字符串漏洞写入地址
- java - @Transactional 在带有 CrudRepository 的 Spring Boot 中不起作用