LINUX环境串口通讯案例源代码参考Word文件下载.docx
- 文档编号:22164509
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:12
- 大小:16.25KB
LINUX环境串口通讯案例源代码参考Word文件下载.docx
《LINUX环境串口通讯案例源代码参考Word文件下载.docx》由会员分享,可在线阅读,更多相关《LINUX环境串口通讯案例源代码参考Word文件下载.docx(12页珍藏版)》请在冰豆网上搜索。
while(bytes_left>
0)
{
written_bytes=write(fd,ptr,bytes_left);
if(written_bytes<
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<
n;
i++){
temp=temp+data[i];
temp=(~temp)+0x01;
check[0]=((temp>
>
8)&
0xff);
check[1]=(temp&
voidlchecksum(unsignedcharlenid,char*lenth){//16位crc校验*(lenth+3)=lenid&
0x0f;
*(lenth+2)=(lenid>
4)&
*(lenth+1)=(lenid>
*lenth=(~(((*(lenth+3))+(*(lenth+2))+(*(lenth+1)))%16)+1)&
//定义内核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<
sizeof(speed_arr)/sizeof(int);
i++)
if(speed==name_arr[i])
tcflush(fd,TCIOFLUSH);
cfsetispeed(&
Opt,speed_arr[i]);
cfsetospeed(&
status=tcsetattr(fd,TCSANOW,&
if(status!
=0)
perror("
tcsetattrfd1"
);
return;
}
tcflush(fd,TCIOFLUSH);
//定义其他串口参数
intset_Parity(intfd,intdatabits,intstopbits,intparity)
structtermiosoptions;
if(tcgetattr(fd,&
options)!
perror("
SetupSerial1"
return(FALSE);
options.c_cflag&
=~CSIZE;
switch(databits)
case7:
options.c_cflag|=CS7;
break;
case8:
options.c_cflag|=CS8;
default:
fprintf(stderr,"
Unsupporteddatasize\n"
return(FALSE);
switch(parity)
{
case'
n'
:
case'
N'
options.c_cflag&
=~PARENB;
options.c_iflag&
=~INPCK;
break;
o'
O'
options.c_cflag|=(PARODD|PARENB);
options.c_iflag|=INPCK;
e'
E'
options.c_cflag|=PARENB;
=~PARODD;
S'
s'
=~CSTOPB;
default:
fprintf(stderr,"
Unsupportedparity\n"
return(FALSE);
switch(stopbits)
case1:
options.c_cflag&
case2:
options.c_cflag|=CSTOPB;
Unsupportedstopbits\n"
if(parity!
='
)
{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;
if(tcsetattr(fd,TCSANOW,&
options)!
perror("
SetupSerial3"
return(TRUE);
//打开串口
intOpenDev(char*Dev)
intfd=open(Dev,O_RDWR);
if(-1==fd)
Can'
tOpenSerialPort"
return-1;
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>
set_speed(fd,4800);
printf("
tOpenSerialPort!
\n"
exit
(1);
if(set_Parity(fd,8,1,'
)==FALSE)
SetParityError\n"
while
(1)
{
n_read=0;
recvlen=0;
480081N\n"
bzero(recvbuf,sizeof(recvbuf));
bzero(recvdata,sizeof(recvdata));
FD_ZERO(&
readfds);
FD_SET(fd,&
//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)
noreaddata\n"
break;
for(k=0;
k<
n_read;
k++)
//recvdata[recvlen+k]=recvbuf[k];
memcpy(recvdata+recvlen+k,recvbuf+k,97);
recvlen=recvlen+n_read;
if(recvlen<
97)
continue;
}
break;
recvdata="
for(tt=0;
tt<
97;
tt++)
%02X"
recvdata[tt]);
moniliangshoudao\n"
//sprintf(a,"
AC13AA0000000000000000000096000102030000640064006400640084002C002C002C0084002C002C002C0064006400640064007F00200034002B007F00200034002B0000000000000000000101000000020005000F0018001400130013001300DB00DA00D900E600E400E1034D01A901A479FF1881"
sprintf(a,"
AC13AA0000000000000000000096000102030000640064006400640084002C002C002C0084002C002C002C0064006400640064007F00200034002B007F00200034002B0000000000000000000101ffffffff0005000F0018001400130013001300DB00DA00D900E600E400E1034D01A901A479FF1881"
118;
i++)
if((p=strstr(a,"
"
))!
=NULL)
*p='
\0'
;
sprintf(b[i],"
%s"
a);
sendata[i]=strtol(b[i],NULL,16);
p+1);
my_write(fd,sendata,118);
senddata="
sendata[tt]);
TheReplyhassended\n"
return0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LINUX 环境 串口 通讯 案例 源代码 参考