简体   繁体   English

使用串行端口时接收额外的空字节

[英]Receive extra null byte when using serial port

I'm writing a program for serial port data transmission on Linux, but find that every time the sender opens the port, the receiver gets an extra null byte '\\x00' . 我正在为Linux上的串行端口数据传输编写程序,但是发现发送方每次打开端口时,接收方都会得到一个额外的空字节'\\x00'

Here's the code of sender: 这是发件人的代码:

#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
  int fd_com_ = open("/dev/ttyAM0", O_RDWR | O_NOCTTY | O_NONBLOCK);

  struct termios attrs_;
  attrs_.c_iflag = IGNBRK;
  attrs_.c_oflag = 0;
  attrs_.c_cflag = (CLOCAL | CREAD);
  attrs_.c_cflag |= CS8;
  attrs_.c_lflag = 0;
  attrs_.c_cc[VMIN] = 0;
  attrs_.c_cc[VTIME] = 0;
  cfsetspeed(&attrs_, B115200);

  tcsetattr(fd_com_, TCSANOW, &attrs_);

  const char *s = "abcd";
  write(fd_com_, s, 4);
  sleep(1);
  write(fd_com_, s, 4);
  sleep(1);
  close(fd_com_);

  fd_com_ = open("/dev/ttyAM0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  write(fd_com_, s, 4);

  return 0;
}

The receiver has the same configuration, but receives "\\x00abcdabcd\\x00abcd" . 接收者具有相同的配置,但是接收到"\\x00abcdabcd\\x00abcd" How to fix this problem so the receiver could get "abcdabcdabcd" ? 如何解决此问题,以便接收者可以获取"abcdabcdabcd"


Update: 更新:

The code of receiver: 接收方代码:

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
  int fd_com_ = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);

  struct termios attrs_;
  attrs_.c_iflag = IGNBRK;
  attrs_.c_oflag = 0;
  attrs_.c_cflag = (CLOCAL | CREAD);
  attrs_.c_cflag |= CS8;
  attrs_.c_lflag = 0;
  attrs_.c_cc[VMIN] = 0;
  attrs_.c_cc[VTIME] = 0;
  cfsetspeed(&attrs_, B115200);

  tcsetattr(fd_com_, TCSANOW, &attrs_);

  char buf[100];
  ssize_t sz;
  while(1) {
    sz = read(fd_com_, buf, 100);
    if (sz > 0) {
      for (ssize_t i=0; i<sz; i++) {
        printf("%02hhx\n", buf[i]);
      }
    }
    sleep(1);
  }

  return 0;
}

Please check in your ARM board's documentation how the ARM's UART is hooked up and how it is configured in hardware and by your platform driver. 请检查ARM板的文档,以了解如何连接ARM的UART,以及如何在硬件中以及通过平台驱动程序对其进行配置。

From what you describe, I would suppose that when opening the UART port on the ARM, the physical UART (ie, the hardware peripheral module within the ARM or maybe an externally wired UART chip) is enabled or restored from some unknown idle state to the proper -12 Volt idle state of RS232. 根据您的描述,我假设在打开ARM上的UART端口时,已启用物理UART(即ARM内的硬件外围模块或也许是外部连接的UART芯片)或从某些未知的空闲状态恢复到了UART。 RS232的正确-12伏空闲状态。 This transistion may be enough for your PC's UART to recognize a start bit and receive a bogus character. 此晶体管转移可能足以使您的PC的UART识别起始位并接收虚假字符。

You may want to check the serial line using an oscilloscope to see what happens when actually opening the port. 您可能需要使用示波器检查串行线,以查看实际打开端口时发生的情况。

the initial \\x0 is the indication that a parity or framing error occurred. 初始\\x0表示发生奇偶校验或成帧错误。 this error occurs because the termios fields are not being setup properly. 发生此错误是因为termios字段未正确设置。 so the initial line high/low status and number of start/stop bits and parity are not being setup properly. 因此初始行的高/低状态以及开始/停止位数和奇偶校验设置不正确。

you might read: http://man7.org/linux/man-pages/man3/termios.3.html which discusses each of the fields and their contents and meaning. 您可能会阅读: http : //man7.org/linux/man-pages/man3/termios.3.html ,其中讨论了每个字段及其内容和含义。

(The linked page is way too long to post here) (链接的页面太长了,无法在此处发布)

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

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