[英]New line character significance in serial port communication
We are testing serial port communication. 我们正在测试串行端口通信。 There are two tty nodes /dev/ttyUSB8 and /dev/ttyUSB9 on the device.
设备上有两个tty节点/ dev / ttyUSB8和/ dev / ttyUSB9。
When I transmit buffer from /dev/ttyUSB8 to /dev/ttyUSB9 I don't receive data on the /dev/ttyUSB9 read call if the buffer doesn't contain new line. 当我将缓冲区从/ dev / ttyUSB8传输到/ dev / ttyUSB9时,如果缓冲区不包含新行,则不会在/ dev / ttyUSB9读取调用中接收到数据。
Transmit Code 传输码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
void write_func()
{
int fdw;
int i;
fdw = open("/dev/ttyUSB8", O_RDWR);
printf("fdw : %d\n",fdw);
printf("%ld\n", write(fdw, "Hello", 6));
close(fdw);
}
int main()
{
int i;
write_func();
return 0;
}
Receive Code 接收密码
void read_thread()
{
int fdr;
char buf[] = "NoData";
fdr = open("/dev/ttyUSB9", O_RDWR);
printf("fdr : %d\n",fdr);
printf("%s: %ld\n", __func__, read(fdr, buf, 6));
printf("%s\n", buf);
close(fdr);
}
int main()
{
int i;
read_thread();
return 0;
}
I don't receive data with the above call, but when I add '\\n in the write call i get data in the read block call. 我没有通过上述调用接收数据,但是在写调用中添加'\\ n时,我在读块调用中获取了数据。
printf("%ld\n", write(fdw, "Hello\n", 7));
What is the significance of new line character in this.. 在此换行符的意义是什么?
Update: 更新:
I added the code to reset canonical mode, still it didn't work: 我添加了代码以重置规范模式,但仍然无法正常工作:
void write_thread()
{
int fdw;
int i;
struct termios config;
fdw = open("/dev/ttymib24", O_RDWR);
printf("fdw : %d\n",fdw);
tcgetattr(fdw, &config);
config.c_lflag &= ~ICANON;
tcsetattr(fdw, TCSANOW, &config);
printf("%ld\n", write(fdw, "Hello", 6));
close(fdw);
}
Your tty is probably in canonical mode. 您的tty可能处于规范模式。
Try to reset ICANON by using tcsetattr()
. 尝试使用
tcsetattr()
重置ICANON。 Something like this: 像这样:
struct termios termiosv;
tcgetattr(fd, &termiosv);
termiosv.c_lflag &= ~ICANON;
tcsetattr(fd, TCSANOW, &termiosv);
More information in man page of termios : termios手册页中的更多信息:
In canonical mode: * Input is made available line by line. An input line is available when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at the start of line). Except in the case of EOF, the line delimiter is included in the buffer returned by read(2).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.