霍尼韦尔中国HPMA115S0的Pocketbeagle串行端口通信问题

问题描述

我想与运行C程序的honeywell 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,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",strerror(errno));
}

有帮助吗? 谢谢!

解决方法

您的终端需要的是 non-canonical (a.k.a. raw) mode。 GNU libc 文档有一个很好的 minimal example 说明它是如何设置的。您的代码没有清除 ICANON 标志,这可能是关键标志。它应该看起来像这样:

if(tcgetattr(fd,&tty) != 0) {
    printf("Error %i from tcgetattr: %s\n",errno,strerror(errno));
}
tty.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
cfsetospeed(&tty,9600);
cfsetispeed(&tty,9600);
if (tcsetattr(fd,TCSAFLUSH,&tty) != 0) {
   printf("Error %i from tcsetattr: %s\n",strerror(errno));
}