求教串口编程????
我编写了一个串口程序,pc机上运行readtest.C,在arm上运行writetest.C,
//readtest.c
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <math.h>
int spfd;
int main()
{
char fname[16],hd[16],*rbuf;
int retv,i,ncount=0;
struct termios oldtio;
int realdata=0;
[b]spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NONBLOCK);
//spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY);if(spfd<0){
perror("open/dev/ttyS0"
return -1;
}
printf("spfd%d\n",spfd);
tcgetattr(spfd,&oldtio);
cfmakeraw(&oldtio);
cfsetispeed(&oldtio,B57600);
cfsetospeed(&oldtio,B57600);
tcsetattr(spfd,TCSANOW,&oldtio);
rbuf=hd;
printf("ready for receiving data...\n"
retv=read(spfd,rbuf,4);
if(retv==-1)
{
perror("read"
return -1;
}
/*
while(*rbuf!='\0')
{
ncount+=1;
rbuf++;
retv=read(spfd,rbuf,1);
printf("the number received is %d\n",retv);
if(retv==-1)
perror("read"
}
*/
printf("%d\n",retv);
printf("%s\n",rbuf);
//for(i=0;i<ncount;i++)
//realdata+=(hd-4*pow(10,ncount-i-1);
//printf("complete receiving the data %d\n",realdata);
close(spfd);
return 0;
}
//writetest.c
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <math.h>
int spfd;
int main()
{
char fname[16],*sbuf;
int retv,i,ncount=0;
struct termios oldtio;
int realdata=0;
spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY);
if(spfd<0)
{
perror("open/dev/ttyS0"
return -1;
}
tcgetattr(spfd,&oldtio);
cfmakeraw(&oldtio);
cfsetispeed(&oldtio,B57600);
cfsetospeed(&oldtio,B57600);
tcsetattr(spfd,TCSANOW,&oldtio);
printf("ready for sending data...\n"
fname[0]='1';
fname[1]='2';
fname[2]='3';
fname[3]='\0';
//sbuf=(char *)malloc(4);
//stncpy(sbuf,fname,4);
retv=write(spfd,fname,4);
if(retv==-1)
perror("write"
printf("the number sent is %d\n",retv);
close(spfd);
return 0;
}
spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NONBLOCK);这个就能运行
//spfd=open("/dev/ttyS0",O_RDWR|O_NOCTTY);这个就不能读串口
为什么??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论