【问题标题】:Serial Programming: Sender and Receiver串行编程:发送者和接收者
【发布时间】:2014-01-09 08:01:39
【问题描述】:

所以我在一个项目中使用两个 Xbee ZB (S2B) 将数据从一个传输到另一个。它是一个8位数据,无奇偶校验,1个停止位系统(8N1)。

我有两个问题。

1. 由于我在笔记本电脑上通过 RS232(DB9 连接器)接口连接到 USB 适配器,因此以 B230400 的波特率将系统调用 fwrite/fread /fopen/fclosewrite/read/open/close 更好用吗? (我假设 fread() 不会正常工作,因为它没有波特率据我所知,为配置评分。)

2. 我有一台计算机运行一个程序(发射器程序)和另一个程序(接收器程序)。两个程序都需要能够读写,因为我希望能够从发送和接收端知道不同的信息集。

就像接收端(如果我看到字母Q,停止并关闭端口)或发送端(等待读取端发送字母G开始写入并在写入完成后关闭)。

在非规范输入处理下使用来自 (http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html) 的一些代码,我尝试设置一个基本模板,用于从任一侧的 USB 端口写入和读取。我的问题是:

*发送和接收程序是否需要对 termios struct (newtio) 进行不同的配置设置(c_cflag、c_iflag、c_oflag、c_lflag)?*

我的代码:

#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>

int open_port(char *path, int modes)
{
    int fd; // File descriptor for port

    fd = open(path, modes); // Open the path
    if(fd == -1) return -1; // Could not be opened

    printf("Port %s opened successfully.\n", path);
    return fd; // Return fd
}

int setBaud(struct termios *termios_p, int baud)
{
    int ires = 0; int ores = 0;
    switch(baud)
    {
        0:
            ires = cfsetispeed(&termios_p, B0);
            ores = cfsetispeed(&termios_p, B0);
            break;
        50:
            ires = cfsetispeed(&termios_p, B50);
            ores = cfsetispeed(&termios_p, B50);
            break;
        75:
            ires = cfsetispeed(&termios_p, B75);
            ores = cfsetispeed(&termios_p, B75);
            break;
        110:
            ires = cfsetispeed(&termios_p, B110);
            ores = cfsetispeed(&termios_p, B110);
            break;
        134:
            ires = cfsetispeed(&termios_p, B134);
            ores = cfsetispeed(&termios_p, B134);
            break;
        150:
            ires = cfsetispeed(&termios_p, B150);
            ores = cfsetispeed(&termios_p, B150);
            break;
        200:
            ires = cfsetispeed(&termios_p, B200);
            ores = cfsetispeed(&termios_p, B200);
            break;
        300:
            ires = cfsetispeed(&termios_p, B300);
            ores = cfsetispeed(&termios_p, B300);
            break;
        600:
            ires = cfsetispeed(&termios_p, B600);
            ores = cfsetispeed(&termios_p, B600);
            break;
        1200:
            ires = cfsetispeed(&termios_p, B1200);
            ores = cfsetispeed(&termios_p, B1200);
            break;
        1800:
            ires = cfsetispeed(&termios_p, B1800);
            ores = cfsetispeed(&termios_p, B1800);
            break;
        2400:
            ires = cfsetispeed(&termios_p, B2400);
            ores = cfsetispeed(&termios_p, B2400);
            break;
        4800:
            ires = cfsetispeed(&termios_p, B4800);
            ores = cfsetispeed(&termios_p, B4800);
            break;
        9600:
            ires = cfsetispeed(&termios_p, B9600);
            ores = cfsetispeed(&termios_p, B9600);
            break;
        19200:
            ires = cfsetispeed(&termios_p, B19200);
            ores = cfsetispeed(&termios_p, B19200);
            break;
        38400:
            ires = cfsetispeed(&termios_p, B38400);
            ores = cfsetispeed(&termios_p, B38400);
            break;
        57600:
            ires = cfsetispeed(&termios_p, B57600);
            ores = cfsetispeed(&termios_p, B57600);
            break;
        115200:
            ires = cfsetispeed(&termios_p, B115200);
            ores = cfsetispeed(&termios_p, B115200);
            break;
        230400:
            ires = cfsetispeed(&termios_p, B230400);
            ores = cfsetispeed(&termios_p, B230400);
            break;
        default:
            ires = cfsetispeed(&termios_p, B9600);
            ores = cfsetispeed(&termios_p, B9600);
            break;
    }

    if(ires == -1 | ores == -1) return -1; // ISpeed or OSpeed could not be set
    else { printf("Baud set successfully to %d\n", baud); return 0; }
}

char* readIt(int fd)
{
    char buf;
    char *tmp;
    tmp = calloc(2, sizeof(char);

    if( read(fd, buf, 1) == 1 ) { tmp[0] = buf; tmp[1] = 0; } // Return char in tmp[0]
    else { tmp[0] = 0x45; tmp[1] = 0x52; } // Return ER in char meaning error
    return tmp;
}

int writeIt(int fd, char *str, int bytes)
{
    if( write(fd, str, bytes) == bytes ) return 0; // Write success
    else return -1; // Failed write
}

int main (int argc, char **argv)
{
    int usb;
    volatile int STOP = 0;

    if(argc == 1) { printf("Must enter usb port path.\n"); exit(EXIT_FAILURE); } // No second argument
    else if (argc > 1) { usb = open_port(argv[1], O_RDWR | O_NOCTTY ); }

    struct termios oldterm, newterm;

    tcgetattr(fd, &oldtio); // Save old terminal settings

    bzero(&newtio, sizeof(newtio)); // Clear out struct
    if( setBaud(&newtio, 230400) != 0 ) { printf("Baud could not be set\n"); exit (EXIT_FAILURE); } // Set up baud rate

    newtio.c_cflag = CS8 | CREAD | CLOCAL; // 8 data bits, enable receiver, local line 
    newtio.c_cflag &= ~PARENB | ~CSTOPB; // No parity, one stop bit 
    newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
    newtio.c_oflag = 0;
    newtio.c_lflag = 0;
    newtio.c_cc[VTIME] = 0; // No timeout clock for read
    newtio.c_cc[VMIN] = 1; // Wait for one character before reading another

    tcflush(fd, TCIFLUSH); 
    tcsetattr(fd, TCSANOW, &newtio); // Set fd with new settings immediately

    /* Do writing or reading from port in here - readIt or writeIt*/

    tcsetattr(fd, TCSANOW, &oldtio); // Set fd with old settings immediately
    close(fd); // Close fd

    return 0;
}

如果可能,我正在寻找最佳的写入和读取速率,此时没有数据损坏处理。通过优化,我说的是 termios 结构的配置标志的最佳设置。

感谢您的帮助。

【问题讨论】:

  • “我正在寻找没有数据损坏处理的最佳写入和读取速率” -- 一旦数据到达接收器,数据丢失的唯一可能性就是接收器或缓冲区溢出。您一次只读取一个字节的慢速方法可能会导致缓冲区溢出。这绝对是对 read() 系统调用的低效使用。

标签: c serial-port xbee termios


【解决方案1】:

关于第一点,使用fdopen 从文件描述符中获取FILE* 很容易。但是,您必须记住 FILE* 默认情况下是缓冲的,因此您写入的内容可能不会立即发送。

【讨论】:

    【解决方案2】:

    看看这个ANSI C Host Library 与 XBee 模块进行通信。 xbee_serial_posix.c 文件包括完整的串行接口,驱动程序中的更高层提供 API 帧的解析和分派。还有很多示例程序。

    您可以直接使用该库,也可以参考串行层编写自己的 termios 串行代码。

    它主要设计用于在“API 模式”(ATAP=1) 下使用 XBee 模块,但也有一些用于“AT 模式”的部分(如xbee_term 示例)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2021-04-03
      • 2023-03-03
      • 2015-07-28
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多