[英]is this linux serial port configured the same as the windows one
我正在尝试将程序移植到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;
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.