我不知道这个问题是因为VirtualBox还是因为串口配置。我正在测试一个通过串行端口与特定硬件交互的程序。一切都工作正常,但在从设备接收的数据流中,某些字节未正确接收。特别是0x11,0x13丢失,0x0d作为0x0a接收。我有一个串口嗅探器程序,看着串口,那里有字节,但我的程序中没有看到它在VirtualBox linux机器上运行。我需要看到没有流量控制的所有二进制字节。以下是我打开串口的方法:
int openPort( char* portname )
{
int fd;
fd = open( portname, O_RDWR | O_NOCTTY | O_NDELAY );
if ( fd == -1 )
{
perror( "openPort():" );
printf( "Unable to open %s\n", portname );
}
else
{
fcntl( fd, F_SETFL, FASYNC );
struct termios my_termios;
struct termios new_termios;
tcgetattr( fd, &termios_save );
tcgetattr( fd, &my_termios );
cfsetispeed( &my_termios, B9600 );
cfsetospeed( &my_termios, B9600 );
my_termios.c_cflag &= ~( PARENB | CSTOPB | CSIZE );
my_termios.c_cflag |= ( CS8 | CREAD );
my_termios.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
my_termios.c_oflag &= ~OPOST;
my_termios.c_cc[VMIN] = 1;
my_termios.c_cc[VTIME] = 100; // wait 10 seconds for a character
tcsetattr( fd, TCSANOW, &my_termios );
tcgetattr( fd, &new_termios );
if ( memcmp( &my_termios, &new_termios, sizeof( my_termios ) ) != 0 )
{
printf( "Error setting serial port options, aborting\n" );
close( fd );
fd = -1;
}
}
return fd;
}
我已经查看了使用串口的各种指南,包括Serial Programming Guide for POSIX Operating Systems,这就是我提出来的。
答案 0 :(得分:1)
0x11
和0x13
是ASCII XON (DC1, Ctrl-Q)
和XOFF (DC3, Ctrl-S)
的十六进制值,用于在性能有限的应用程序中提供数据流的调步。
主机串口或VirtualBox正在解释这些(或者只是过滤它们以避免后续解释)。在主机中将端口设置为原始模式。你没有说,但如果它是* nix的某种味道,那么stty
可以帮助。
答案 1 :(得分:0)
显然我错过了指南中的一节。我最初从一些我现在无法找到的示例程序中复制了我的初始化代码,因此我不知道他们是否错过了它,或者我是否错过了我的副本。无论如何,问题是c_iflag的设置,我最初没有改变。添加行:
my_termios.c_iflag &= ~( IXON | IXOFF | IXANY | ICRNL | INLCR | IGNCR );
解决这个问题。显然,默认情况下启用了软件流控制和CR-> NL映射。