c++—读取uart十六进制值会忽略某些字节/字符

fykwrbwg  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(491)

我是c/c++新手,我正在尝试使用十六进制值通过uart进行通信。
设备端口:/dev/ttys2。波特率:38400
我正在使用redis订阅消息。为了测试,我使用了“白蚁”,一个rs232终端来模拟。
我找到了一些非常好用的指南,问题是当我试图阅读信息时,有些字节/字符会弄乱它。
以下是我的连接代码:

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] = '\0';

                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));
        }

当我发送消息0x24010102003040506070809af23时,它只显示8个字节(字节0x03和0x04与消息相混淆)

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”添加到我的消息中,这与串行端口的配置有关吗?

lqfhib0f

lqfhib0f1#

好的,问题是二进制中的一些字节是0x03和0x04,它们是ascii的“文本结束”和“传输结束”。在配置上缺少这一行:

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

感谢这些评论,我搜索了一些缺少的配置并找到了它。
还有一篇关于我的问题的帖子,我当时没有找到:二进制串口读取c中丢失的字节

相关问题