读取 uart 十六进制值会忽略某些 bytes/characters

Reading uart hex values ignores certain bytes/characters

我是 C/C++ 的新手,我正在尝试使用十六进制值通过 uart 进行通信。

Device port: /dev/ttyS2. Baudrate: 38400

我正在使用 redis 订阅消息。为了进行测试,我使用了 "Termite",一个 RS232 终端来模拟。

我找到了一些非常有用的指南,问题是当我尝试阅读消息时,一些 bytes/characters 弄乱了它。

这是我的连接代码:

    this->fd = open(device,O_RDWR | O_NOCTTY);

    /*---------- Setting the Attributes of the serial port using termios structure --------- */
    tcgetattr(this->fd, &SerialPortSettings);   /* Get the current attributes of the Serial port */

    /* Setting the Baud rate */
    cfsetispeed(&SerialPortSettings,B38400); /* Set Read  Speed as 38400 */
    cfsetospeed(&SerialPortSettings,B38400); /* Set Write Speed as 38400 */

    /* 8N1 Mode */
    SerialPortSettings.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
    SerialPortSettings.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
    SerialPortSettings.c_cflag &= ~CSIZE;    /* Clears the mask for setting the data size             */
    SerialPortSettings.c_cflag |=  CS8;      /* Set the data bits = 8                                 */

    SerialPortSettings.c_cflag &= ~CRTSCTS;       /* No Hardware flow Control                         */
    SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines       */

    SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          /* Disable XON/XOFF flow control both i/p and o/p */
    SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);  /* Non Cannonical mode                            */

    SerialPortSettings.c_oflag &= ~OPOST; /*No Output Processing*/
    SerialPortSettings.c_oflag = 0;

    /* Setting Time outs */
    SerialPortSettings.c_cc[VMIN] = 1;  /* Read at least X characters */
    SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */

    tcflush(this->fd, TCIFLUSH);

    if((tcsetattr(this->fd,TCSANOW,&SerialPortSettings)) != 0) { /* Set the attributes to the termios structure*/
        printf("\n  ERROR ! in Setting attributes");
    }
    else {
        tcsetattr(fd, TCSANOW, &SerialPortSettings);
        printf("\n  BaudRate = 38400 \n  StopBits = 1 \n  Parity   = none\n\n");
    }

这里是阅读的代码:

        char read_buffer[32];
        int bytes_read;

        while(true) {
            bytes_read = read(serialport.fd, &read_buffer, sizeof(read_buffer)); /* Read the data */

            printf("bytes read: %d", bytes_read);

            if (bytes_read < 0) {
                printf("Error reading: %s", strerror(errno));
            }
            else if (bytes_read > 0) {
                //read_buffer[bytes_read] = '[=12=]';

                printf("\n");
                printf("HEX:");
                for (int i=0; i<bytes_read; i++) {   /*printing only the received characters*/
                    printf(" %02x",read_buffer[i]);
                }
                printf("\n");
                printf("+----------------------------------+\n");


                /*#if defined(MODULE_REDIS) || defined(MODULE_ALL)
                    //redis.publish("channel:uart:ack", read_buffer);
                    redis.publish("channel:uart:ack", vectorUint8toHex(read_data).c_str());
                #endif*/
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }

当我发送消息0x2401010203040506070809af23时,它只显示8个字节。 (字节 0x030x04 与消息混淆)

OUTPUT:

bytes read: 0bytes read: 8
HEX: 05 06 07 08 09 af 23 0a
+----------------------------------+

但是当我发送 0x2401010200000506070809af23 时,它工作正常

OUTPUT:

bytes read: 14
HEX: 24 01 01 02 00 00 05 06 07 08 09 af 23 0a
+----------------------------------+

我做错了什么?还有一件事情。使用 Termite 必须打开 "append LF" 设置,否则我无法阅读消息,但这会将“0a”添加到我的message,这个跟串口配置有关系吗?

好的,所以问题是二进制中的某些字节是 ASCII "end of text" 和 "end of transmission" 中的 0x03 和 0x04。并且在配置中缺少这一行:

SerialPortSettings.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);

感谢评论,搜索了一些缺失的配置,找到了

还有一个 post 我当时没有发现的问题: