首页 > 解决方案 > 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));
}

有什么帮助吗?谢谢!

标签: clinuxbeagleboard

解决方案


我用 strace 运行了 Python 脚本和 C 程序,我注意到问题出在:

cfsetospeed(&tty, 9600);
cfsetispeed(&tty, 9600);

它应该使用“B9600”而不是“9600”。

正确的形式是:

cfsetospeed(&tty, B9600);
cfsetispeed(&tty, B9600);

推荐阅读