当前位置: 技术问答>linux和unix
为什么从串口读不出数据呢?(附源码)
来源: 互联网 发布时间:2016-05-07
本文导语: 下面的源码主要实现了读取串口中的数据, 其中open_port用于打开串口,set_opt用于设置串口。 在Linux用gcc编译(gcc -o ser ser.c)后.运行./ser出现如下结果: fcntl=0 isatty success fd-open=3 set done! fd=3; nread=0,hello 由以上结果...
下面的源码主要实现了读取串口中的数据,
其中open_port用于打开串口,set_opt用于设置串口。
在Linux用gcc编译(gcc -o ser ser.c)后.运行./ser出现如下结果:
fcntl=0
isatty success
fd-open=3
set done!
fd=3;
nread=0,hello
由以上结果可知串中已经打开并设置好了,可是为什么nread的返回值为0呢,
应该是8啊!
另外我的板子的波特率是57600的。
#include
#include
#include
#include
#include
#include
#include
#include
#include
int open_port(int fd,int comport);
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop);
int main(void)
{
int fd;
int nread,i;
char buff[]="hellon";
if ((fd=open_port(fd,1))options.c_oflag &= ~(OPOST);
//阻塞字节数和等待时间
tty->options.c_cc[VMIN] = 1;
tty->options.c_cc[VTIME] = 0;
serial_attr_update(tty);
/*设置串口读写为阻塞模式*/
fcntl(tty->fd,F_SETFL,0);
//非阻塞方式
//fcntl(tty->fd, F_SETFL, FNDELAY);
这是我的接收函数
int serial_recv(struct serial_t *tty,char *buf,int length)
{
int ret,left,bytes;
left = length;
while(left > 0)
{
ret = 0;
bytes = 0;
/*wait util have a key press*/
ret = read(tty->fd,buf,left);
if(ret > 0){
left -= ret;
if(length!=1) buf += ret;
}
/*usleep(100);*/
/* 非阻塞式读取按键,会消耗很大CPU。
ioctl(tty->fd, FIONREAD, &bytes);
if(bytes > 0){
ret = read(tty->fd,buf,left);
}
if(ret >0){
left -= ret;
if(length!=1)buf += ret;
}
//usleep(100);
*/
}
return length - left;
}
//只能一下读取刚好的数据,比如需立即读取8个字符,如果没有就返回错误
//没有阻塞的读取,就是没有数据来的话就要立即返回了,而不是等待数据的到来
int serial_recv_noblock(struct serial_t *tty, char *buf, int length)
{
int bytes;
ioctl(tty->fd, FIONREAD, &bytes);
if(bytes fd,buf,length);
}
其中open_port用于打开串口,set_opt用于设置串口。
在Linux用gcc编译(gcc -o ser ser.c)后.运行./ser出现如下结果:
fcntl=0
isatty success
fd-open=3
set done!
fd=3;
nread=0,hello
由以上结果可知串中已经打开并设置好了,可是为什么nread的返回值为0呢,
应该是8啊!
另外我的板子的波特率是57600的。
#include
#include
#include
#include
#include
#include
#include
#include
#include
int open_port(int fd,int comport);
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop);
int main(void)
{
int fd;
int nread,i;
char buff[]="hellon";
if ((fd=open_port(fd,1))options.c_oflag &= ~(OPOST);
//阻塞字节数和等待时间
tty->options.c_cc[VMIN] = 1;
tty->options.c_cc[VTIME] = 0;
serial_attr_update(tty);
/*设置串口读写为阻塞模式*/
fcntl(tty->fd,F_SETFL,0);
//非阻塞方式
//fcntl(tty->fd, F_SETFL, FNDELAY);
这是我的接收函数
int serial_recv(struct serial_t *tty,char *buf,int length)
{
int ret,left,bytes;
left = length;
while(left > 0)
{
ret = 0;
bytes = 0;
/*wait util have a key press*/
ret = read(tty->fd,buf,left);
if(ret > 0){
left -= ret;
if(length!=1) buf += ret;
}
/*usleep(100);*/
/* 非阻塞式读取按键,会消耗很大CPU。
ioctl(tty->fd, FIONREAD, &bytes);
if(bytes > 0){
ret = read(tty->fd,buf,left);
}
if(ret >0){
left -= ret;
if(length!=1)buf += ret;
}
//usleep(100);
*/
}
return length - left;
}
//只能一下读取刚好的数据,比如需立即读取8个字符,如果没有就返回错误
//没有阻塞的读取,就是没有数据来的话就要立即返回了,而不是等待数据的到来
int serial_recv_noblock(struct serial_t *tty, char *buf, int length)
{
int bytes;
ioctl(tty->fd, FIONREAD, &bytes);
if(bytes fd,buf,length);
}