LINUX环境,串口通讯案例源代码参考

LINUX环境,串口通讯案例源代码参考

ID:47725040

大小:54.50 KB

页数:15页

时间:2019-11-06

LINUX环境,串口通讯案例源代码参考_第1页
LINUX环境,串口通讯案例源代码参考_第2页
LINUX环境,串口通讯案例源代码参考_第3页
LINUX环境,串口通讯案例源代码参考_第4页
LINUX环境,串口通讯案例源代码参考_第5页
资源描述:

《LINUX环境,串口通讯案例源代码参考》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、标准文案LINUX环境,串口通讯案例源代码参考#include#include#include#include#include#include#include#include#include#include#include#include#include#defineFALSE-1#defineTRUE1

2、intmy_write(intfd,void*buffer,intlength){intbytes_left;intwritten_bytes;大全标准文案char*ptr;ptr=buffer;bytes_left=length;while(bytes_left>0){written_bytes=write(fd,ptr,bytes_left);if(written_bytes<0){if(errno==EINTR)written_bytes=0;elsereturn(-1);}bytes_left-=written_bytes;ptr+=written_bytes;}re

3、turn(0);}//16位crc校验voidchecksum(unsignedchar*data,unsignedchar*check,intn){大全标准文案longtemp=0;inti;for(i=0;i>8)&0xff);check[1]=(temp&0xff);}voidlchecksum(unsignedcharlenid,char*lenth){//16位crc校验*(lenth+3)=lenid&0x0f;*(lenth+2)=(leni

4、d>>4)&0x0f;*(lenth+1)=(lenid>>8)&0x0f;*lenth=(~(((*(lenth+3))+(*(lenth+2))+(*(lenth+1)))%16)+1)&0x0f;}//定义内核bott常量对照表大全标准文案intspeed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,B38400,B19200,B9600,B4800,B2400,B1200,B300,};intname_arr[]={38400,19200,9600,4800,2400,1200,300,38400,19200,9

5、600,4800,2400,1200,300,};//定义bott率voidset_speed(intfd,intspeed){inti,status;structtermiosOpt;tcgetattr(fd,&Opt);for(i=0;i

6、,&Opt);if(status!=0)大全标准文案perror("tcsetattrfd1");return;}tcflush(fd,TCIOFLUSH);}}//定义其他串口参数intset_Parity(intfd,intdatabits,intstopbits,intparity){structtermiosoptions;if(tcgetattr(fd,&options)!=0){perror("SetupSerial1");return(FALSE);}options.c_cflag&=~CSIZE;switch(databits){case7:options.c

7、_cflag

8、=CS7;break;大全标准文案case8:options.c_cflag

9、=CS8;break;default:fprintf(stderr,"Unsupporteddatasize");return(FALSE);}switch(parity){case'n':case'N':options.c_cflag&=~PARENB;options.c_iflag&=~INPCK;break;case'o':case'O':options.c_cflag

10、=(PARODD

11、PARENB)

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。