从串口无限循环接收 [英] infinite loop receive from serial port

查看:250
本文介绍了从串口无限循环接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自复制了串行端口配置示例:

from this I copied the example for serial port configuration:

tcgetattr (serialfd, &tty);

cfsetospeed(&tty,B115200);
cfsetispeed(&tty,B115200);

tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag &= ~IGNBRK;
tty.c_lflag = 0;

tty.c_oflag = 0;
tty.c_cc[VMIN]  = 0;
tty.c_cc[VTIME] = 5;

tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= 0;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;

我的实际代码是这样的:

My actual code is like this:

char buf[100];
write(serialfd, "PING", strlen("PING"));
fsync(serialfd);

while (1) 
{
    read(serialfd, buf, sizeof(buf));
    printf("length: %d\n", strlen(buf));
}

在这种情况下,它会不停地无限打印length: 6.当我更改tty.c_cc[VMIN] = 1tty.c_cc[VTIME] = 0时,它不会读取.(它在read()中阻塞)

In this case it is printing length: 6 infinitely without stopping. when I change tty.c_cc[VMIN] = 1 and tty.c_cc[VTIME] = 0 it doesn't read.(it blocks in read())

我正在将Debian 6.0.5与USB转换为串行转换器.我这样打开串口:

I'm using debian 6.0.5 with usb to serial converter. I open serial port like this:

serialfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC);

推荐答案

查看您的代码

while (1) 
{
    read(serialfd, buf, sizeof(buf));
    printf("length: %d\n", strlen(buf));
}

您已在此循环之前编写了一个数据包,然后在第一次迭代时读取了将被读入缓冲区的可用数据.您每次需要将memset的缓冲区设置为零,或者使用对read的调用的返回值中给出的读取字节计数将零终止您的缓冲区.然后,您将无限循环,每次再次读取-但随后的读取将不会再复制任何数据,因为没有要读取的数据.但是,通过调用read可以使缓冲区保持不变,因此,每次迭代时打印输出都保持不变,因为每次迭代时缓冲区都保持不变.

You have written a packet prior to this loop, then on the first iteration you read the available data which gets read into your buffer. You need to either memset your buffer to zeros each time OR zero terminate your buffer using the read byte count given in the return value of your call to read. You then loop infinitely, each time reading again - but subsequent reads will not copy any more data as there is none to read. However, your buffer remains unchanged by the call to read therefore and your printed output remains the same every iteration as the buffer remains the same every iteration.

关于阻塞方面,您应该阅读以下指南(之前曾在SO上被推荐,并且非常适合作为串行端口编程的介绍)

As for the blocking aspect, you should read the following guide (which has been recommended on SO before and is very good as an introduction to serial port programming)

http://www.easysw.com/~mike/serial/serial. html

本节介绍将VMIN和VTIME设置为各种值时得到的行为.特别是最后一段解释了您看到的阻止行为.

This section describes the behaviour you get when setting VMIN and VTIME to various values. In particular the last paragraph explains the blocking behaviour you see.

VMIN指定要读取的最小字符数.如果已设置 设置为0,则VTIME值指定每个 字符读取.请注意,这并不意味着对N的读取调用 个字节将等待N个字符进入.相反,超时将 应用于第一个字符,read调用将返回该数字 立即可用的字符数(取决于您请求的字符数).

VMIN specifies the minimum number of characters to read. If it is set to 0, then the VTIME value specifies the time to wait for every character read. Note that this does not mean that a read call for N bytes will wait for N characters to come in. Rather, the timeout will apply to the first character and the read call will return the number of characters immediately available (up to the number you request).

如果VMIN不为零,则VTIME指定等待第一个时间 字符读取.如果在给定时间内读取了字符,则任何读取 将阻塞(等待),直到读取所有VMIN字符.即一次 读取第一个字符后,串行接口驱动程序期望 接收整个字符包(总共VMIN个字节).如果不 在允许的时间内读取字符,然后调用读取 返回0.此方法使您可以告诉所需的串行驱动程序 正好N个字节,任何读取调用都将返回0或N个字节.然而, 超时仅适用于读取的第一个字符,因此对于某些 原因是驱动程序错过了N字节数据包中的一个字符,然后 读取调用可能会永远阻止您等待其他输入 字符.

If VMIN is non-zero, VTIME specifies the time to wait for the first character read. If a character is read within the time given, any read will block (wait) until all VMIN characters are read. That is, once the first character is read, the serial interface driver expects to receive an entire packet of characters (VMIN bytes total). If no character is read within the time allowed, then the call to read returns 0. This method allows you to tell the serial driver you need exactly N bytes and any read call will return 0 or N bytes. However, the timeout only applies to the first character read, so if for some reason the driver misses one character inside the N byte packet then the read call could block forever waiting for additional input characters.

VTIME指定等待输入字符的时间 十分之一秒.如果VTIME设置为0(默认值),将读取 无限期地阻塞(等待),除非在端口上设置了NDELAY选项 带有open或fcntl.

VTIME specifies the amount of time to wait for incoming characters in tenths of seconds. If VTIME is set to 0 (the default), reads will block (wait) indefinitely unless the NDELAY option is set on the port with open or fcntl.

这篇关于从串口无限循环接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆