嵌入式串行读取操作和台式PC之间可能有什么区别

时间:2019-02-21 16:01:30

标签: c++ linux embedded-linux uart

在嵌入式Linux系统中,我正在对项目进行编码,该项目将在物理回送环境中测试串行端口。这表示我正在连接rx-tx。外围输出为RS-232

为了测试端口,我正在发送1个字节,然后读取发送的字节。我将这个循环从0x00重复到0XFF。我正在为UART使用原始输入类型。

如果我在Linux台式PC上运行代码,看起来很好。

但是,在我的嵌入式Linux系统上,我无法正确读取RS-232连接。最后,我读取了返回零。

您如何看待可能的问题?

我正在检查参考Serial Programming Guide for POSIX Operating Systems的termios配置

         StatusResult  UartInterface::openComPort() {

           m_fileDesc = open(m_device.c_str(), O_RDWR | O_NOCTTY );

            if (m_fileDesc == -1) {
                retStatus.type = COMM_ERROR;
            }
            configureUART();
            return retStatus;
        }


        void UartInterface::configureUART(){

                struct termios options;

                tcgetattr(m_fileDesc, &options);

                cfsetispeed(&options, B9600);
                cfsetospeed(&options, B9600);

                options.c_cflag |= (CLOCAL |CREAD);

                tcsetattr(m_fileDesc, TCSANOW, &options);

                options.c_cflag &= ~CSIZE;
                options.c_cflag |= CS8;
                options.c_cflag &= ~PARENB;
                options.c_cflag &= ~CSTOPB;
                options.c_cflag &= ~CRTSCTS;
                /*=============================================*/

                options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

                /*=============================================*/

                options.c_iflag &= (IXON | IXOFF | IXANY);

                /*=============================================*/

                options.c_oflag &= ~OPOST;

                /*=============================================*/

                options.c_cc[VMIN] = 0;
                options.c_cc[VTIME] = 10;

                tcsetattr(m_fileDesc, TCSANOW, &options);

            }

这是我的主要测试循环

    std::cout<<"-----------UART DEBUG-------------\n";


    while( int(write_data) < 255 ){
        n = write(m_fileDesc, &write_data, 1);
        if( n != 1) {
            std::cout << "UART write failed!\n";
            res=false;
            return res;
        }

        n = read(m_fileDesc, &read_data, 1);
        if ( n == 1){
            if(read_data != write_data) {
                std::cout << "UART mismatch error!\t data_read:0x" << int(read_data)<<"   data write:0x"<<int(write_data)<< std::endl;
                res=false;
                //return res;
            }

            std::cout<<std::hex<<"Byte: 0x"<<int(read_data) <<"  is OK!"<<std::endl;


        }
        else {
            std::cout << "UART read failed! Res: "<<n<<"Errno"<< strerror(errno)<<std::endl;
            res=false;
            return res;
        }
        write_data++;
    }
    (res) ? (std::cout<<"Uart interface test OK!"<<std::endl) : ((std::cout<<"UART FAILED!!"<<std::endl));
    return res;
}

这是嵌入式linux系统的输出,如您所见,读写数据完全不同。重复多次后,它以零读取结束吗?

    -----------UART DEBUG-------------
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x1
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x2
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x3
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x4
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x5
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x6
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x7
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x8
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x9
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x1   data write:0xa
Byte: 0x1  is OK!
UART mismatch error!     data_read:0x2   data write:0xb
Byte: 0x2  is OK!
UART mismatch error!     data_read:0x3   data write:0xc
Byte: 0x3  is OK!
UART mismatch error!     data_read:0x4   data write:0xd
Byte: 0x4  is OK!
UART mismatch error!     data_read:0x5   data write:0xe
Byte: 0x5  is OK!
UART mismatch error!     data_read:0x6   data write:0xf
Byte: 0x6  is OK!
UART mismatch error!     data_read:0x7   data write:0x10
Byte: 0x7  is OK!
UART mismatch error!     data_read:0x8   data write:0x11
Byte: 0x8  is OK!
UART mismatch error!     data_read:0x9   data write:0x12
Byte: 0x9  is OK!
UART mismatch error!     data_read:0xa   data write:0x13
Byte: 0xa  is OK!
UART mismatch error!     data_read:0xb   data write:0x14
Byte: 0xb  is OK!
UART mismatch error!     data_read:0xc   data write:0x15
Byte: 0xc  is OK!
UART mismatch error!     data_read:0xd   data write:0x16
Byte: 0xd  is OK!
UART mismatch error!     data_read:0xe   data write:0x17
Byte: 0xe  is OK!
UART mismatch error!     data_read:0xf   data write:0x18
Byte: 0xf  is OK!
UART mismatch error!     data_read:0x10   data write:0x19
Byte: 0x10  is OK!
UART mismatch error!     data_read:0x12   data write:0x1a
Byte: 0x12  is OK!
UART read failed! Res: 0 Errno: No such file or directory

1 个答案:

答案 0 :(得分:0)

通过3个更正解决了该问题:

首先,我发现有关termios配置的错误。这是我的。流控制必须关闭,并且Tildy看上去不见了。

 options.c_iflag &= (IXON | IXOFF | IXANY); //FALSE
 options.c_iflag &= ~(IXON | IXOFF | IXANY); //CORRECT

毕竟,它计算出读取归零问题。我最多可以完成255个测试数据。但是,读写数据仍然不匹配!

第二,我清除了所有termios标志,而不是从系统中读取。这使我不受操作环境的影响。

然后我在tcsetattr()之后添加了以下两行以刷新第一个数据。 sleep()是必需的,因为ioctl syscall无法找到要刷新的任何数据,除非您稍等片刻。

 ....
//tcgettattr(m_fileDesc, &options); //Ignore default system data
memset(&options, 0, sizeof options);
.
.
tcsetattr(m_fileDesc, TCSANOW, &options);
sleep(2);
tcflush(m_fileDesc, TCIOFLUSH);

sleep()看起来是一种解决方法,但是我找不到更好的解决方案。此外,以下主题非常有帮助。 Clearing the serial port's buffer

Serial port binary transfer changes carriage return