【问题标题】:Pocketbeagle Serial port communication issue with Honeywell HPMA115S0Pocketbeagle 与 Honeywell HPMA115S0 的串行端口通信问题
【发布时间】:2021-02-13 10:02:42
【问题描述】:

我想与运行 C 程序的 Honeywell HPMA115S0 传感器通信。目标系统是运行 Debian 的 PocketBeagle。

我可以通过仅设置端口和 BPS 使用“屏幕”实用程序与传感器通信。 我也可以使用 python3 和 Serial 库进行通信,所以我排除了任何硬件问题。

但是我不能用 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));
}

有什么帮助吗? 谢谢!

【问题讨论】:

  • 我不确定以下链接是否会有所帮助,您可能已经看过了,但声明 “但我在 RX 上没有收到数据。”,在this post 与您的“当我期望 ACK 我什么也没收到”非常相似,我认为您可能会在那里看到一些有用的东西。
  • 您已经发布了如何配置串行端口,但没有发布从/向它读取和写入的代码。请使用完整的代码更新您的帖子。

标签: c linux beagleboard


【解决方案1】:

我用 strace 运行了 Python 脚本和 C 程序,发现问题出在:

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

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

正确的形式是:

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

【讨论】:

    【解决方案2】:

    您的终端需要的是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", errno, strerror(errno));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2010-11-13
      相关资源
      最近更新 更多