LINUX环境,串口通讯案例源代码参考.doc_第1页
LINUX环境,串口通讯案例源代码参考.doc_第2页
LINUX环境,串口通讯案例源代码参考.doc_第3页
LINUX环境,串口通讯案例源代码参考.doc_第4页
LINUX环境,串口通讯案例源代码参考.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

LINUX环境,串口通讯案例源代码参考#include #include #include #include #include #include #include #include #include #include #include #include #include #define FALSE -1#define TRUE 1int my_write(int fd,void *buffer,int length) int bytes_left; int written_bytes; char *ptr; ptr=buffer; bytes_left=length; while(bytes_left0) written_bytes=write(fd,ptr,bytes_left); if(written_bytes0) if(errno=EINTR) written_bytes=0; elsereturn(-1); bytes_left-=written_bytes; ptr+=written_bytes; return(0);/ 16位crc校验void checksum(unsigned char *data,unsigned char *check,int n) long temp=0; int i; for(i=0;i8)&0xff); check1=(temp&0xff);void lchecksum(unsigned char lenid,char *lenth) / 16位crc校验 *(lenth+3)=lenid&0x0f; *(lenth+2)=(lenid4)&0x0f; *(lenth+1)=(lenid8)&0x0f; *lenth=(*(lenth+3)+(*(lenth+2)+(*(lenth+1)%16)+1)&0x0f;/定义内核bott常量对照表int speed_arr = B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, ;int name_arr = 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, ;/定义bott率void set_speed(int fd, int speed) int i,status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i 0) set_speed(fd,4800); else printf(Cant Open Serial Port!n); exit(1); if (set_Parity(fd,8,1,N)= FALSE) printf(Set Parity Errorn); exit(1); while(1) n_read = 0; recvlen = 0; printf(4800 8 1 Nn); bzero(recvbuf,sizeof(recvbuf); bzero(recvdata,sizeof(recvdata); FD_ZERO(&readfds); FD_SET(fd,&readfds); while(1) /timeout.tv_sec=1; /timeout.tv_usec=0; /ret=select(keyboard+1,&readfd,NULL,NULL,&timeout); /tv.tv_sec=10; /tv.tv_usec=0; /if(select(fd+1,&readfds,NULL,NULL,&tv)0)/被动的等待数据,直到超出tv设置 if(select(fd+1,&readfds,NULL,NULL,NULL)0)/被动的等待数据,没有数据时一直等待 if(FD_ISSET(fd,&readfds) if(n_read =read(fd,recvbuf,1024)=-1) printf(no read data n); break; else for(k=0;kn_read;k+) /recvdatarecvlen+k=recvbufk; memcpy(recvdata+recvlen+k,recvbuf+k,97); recvlen=recvlen+n_read; if(recvlen97) continue; break;break; break; printf(recvdata=); for(tt=0;tt97;tt+) printf(%02X ,recvdatatt); printf(n); printf(moniliang shoudaon); /sprintf(a,AC 13 AA 00 00 00 00 00 00 00 00 00 00 96 00 01 02 03 00 00 64 00 64 00 64 00 64 00 84 00 2C 00 2C 00 2C 00 84 00 2C 00 2C 00 2C 00 64 00 64 00 64 00 64 00 7F 00 20 00 34 00 2B 00 7F 00 20 00 34 00 2B 00 00 00 00 00 00 00 00 00 01 01 00 00 00 02 00 05 00 0F 00 18 00 14 00 13 00 13 00 13 00 DB 00 DA 00 D9 00 E6 00 E4 00 E1 03 4D 01 A9 01 A4 79 FF 18 81); sprintf(a,AC 13 AA 00 00 00 00 00 00 00 00 00 00 96 00 01 02 03 00 00 64 00 64 00 64 00 64 00 84 00 2C 00 2C 00 2C 00 84 00 2C 00 2C 00 2C 00 64 00 64 00 64 00 64 00 7F 00 20 00 34 00 2B 00 7F 00 20 00 34 00 2B 00 00 00 00 00 00 00 00 00 01 01 ff ff ff ff 00 05 00 0F 00 18 00 14 00 13 00 13 00 13 00 DB 00 DA 00 D9 00 E6 00 E4 00 E1 03 4D 01 A9 01 A4 79 FF 18 81); for(i=0;i118;i+) if(p=strstr(a, )!=NULL) *p=0; sprintf(bi,%s,a); sendatai=strtol(bi,NULL,16); sprintf(a,%s,p+1); else sprintf(bi,%s,a); sendat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论