[英]Language C serial port : read non-canonical mode
I'm trying to get data from a STM32 Nucleo Board ( but it doesn't really matter ^^ ). 我正在尝试从STM32 Nucleo开发板获取数据(但这并不重要^^)。 I want to get raw data and use the non-canonical mode and everything is set up for.
我想获取原始数据并使用非规范模式,并且一切均已设置。 I have set VMIN to 7 to wait until 7 characters are unread and the read wait 7 characters aswell.
我将VMIN设置为7,以等待直到未读7个字符,并且读也等待7个字符。 However my read returns value before to get 7 values and I don't understand why.
但是我的读之前返回值以获得7个值,我不明白为什么。 Do you have any idea ?
你有什么主意吗 ? Am I understanding in a wrong way VMIN ?
我是否以错误的方式理解了VMIN? Thanks for helping me !
感谢您的帮助!
My code : 我的代码:
int main(){
int r=-1;
char * device = "/dev/ttyS3";
pt = open(device, O_RDWR | O_NOCTTY | O_SYNC);
if(pt == -1){
perror("open");
exit(-1);
}
printf("open : %d\n",pt);
ioctl(pt, I_SRDOPT, RMSGD);
tcgetattr(pt, &old);
atexit(reset_tty);
tcgetattr(pt, &tty); // Get the current attributes of the Serial port
//cfmakeraw(&tty);
tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF);
tty.c_oflag &= ~(OPOST);
tty.c_cflag |= (CS8);
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tty.c_cc[VMIN] = 7; // wait 7 characters
tty.c_cc[VTIME] = 0;
cfsetispeed(&tty,B9600); // Setting the Baud rate
cfsetospeed(&tty,B9600);
sleep(1);
r = tcflush(pt, TCIFLUSH);
printf("tcflush : %d\n",r);
r = tcsetattr(pt, TCSANOW, &tty);
printf("tcsetattr : %d\n",r);
int i = 0;
char end[20];
int count = 0;
char line[20];
memset(line,0,sizeof(line));
char forWrite[2]={'A','\0'};
while(count<10){
r = read(pt ,line,7);
printf("number of characters : %d\n",r);
printf("line = %s\n",line);
//printf("l = %c\n",line[0]);
count++;
memset(line,0,sizeof(line));
printf("\n");
}
return 0;
}
My output : 我的输出:
number of characters : 1
line =
number of characters : 2
line = 12
number of characters : 2
line = 33
number of characters : 5
line = 456ab
number of characters : 5
line = cdefg
number of characters : 1
line = a
number of characters : 4
line = bcde
number of characters : 2
line = fg
number of characters : 1
line = 1
number of characters : 3
line = 233
And i'm sending from my STM32 "abcdefg" and then "1234567" 我从我的STM32发送“ abcdefg”,然后从“ 1234567”发送
EDIT : the code and the output I posted was matching, I have just changed my prints in order to be clearer for you...and I failed ^^, It's edited now thx :) 编辑:我发布的代码和输出是匹配的,我刚刚更改了我的打印件以便为您更清楚...而我失败了^^,现在进行了编辑:)
我不知道这是怎么回事,但是“慢速”设备(例如串行端口)上的read()
(和其他I / O接口)将在等待完成时收到信号后立即返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.