C++ 此linux串行端口的配置是否与windows相同
我正在尝试将一个程序移植到linux,但无法使串行端口正常工作 这是windows代码C++ 此linux串行端口的配置是否与windows相同,c++,linux,serial-port,C++,Linux,Serial Port,我正在尝试将一个程序移植到linux,但无法使串行端口正常工作 这是windows代码 if( (idComDev[i] = CreateFile(ComStr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0)) != INVALID_HANDLE_VALUE ) { SetupComm(idComDev[i],1024,1024); cto.ReadIntervalTimeout = MAXDWORD
if( (idComDev[i] = CreateFile(ComStr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0)) != INVALID_HANDLE_VALUE )
{
SetupComm(idComDev[i],1024,1024);
cto.ReadIntervalTimeout = MAXDWORD;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 0;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(idComDev[i],&cto);
sprintf(ComStr,"COM%hd:19,n,8,1",CommNo[i]);
BuildCommDCB(ComStr,&dcb);
dcb.fBinary = TRUE;
dcb.BaudRate = baud;
dcb.fOutxCtsFlow = FALSE; // CTS output flow control
dcb.fOutxDsrFlow = FALSE; // DSR output flow control
dcb.fDtrControl = DTR_CONTROL_ENABLE; // DTR line on
dcb.fDsrSensitivity = FALSE; // DSR sensitivity
dcb.fTXContinueOnXoff = FALSE; // XOFF continues Tx
dcb.fOutX = FALSE; // XON/XOFF out flow control
dcb.fInX = FALSE; // XON/XOFF in flow control
dcb.fErrorChar = FALSE; // enable error replacement
dcb.fNull = FALSE; // enable null stripping
dcb.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control
dcb.fAbortOnError = FALSE; // abort reads/writes on error
dcb.wReserved = 0; // Not used; must be set to zero
dcb.ByteSize = 8; // number of bits/byte, 4-8
dcb.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
dcb.Parity = SPACEPARITY; // use parity as address bit
if( CardType == 2 ) SetCommMask(idComDev[i],EV_TXEMPTY);
SetCommState(idComDev[i],&dcb);
dbprintf("Seg %d = COM%hd\r\n",i,CommNo[i]);
}
这是我的linux代码
idComDev[i] = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (idComDev[i] == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
ret = false;
}
else
{
fcntl(idComDev[i], F_SETFL, 0);
struct termios options;
tcgetattr(idComDev[i], &options); // get current settings
cfsetispeed(&options, B115200); // set baud rate
cfsetospeed(&options, B115200); // set baud rate
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; // set parity to no
options.c_cflag &= ~CSTOPB;//set one stop bit
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8; // 8 bit data
options.c_lflag |= (ICANON);
options.c_iflag &= ~(IXON | IXOFF | IXANY); //disable software flow controll
tcsetattr(idComDev[i], TCSANOW, &options);// save the settings
每当我尝试写入串行端口时,它都会返回-1,并且不会向我提供出错的信息。我尝试使用errorno,这表示输入/输出错误没有帮助
我是否以与原始程序相同的方式配置串行端口
是否有人能就如何调试此问题提供建议,因为这不是我的专业领域可能一条或多条控制线设置不正确。请参阅
tty_ioctl(4)
章节“调制解调器控制”,了解TIOCMSET
和相关内容。您的Windows代码看起来至少要设置DTR。可能一个或多个控制行设置不正确。请参阅tty_ioctl(4)
章节“调制解调器控制”,了解TIOCMSET
和相关内容。您的Windows代码看起来至少要设置DTR