我从linux发送数据字节到串口RS232的窗口然后一切正常,只有我必须处理0xa从linux发送,因为windows将其读为0xd + 0xa。 但是当我从Windows到Linux的一些字节发送数据字节被替换为 - 窗口发送 - 0xd中Linux的接受是0xA 窗口发送 - 为0x11 Linux的整数东西收到垃圾tyte价值8200linux和windows之间的串行通信
普莱舍解释什么不顺心的时候我从Windows发送数据到Linux。 由于事先
Windows串行端口初始化
char *pcCommPort = "COM1";
hCom = CreateFile(TEXT("COM1"),
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
fSuccess = GetCommState(hCom, &dcb);
FillMemory(&dcb, sizeof(dcb),0);
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = CBR_115200; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fOutxCtsFlow = false;
fSuccess = SetCommState(hCom, &dcb);
buff_success = SetupComm(hCom, 1024, 1024);
COMMTIMEOUTS cmt;
// ReadIntervalTimeout in ms
cmt.ReadIntervalTimeout = 1000;
cmt.ReadTotalTimeoutMultiplier = 1000;
cmt.ReadTotalTimeoutConstant=1000;
timeout_flag = SetCommTimeouts(hCom, &cmt);
窗口写serial-
WriteFile(hCom, buffer, len, &write, NULL);
Linux的串行initialize-
_fd_port_no = open("//dev//ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
tcgetattr(_fd_port_no, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CS8);
options.c_cflag|=(CLOCAL|CREAD);
options.c_cflag &=~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag |= (IXON | IXOFF | IXANY);
options.c_cflag &= ~ CRTSCTS;
tcsetattr(_fd_port_no, TCSANOW, &options);
读取串口的linux
while(read(_fd_port_no,buffer+_buffer_len,sizeof(buffer))>0)
{
_buffer_len = _buffer_len+sizeof(buffer);
}
是的,正如我从Linux告诉windows只检测到NL/CR问题,但我解决了它通过字节替换, ,但你有任何关于从Windows发送到Linux(字节替换策略)的串行数据的想法。 其实我有送200 KB的文件在200字节的块通过串行所以它如果从Windows发送到Linux
字节的可更换
所以你要发送原始字节,而不是发短信?显示发送和接收的代码;尽可能少的代码当然。 –
请注意,“0x0a”是换行符,“0x0d”是回车符。听起来像典型的NL/CR问题。 –
如果您从Windows发送文件,是否以文本模式或二进制模式打开了该文件?确保两端的所有串行设置都相同,例如你似乎在Linux端有XON/XOFF,但不在Windows端(不使用XON/XOFF) – nos