简体   繁体   English

使用C进行串口通讯

[英]Serial Port communication using C

I am developing a software wherein I have to receive data via serial port. 我正在开发必须通过串行端口接收数据的软件。 I am opening the port in canonical mode and setting the VEOL flag to 0X78 (hexadecimal). 我以规范模式打开端口并将VEOL标志设置为0X78 (十六进制)。 The problem I am facing is that read is exiting every time it receives byte value 0X0A in between. 我面临的问题是,读取每次在其间接收到字节值0X0A时都将退出。 Can somebody help me in resolving this issue, I want read to block till I receive end byte as 0X78 . 有人可以帮我解决这个问题,我想读阻塞直到我收到结束字节为0X78 This is the code I have written so far 这是我到目前为止编写的代码

struct termios tio;
char buf[255];

int fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); 
if (fd <0) {
    perror(MODEMDEVICE);
    exit(−1); 
}

bzero(&tio, sizeof(tio));
tio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
tio.c_iflag = IGNPAR;
tio.c_oflag = 0;
tio.c_lflag = 1;
tio.c_cc[VEOL] = 0X78;

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&tio);


int read=read(fd,buf,255);

printf("Number of bytes read is %d\n",read);

Thank you in advance. 先感谢您。

The manual page states: 手册页指出:

VEOL (0, NUL) Additional end-of-line character (EOL). VEOL(0,NUL)附加行尾字符(EOL)。 Recognized when ICANON is set. 设置ICANON时可识别。

So, it doesn't replace all end-of-line characters, it just adds one more, meaning 0xa is still active. 因此,它不会替换所有行尾字符,而只会添加一个,这意味着0xa仍处于活动状态。

Don't send the wrong data, or filter them out in software. 不要发送错误的数据,也不要在软件中将其过滤掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM