简体   繁体   English

语言C串行端口:读取非规范模式

[英]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.

相关问题 在Linux中用C编写串行端口:规范方法和非规范方法之间的区别 - Writing to a serial port in C in linux: differences between canonical and non-canonical methods 在UNIX和Windows上具有非规范模式的getchar() - getchar() with non-canonical mode on UNIX and Windows 非规范(原始)模式下的异步串行通信,以及在linux / osx中生成SIGIO - Async serial communication in non-canonical (raw) mode and generating SIGIO in linux/osx 如何在非规范模式下从读取函数返回并且VMIN和TIME不等于零 - How to return from read function in Non-Canonical mode and VMIN and TIME not equal to ZERO 为什么即使在非规范模式下也要按Enter键才能读取和输出输入? - Why I still need to press 'Enter" in order to let input be read and output even in non-canonical mode? 在非规范模式下使用终端IO确定按钮边界 - Determining button boundries with terminal IO in non-canonical mode 非规范模式不适用于反引号 - Non-canonical mode don't work on backtick Linux 非规范输入模式在 Linux 和 WSL 中的工作方式不同 - Linux non-canonical input mode works different in Linux and WSL 规范模式 Linux 串口 - Canonical Mode Linux Serial Port 复制并粘贴到非规范模块中 - Copy & paste in non-canonical mod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM