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

上传人:b****5 文档编号:11958602 上传时间:2023-04-16 格式:DOCX 页数:12 大小:16.25KB
下载 相关 举报
LINUX环境串口通讯案例源代码参考.docx_第1页
第1页 / 共12页
LINUX环境串口通讯案例源代码参考.docx_第2页
第2页 / 共12页
LINUX环境串口通讯案例源代码参考.docx_第3页
第3页 / 共12页
LINUX环境串口通讯案例源代码参考.docx_第4页
第4页 / 共12页
LINUX环境串口通讯案例源代码参考.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

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

《LINUX环境串口通讯案例源代码参考.docx》由会员分享,可在线阅读,更多相关《LINUX环境串口通讯案例源代码参考.docx(12页珍藏版)》请在冰豆网上搜索。

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

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

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

 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#defineFALSE-1

#defineTRUE1

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;

else

return(-1);

}

bytes_left-=written_bytes;

ptr+=written_bytes;

}

return(0);

}

//16位crc校验

voidchecksum(unsignedchar*data,unsignedchar*check,intn){

longtemp=0;

inti;

for(i=0;i

temp=temp+data[i];

}

temp=(~temp)+0x01;

check[0]=((temp>>8)&0xff);

check[1]=(temp&0xff);

}

voidlchecksum(unsignedcharlenid,char*lenth){//16位crc校验*(lenth+3)=lenid&0x0f;

*(lenth+2)=(lenid>>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,9600,4800,2400,1200,300,};

//定义bott率

voidset_speed(intfd,intspeed)

{

inti,status;

structtermiosOpt;

tcgetattr(fd,&Opt);

for(i=0;i

{

if(speed==name_arr[i])

{

tcflush(fd,TCIOFLUSH);

cfsetispeed(&Opt,speed_arr[i]);

cfsetospeed(&Opt,speed_arr[i]);

status=tcsetattr(fd,TCSANOW,&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_cflag|=CS7;

break;

case8:

options.c_cflag|=CS8;

break;

default:

fprintf(stderr,"Unsupporteddatasize\n");

return(FALSE);

}

switch(parity)

{

case'n':

case'N':

options.c_cflag&=~PARENB;

options.c_iflag&=~INPCK;

break;

case'o':

case'O':

options.c_cflag|=(PARODD|PARENB);

options.c_iflag|=INPCK;

break;

case'e':

case'E':

options.c_cflag|=PARENB;

options.c_cflag&=~PARODD;

options.c_iflag|=INPCK;

break;

case'S':

case's':

options.c_cflag&=~PARENB;

options.c_cflag&=~CSTOPB;

break;

default:

fprintf(stderr,"Unsupportedparity\n");

return(FALSE);

}

switch(stopbits)

{

case1:

options.c_cflag&=~CSTOPB;

break;

case2:

options.c_cflag|=CSTOPB;

break;

default:

fprintf(stderr,"Unsupportedstopbits\n");

return(FALSE);

}

if(parity!

='n')

{options.c_iflag|=INPCK;}

options.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|IGNCR|ICRNL|ISTRIP);

options.c_oflag|=OPOST;

options.c_oflag&=~(ONLCR|OCRNL);

options.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);

options.c_cc[VMIN]=1;

options.c_cc[VTIME]=100;

tcflush(fd,TCIOFLUSH);

if(tcsetattr(fd,TCSANOW,&options)!

=0)

{

perror("SetupSerial3");

return(FALSE);

}

return(TRUE);

}

//打开串口

intOpenDev(char*Dev)

{

intfd=open(Dev,O_RDWR);

if(-1==fd)

{

perror("Can'tOpenSerialPort");

return-1;

}

else

returnfd;

}

intmain()

{

chara[1024],b[1024][5];

char*p;

unsignedcharrecvbuf[1024];

unsignedcharrecvdata[1024],sendata[1024];

unsignedcharcomid[20];

intfd,n_read;

intrecvlen=0;

fd_setreadfds;

structtimevaltv;

intk,i,tt;

sprintf(comid,"/dev/ttyS4");

fd=OpenDev(comid);

if(fd>0)

{

set_speed(fd,4800);

}

else

{

printf("Can'tOpenSerialPort!

\n");

exit

(1);

}

if(set_Parity(fd,8,1,'N')==FALSE)

{

printf("SetParityError\n");

exit

(1);

}

while

(1)

{

n_read=0;

recvlen=0;

printf("480081N\n");

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("noreaddata\n");

break;

}

else

{

for(k=0;k

{

//recvdata[recvlen+k]=recvbuf[k];

memcpy(recvdata+recvlen+k,recvbuf+k,97);

}

recvlen=recvlen+n_read;

if(recvlen<97)

{

continue;

}

}

break;

}

break;

}

break;

}

printf("recvdata=");

for(tt=0;tt<97;tt++)

{

printf("%02X",recvdata[tt]);

}

printf("\n");

printf("moniliangshoudao\n");

//sprintf(a,"AC13AA0000000000000000000096000102030000640064006400640084002C002C002C0084002C002C002C0064006400640064007F00200034002B007F00200034002B0000000000000000000101000000020005000F0018001400130013001300DB00DA00D900E600E400E1034D01A901A479FF1881");

sprintf(a,"AC13AA0000000000000000000096000102030000640064006400640084002C002C002C0084002C002C002C0064006400640064007F00200034002B007F00200034002B0000000000000000000101ffffffff0005000F0018001400130013001300DB00DA00D900E600E400E1034D01A901A479FF1881");

for(i=0;i<118;i++)

{

if((p=strstr(a,""))!

=NULL)

{

*p='\0';

sprintf(b[i],"%s",a);

sendata[i]=strtol(b[i],NULL,16);

sprintf(a,"%s",p+1);

}

else

{

sprintf(b[i],"%s",a);

sendata[i]=strtol(b[i],NULL,16);

}

}

my_write(fd,sendata,118);

printf("senddata=");

for(tt=0;tt<118;tt++)

{

printf("%02X",sendata[tt]);

}

printf("TheReplyhassended\n");

}

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 营销活动策划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1