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

What could be the difference between an embedded serial read operation and a Desktop- PC

本文关键字:之间 PC 可能有 什么 区别 桌面 读取 操作 嵌入式      更新时间:2023-10-16

在嵌入式Linux系统中,我正在编码该项目,该项目测试了PYHSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSERAIL端口。这意味着我正在连接RX-TX。外围输出为RS-232

为了测试我要发送的1个字节的端口,然后我读取已发送字节。我将此周期从0x00重复到0xff。我正在为UART使用原始输入类型。

如果我在Linux-Desktop PC上运行代码,看起来不错。

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

您如何看待可能的问题?

我正在检查我的术语配置,引用了POSIX操作系统串行编程指南

         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

用3个更正解决了问题:

首先,我发现了关于Termios配置的错误。这里我的硬件。流量控制必须关闭,并且看起来不明显。

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

毕竟,它发现了零问题的读取返回。我可以将测试数据完成到255。但是,读写数据仍然不匹配!

第二,我清除了所有术语标志,而不是从系统中读取。这使我独立于操作环境。

然后,我在tcsetAttr()之后添加了以下2行,以刷新第一个数据。而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()看起来像个解决方法,但找不到更好的解决方案。此外,以下主题非常有帮助。清除串行端口的缓冲区

串行端口二进制转移更改马车返回