嵌入式串行读取操作和桌面PC之间可能有什么区别
What could be the difference between an embedded serial read operation and a Desktop- PC
在嵌入式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()看起来像个解决方法,但找不到更好的解决方案。此外,以下主题非常有帮助。清除串行端口的缓冲区
串行端口二进制转移更改马车返回
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 通过USB在PC和Arduino之间进行串行通信
- 嵌入式串行读取操作和桌面PC之间可能有什么区别
- PC和Arduino之间的串行通信
- C++插座:PC之间通过互联网进行通信
- Sizeof() 在 PC 和 Arduino 上的 C++ 之间的差异
- 在安卓和PC之间创建Wifi点对点连接
- 通过串行方式在Arduino和PC之间传递变量的最佳方式
- Arduino和PC之间通过Usb串行端口(C++控制台应用程序)进行通信
- 用c++实现pc机与arduino之间的RS232串行通信
- WinCE设备和Windows PC之间通过USB进行通信