我计划使用 Pi4 接收来自无线电设备(“协调器”)的 UART 的数据,该设备通过飞思卡尔 MC9S08QE32 编程,以序列化它从客户端接收的更新。我有 30 个左右的客户端节点(相同的设备)每 5 秒向这个协调器发送一次更新。一旦 Coordinator 收到一个客户端节点更新信封,它就会向 RPi4 写入一个具有自己独特格式的字符串。目前仅printf
用于调试。
if ((*((unsigned char*) &message[0])=='A') && (*((unsigned char*) &message[0]+1)=='Z') {
printf("AZ01\t%s\t%d\n", // construct snapshot, appending type#, 2 points, terminator.
snap[t].point_a,
snap[t].point_b;
}
我正在开发解析这些字符串的 RPi4 应用程序,并希望尽可能地简化它。我目前打算用\n
. 看来我对规范/非规范模式缺乏充分理解似乎是我困惑的一个因素。仅此终止字符就可以证明使用规范模式是合理的吗?我不介意使用非规范,这是 Pi 当前配置为运行的方式,但如果我确实拉出终止字符之外的任何内容,我不想只是丢弃它,因为这样做几乎肯定会破坏下一个信息。
话虽如此,我确实控制从协调器到 Pi 的 UART 写入。虽然我觉得我理解VTIME
并且VMIN
,但我不清楚的是,Pi 处理器的内部缓冲区是否有可能在协调器写入之前以某种方式将此字符串部分推送到 read() 请求。\n
尽管在协调器写入之前,字符串是从源代码中完整构建的。缓冲区本身是否没有“锁定”,所以如果VMIN=5
和VTIME=0
,仍然存在仅获得 5-6 个字符的风险?我会设置VMIN
为静态大小,但我的消息因消息类型而异#。
struct termios tty;
memset(&tty, 0, sizeof(tty));
bytesRecv = read(serial_port, &rbuf, sizeof(rbuf)-1); // Read bytes
tty.c_cc[VTIME] = 0; // Wait 0 deciseconds
tty.c_cc[VMIN] = 5; // Read 5 chars minimum from buffer
这里有什么我应该注意的技巧、最佳实践或架构建议吗?
编辑 1:完整的串行配置
int serial_port = open("/dev/ttyAMA1", O_RDWR);
if (serial_port < 0) {
printf("\nError %i from Open serial attempt: %s\n", errno, strerror(errno));
errchk=1;
}
// Configure the terminal's serial settings for correct synchronization of data from host node communication
struct termios tty;
memset(&tty, 0, sizeof(tty));
// Validate by reading in ttyAMA1's (serial_port's) existing settings
if (tcgetattr(serial_port,&tty) != 0) {
printf("\nError %i from tcgetattr: %s\n", errno, strerror(errno));
errchk=1;
}
// Set the actual control parameter fields (c_cflags)
// Control modes
tty.c_cflag &= ~PARENB; // Clear parity bit, disables priority
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag |= (CREAD | CLOCAL); // Turn on READ and ignore control lines like carrier detect for modems (CLOCAL=1)
// Local modes
tty.c_lflag &= ~ICANON; // Set as Non-canonical unix mode (do not process input only when new line char is read)
tty.c_lflag &= ~ECHO; // Disable echos
tty.c_lflag &= ~ECHOE; // Disable erasure echo
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT, SUSP
tty.c_lflag &= ~IEXTEN;
// Input modes
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off software flow control
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable special handling of bytes -- only raw data!
tty.c_iflag = 0; // new
// Output modes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline char
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// Timing settings for read() blocking (VMIN/VTIME)
tty.c_cc[VTIME] = 0; // Wait 0 deciseconds = 0.0 second
tty.c_cc[VMIN] = 5; // Read 5 chars minimum loaded into the buffer before returning from read()
// Baud rate settings
cfsetispeed(&tty, B115200); // Set to 115.2 kBaud IN
cfsetospeed(&tty, B115200); // Set to 115.2 kBaud OUT
tcflush(serial_port,TCIFLUSH);
// Save these TTY settings (and error check)
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("\nError %i from tcsetattr: %s\n", errno, strerror(errno));
errchk=1;
}
// Set the actual control parameter fields (c_cflags)
// Control modes
tty.c_cflag &= ~PARENB; // Clear parity bit, disables priority
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag |= (CREAD | CLOCAL); // Turn on READ and ignore control lines like carrier detect for modems (CLOCAL=1)
// Local modes
tty.c_lflag &= ~ICANON; // Set as Non-canonical unix mode
tty.c_lflag &= ~ECHO; // Disable echos
tty.c_lflag &= ~ECHOE; // Disable erasure echo
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT, SUSP
tty.c_lflag &= ~IEXTEN;
// Input modes
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off software flow control
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable special handling of bytes -- only raw data!
tty.c_iflag = 0; // new
// Output modes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline char
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to car return/line feed
// Timing settings for read() blocking (VMIN/VTIME)
tty.c_cc[VTIME] = 0; // Wait 0 deciseconds = 0.0 second
tty.c_cc[VMIN] = 5; // Read 5 chars minimum loaded into the buffer before returning from read()
// Baud rate settings
cfsetispeed(&tty, B115200); // Set to 115.2 kBaud IN
cfsetospeed(&tty, B115200); // Set to 115.2 kBaud OUT
tcflush(serial_port,TCIFLUSH);
// Save these TTY settings (and error check)
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("\nError %i from tcsetattr: %s\n", errno, strerror(errno));
errchk=1;
}