我正在尝试将程序移植到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和输入/输出错误,这是没有用的。
我是否以与原始程序相同的方式配置了我的串口?
任何人都可以提供有关如何调试此问题的建议,因为这不是我的专业领域
答案 0 :(得分:1)
也许一个或多个控制线未正确设置。有关tty_ioctl(4)
及相关内容,请查看TIOCMSET
部分“调制解调器控制”。您的Windows代码看起来至少要设置DTR。