当前位置: 技术问答>linux和unix
串口通信程序问题
来源: 互联网 发布时间:2015-09-18
本文导语: #include #include #include #include #include #include #include #include #include #include #include #i...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int spfd;
int main(int argc,char *argv[])
{
unsigned char send[4],rece[4],*sbuf;
int sfd,retv,i;
struct termios oldtio;
spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(spfd0)
{
printf("The receive data is %sn",rece);
printf("nLen %dn",retv);
}
}
close(spfd);
return 0;
}
这个程序是linux往单片机上发送数据,然后接收单片机返回的数据,运行之后,用示波器察看单片机确实收到了数据,而且单片机也发送了数据(用串口调试工具也同样能收到数据),但是这个程序就是没有收到数据,连printf语句都没有打印,不知道什么地方出错了,希望各位大虾指点,分不够可以再加!
谢谢!!!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int spfd;
int main(int argc,char *argv[])
{
unsigned char send[4],rece[4],*sbuf;
int sfd,retv,i;
struct termios oldtio;
spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(spfd0)
{
printf("The receive data is %sn",rece);
printf("nLen %dn",retv);
}
}
close(spfd);
return 0;
}
这个程序是linux往单片机上发送数据,然后接收单片机返回的数据,运行之后,用示波器察看单片机确实收到了数据,而且单片机也发送了数据(用串口调试工具也同样能收到数据),但是这个程序就是没有收到数据,连printf语句都没有打印,不知道什么地方出错了,希望各位大虾指点,分不够可以再加!
谢谢!!!
|
试试这段初始化串口的代码:
tcgetattr(fd,&t);
bzero(&t,sizeof(t));
fcntl(fd,F_SETFL,0);
t.c_cflag |= CLOCAL | CREAD;
t.c_cflag &= ~PARENB;
t.c_cflag &= ~CSTOPB;
t.c_cflag &= ~CSIZE;
t.c_cflag |= CS8;
t.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
t.c_oflag &= ~OPOST;
tcgetattr(fd,&t);
bzero(&t,sizeof(t));
fcntl(fd,F_SETFL,0);
t.c_cflag |= CLOCAL | CREAD;
t.c_cflag &= ~PARENB;
t.c_cflag &= ~CSTOPB;
t.c_cflag &= ~CSIZE;
t.c_cflag |= CS8;
t.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
t.c_oflag &= ~OPOST;
|
单步调试过没?
|
if((retv=read(spfd,rece,4))>0)
{
printf("The receive data is %sn",rece);
printf("nLen %dn",retv);
}
没printf 显然是read 失败了嘛
{
printf("The receive data is %sn",rece);
printf("nLen %dn",retv);
}
没printf 显然是read 失败了嘛