欢迎来到天天文库
浏览记录
ID:27587313
大小:115.96 KB
页数:7页
时间:2018-12-03
《linux下的串口通信程序详解》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、linux下的串门通信程序详解2009-07-1912:37为丫说明问题,卜面给出测试程序來理解lintix卜的串门操作流程,例程receive,c用来接收从串口发来的数据,而例程send,c用来发送数据到串口。二者成功建立串口连接后,串口接收端会收到串口发送端发来的字符串数据“Hello,thisisaSerialPorttest!”01.receive,c程序清单:1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.
2、31.32.33.34./********************************************************ilename:receive.c*Description:ReceivedatafromSerial—Port*Date:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★y/*********************头文件定义***********************拉include^include3、ng.h>#includettinclude#include^include^include#includettinclude"math•h"#definemax^buffer_size100/*定义缓冲区最大兜度*//***************■**★*********************★****★******★*****/intfd,s;intopen—serial(intk){if(k4、==0)/*串口选择*/{fd=open("/dev/ttyUSB0",O_RDWR5、O_MOCTTY};/*读写方式打开中口*/perror("open/dev/ttyUS30M);}elsefd=open(H/aev/ttySln,O_RDWRIO.KOCTTY);perror("open/dev/ttySln);}if(fd==-1)/*打开失败*/return-1;elsereturn0;37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.56、9.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.36./intmain(}{charhd[max—buffer—size],*rbuf;"记义接收缓冲区*/intflag一close,retv,i,ncount=0;structtermiosopt;intrealdata=0;open—serial(0};/*打开串IJ1*//****************************************************************7、***/tcgetattr(fd,&opt);cfmakeraw(&opt);cfsetispeed(&optF39600);/*波特率设罝为9600bps*/cfsetospeeci(&opt,39600);/it******************************************************************/tcsetattr(fd,TCSANOW,&opt);rbuf=hd;/*数据保存*/printf<"readyforreceivingdata.../nM);retv=read(fd8、,rbuf,1);/*接收数据*/if(retv==-l){perror(Hreadbadl");/*读状态标志判断*/},★女★★★★★★★★★★★★★★★★★★★★★★★:)'•女台★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★!while(*rbufl-Vn')/*判断数据是否接收完毕*/{ncount+=l;rbuf++;retv=read(fdzrbuf,1};if(retv==-l){perror("readbad2M);}}/it************************************9、******************************/printf(HThedatareceivedis:/n,f);/*输出接收到的数掘*/for(i=0;i{printf("%c",hd[i]};}printf(w/nw);f
3、ng.h>#includettinclude#include^include^include#includettinclude"math•h"#definemax^buffer_size100/*定义缓冲区最大兜度*//***************■**★*********************★****★******★*****/intfd,s;intopen—serial(intk){if(k
4、==0)/*串口选择*/{fd=open("/dev/ttyUSB0",O_RDWR
5、O_MOCTTY};/*读写方式打开中口*/perror("open/dev/ttyUS30M);}elsefd=open(H/aev/ttySln,O_RDWRIO.KOCTTY);perror("open/dev/ttySln);}if(fd==-1)/*打开失败*/return-1;elsereturn0;37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.5
6、9.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.36./intmain(}{charhd[max—buffer—size],*rbuf;"记义接收缓冲区*/intflag一close,retv,i,ncount=0;structtermiosopt;intrealdata=0;open—serial(0};/*打开串IJ1*//****************************************************************
7、***/tcgetattr(fd,&opt);cfmakeraw(&opt);cfsetispeed(&optF39600);/*波特率设罝为9600bps*/cfsetospeeci(&opt,39600);/it******************************************************************/tcsetattr(fd,TCSANOW,&opt);rbuf=hd;/*数据保存*/printf<"readyforreceivingdata.../nM);retv=read(fd
8、,rbuf,1);/*接收数据*/if(retv==-l){perror(Hreadbadl");/*读状态标志判断*/},★女★★★★★★★★★★★★★★★★★★★★★★★:)'•女台★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★!while(*rbufl-Vn')/*判断数据是否接收完毕*/{ncount+=l;rbuf++;retv=read(fdzrbuf,1};if(retv==-l){perror("readbad2M);}}/it************************************
9、******************************/printf(HThedatareceivedis:/n,f);/*输出接收到的数掘*/for(i=0;i{printf("%c",hd[i]};}printf(w/nw);f
此文档下载收益归作者所有