微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux从UART读取数据

我想从UART读取数据,我遵循这个教程 ,写function按预期工作,但是我得到了阅读function的问题:

这是uart_init函数

void uart_init() { printf("n +----------------------------------+"); printf("n | Serial Port Write |"); printf("n +----------------------------------+"); /*------------------------------- opening the Serial Port -------------------------------*/ fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY| O_SYNC); /* !!blocks the read */ /* O_RDWR Read/Write access to serial port */ /* O_NOCTTY - No terminal will control the process */ /* O_NDELAY -Non Blocking Mode,Does not care about- */ /* -the status of DCD line,open() returns immediatly */ if(fd == -1) /* Error Checking */ printf("n Error! in opening ttyUSB0 "); else printf("n ttyUSB0 Opened Successfully "); /*---------- Setting the Attributes of the serial port using termios structure --------- */ struct termios SerialPortSettings; /* Create the structure */ tcgetattr(fd,&SerialPortSettings); /* Get the current attributes of the Serial port */ cfsetispeed(&SerialPortSettings,B19200); /* Set Read Speed as 19200 */ cfsetospeed(&SerialPortSettings,B19200); /* Set Write Speed as 19200 */ SerialPortSettings.c_cflag &= ~PARENB; /* disables the Parity Enable bit(PARENB),So No Parity */ SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */ SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */ SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */ SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */ SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */ SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* disable XON/XOFF flow control both i/p and o/p */ SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */ SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/ /* Setting Time outs */ SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */ SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */ if((tcsetattr(fd,TCSANow,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/ printf("n ERROR ! in Setting attributes"); else printf("n Baudrate = 19200 n StopBits = 1 n Parity = none"); }

接收function:

void uart_receive() { char read_buffer[32]; /* Buffer to store the data received */ int bytes_read = 0; /* Number of bytes read by the read() system call */ int i = 0; bytes_read = read(fd,&read_buffer,10); /* Read the data */ printf("nn Bytes Rxed %d",bytes_read); /* Print the number of bytes read */ printf("nn "); for(i=0;i<bytes_read;i++) /*printing only the received characters*/ printf("%c",read_buffer[i]); printf("n +----------------------------------+nnn"); }

主要function:

linux:stdin导致select返回,当我不想要它

如何在Windows上正确configurationnetbeans 6.7和c + +?

另外的10键键盘input

检查pthread_mutex是否被初始化

在控制台应用程序中挣扎着一个空的Windows消息队列

void main(void) { uart_init(); /*------------------------------- Write data to serial port -----------------------------*/ //uart_write_commande(write_buffer); //Write function works well uart_receive(); close(fd);/* Close the Serial port */ }

编辑

我执行程序并等待数据字节在UART中被接收,我使用UART发送数据,但读取function保持阻塞状态。

我使用的虚拟机与Ubunutu 14.04,并且我不确定使用仿真的UART会在接收过程中产生问题。

Winsock错误代码10014

我如何在多个监视器设置中testing我的Windows应用程序?

如何通过Windows身份validation

使用System.Diagnostics.processstartinfo创build的cmd中的环境variables

UDP recvfrom不工作

你的程序挂在read()系统调用中,因为它被阻塞等待行终止字符。

您尝试使用该语句将端口配置为非规范模式

SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode

但该操作在错误的termios元素上。

ICANON属性lflag元素的一部分(而不是iflag )。 (这个错误来自你引用的教程!)

因此,您的程序正在执行阻止规范读取。

一个方便的termios函数来配置非规范模式:

cfmakeraw() sets the terminal to something like the "raw" mode of the old Version 7 terminal driver: input is available character by character,echoing is disabled,and all special processing of terminal input and output characters is disabled. The terminal attributes are set as follows: termios_p->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); termios_p->c_oflag &= ~OPOST; termios_p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); termios_p->c_cflag &= ~(CSIZE | PARENB); termios_p->c_cflag |= CS8;

阅读功能错误

bytes_read = read(fd,10); /* Read the data

应该

bytes_read = read(fd,read_buffer,10); /* Read the data

你的read()函数可能被阻塞,无论出于何种原因。 这里是关于从串口读取的讨论 ,包括阻塞/解锁设置的代码

也有可能没有数据被传输,导致没有被读取。 如果不能访问硬件设置,没有关于所看到的具体信息就很难走得更远。

另外,除了正确地传递read_buffer(另一个答案)之外,至少还有两件事情可以改进:

1)使用前检查read的返回:

bytes_read = read(fd,10); /* Read the data*/ if(bytes_read > 0) { ... }

2)更改:

for(i=0;i<bytes_read;i++) /*printing only the received characters*/ printf("%c",read_buffer[i]);

至:

//after successful read: read_buffer[bytes_read]=0;//place null termination after last character read. printf("%s",read_buffer);//note format specifier

这将打印读取的字符数,没有循环。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐