當前位置:首頁 » 操作系統 » linux串口讀

linux串口讀

發布時間: 2024-03-10 13:44:01

linux怎麼讀取串口數據

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#include<errno.h>

#defineFALSE-1
#defineTRUE0

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,};
voidset_speed(intfd,intspeed){
inti;
intstatus;
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(&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 ");return(FALSE);
}
switch(parity)
{
case'n':
case'N':
options.c_cflag&=~PARENB;/*Clearparityenable*/
options.c_iflag&=~INPCK;/*Enableparitychecking*/
break;
case'o':
case'O':
options.c_cflag|=(PARODD|PARENB);
options.c_iflag|=INPCK;/*Disnableparitychecking*/
break;
case'e':
case'E':
options.c_cflag|=PARENB;/*Enableparity*/
options.c_cflag&=~PARODD;
options.c_iflag|=INPCK;/*Disnableparitychecking*/
break;
case'S':
case's':/*asnoparity*/
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;break;
default:
fprintf(stderr,"Unsupportedparity ");
return(FALSE);
}

switch(stopbits)
{
case1:
options.c_cflag&=~CSTOPB;
break;
case2:
options.c_cflag|=CSTOPB;
break;
default:
fprintf(stderr,"Unsupportedstopbits ");
return(FALSE);
}
/*Setinputparityoption*/
if(parity!='n')
options.c_iflag|=INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME]=150;
options.c_cc[VMIN]=0;/*UpdatetheoptionsanddoitNOW*/
if(tcsetattr(fd,TCSANOW,&options)!=0)
{
perror("SetupSerial3");
return(FALSE);
}
return(TRUE);
}

intmain()
{
printf("Thisprogramupdateslasttimeat%s%s ",__TIME__,__DATE__);
printf("STDIOCOM1 ");
intfd;
fd=open("/dev/ttyS0",O_RDWR);
if(fd==-1)
{
perror("serialporterror ");
}
else
{
printf("open");
printf("%s",ttyname(fd));
printf("succesfully ");
}

set_speed(fd,115200);
if(set_Parity(fd,8,1,'N')==FALSE){
printf("SetParityError ");
exit(0);
}
charbuf[]="fe55aa07bc010203040506073d";
write(fd,&buf,26);
charbuff[512];
intnread;
while(1)
{
if((nread=read(fd,buff,512))>0)
{
printf(" Len:%d ",nread);
buff[nread+1]='';
printf("%s",buff);
}
}
close(fd);
return0;
}

㈡ Linux普通用戶運行串口

將USB串口設備插入USB口後,會在/dev/目錄下生成/dev/ttyUSB0文件(也可能為/dev/ttyUSB1,/dev/ttyUSB2...),
查看此文件

輸出為:

c說明表明設備為字元設備文件(d表示目錄文件,-表示普通文件,l表示鏈接文件,b表示塊文件),
其中rw-rw----表示root用戶作為文件所有者可以讀和寫,dialout用戶組內的用戶可以讀和寫,其他用戶不允許讀、寫和執行(r表示可讀,w表示可寫,x表示可執行)
因此,需要將當前用戶增加到dialout用戶組中

熱點內容
安卓手機攝像頭連接雲存儲 發布:2025-05-11 05:10:52 瀏覽:34
瑞虎三都有哪些配置 發布:2025-05-11 05:05:08 瀏覽:950
mc非正版伺服器怎麼做 發布:2025-05-11 05:04:54 瀏覽:308
安卓手機九宮格忘記密碼怎麼解 發布:2025-05-11 05:00:30 瀏覽:595
安卓手機拼多多怎麼解綁銀行卡 發布:2025-05-11 05:00:25 瀏覽:686
校園網可以搭建伺服器地址 發布:2025-05-11 04:54:40 瀏覽:785
noip演算法 發布:2025-05-11 04:53:51 瀏覽:50
有什麼我的世界伺服器啟動器 發布:2025-05-11 04:50:41 瀏覽:296
寫shell腳本 發布:2025-05-11 04:37:41 瀏覽:935
電腦伺服器打開有什麼用 發布:2025-05-11 04:36:49 瀏覽:98