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

linux串口獲取

發布時間: 2022-09-20 23:38:14

A. 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;
}

B. linux 串口讀取數據被截斷,怎樣一次全部接收

沒辦法保證能夠一次全部接收,只能自己緩存起來處理。比如,發送數據時候增加帶有長度的數據頭以及末尾的校驗,用 select 對串口做非阻塞讀取,讀到數據後檢查是否完整,不完整就接著讀,直到獲得完整數據再處理。

C. linux串口讀取問題

首先你確定你那串口是否有東西可讀? 就是你上面說的「一個文件不停的寫數據到串口」!你可以先不這樣讀取,你可以在終端上用cat試試是否有數據可讀:cat /dev/ttyS0

如果有的話,那你就檢查串口設置是否正確,如波特率,數據位,停止位,校驗位等!

最後就是你讀取的函數了,看看先不要用printf列印字元串了,先看看十六進制是否有,然後再看字元等!

就是以上一些,你還可以參考Linux下串口文檔,網路上很多的……

D. linux下,用什麼工具可以抓串口的數據

對於picocom, kermit, minicom, picocom 最簡單易用,也完全符合我的使用需求。
安裝(mint / ubuntu):
$ sudo apt-get install picocom
使用:
$ picocom -b 115200 /dev/ttyUSB0
(/dev/ttyUSB0 為串口設備文件,如果用的不是USB轉串口,則為 /dev/ttyS*)
(可以設置一個別名,如 alias pc='picocom -b 115200 /dev/ttyUSB0',這樣在終端輸入 sudo pc 就可以打開終端了)
退出:
Ctrl-a 是轉義鍵,按 Ctrl-a Ctrl-q 就可以退出終端。

E. 虛擬機下的linux怎麼接受主機串口接收的數據

虛擬機中的串口連接可以採用兩種方法。一種是指定虛擬機的串口連接到實際的COM上,例如開發機連接到COM1,目標機連接到COM2,然後把兩個串口通過串口線相連接。另一種更為簡便的方法是:在較高一些版本的VMware中都支持把串口映射到命名管道,把兩個虛擬機的串口映射到同一個命名管道。例如,在兩個虛擬機中都選定同一個命名管道 \\.\pipe\com_1,指定target機的COM口為server端,並選擇"The other end is a virtual machine"屬性;指定development機的COM口端為client端,同樣指定COM口的"The other end is a virtual machine"屬性。對於IO mode屬性,在target上選中"Yield CPU on poll"復選擇框,development機不選。

串口是開發者經常要使用到的,我們有些時候需要使用windows開發,然而window裝在了虛擬機中,我們怎麼樣在虛擬機中使用串口呢?
1、在virtualbox中選擇啟用串口,埠模式選擇Host device.
由於現有兩個系統但是只有一個物理串口,所以虛擬機要通過主機的串口文件間接獲取數據,主機是linux,串口設備為/dev/ttyS0
2、埠/文件位置一欄填入:/dev/ttyS0,啟動虛擬機。
3、如果無法啟動虛擬機,很有可能是沒有對ttyS0設備的讀寫許可權,(可以ls -l /dev/ttyS0查看以下許可權)於是我們要修改ttyS0的許可權,在終端中輸入:sudo chmod 777 /dev/ttyS0 這樣就將ttyS0的所有許可權都打開了。啟動虛擬機,這時候就可以在虛擬機中(winXP)使用串口了

F. 請問程序中linux如何獲取可用的串口列表

dmesg | grep tty

下面是一個例子,例子中採用的是USB轉串口線(pl2303)。

插入線:
$ dmesg|grep tty
[ 0.000000] console [tty0] enabled
[ 1.248404] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 1.624590] 00:07: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 12.618301] usb 5-2: pl2303 converter now attached to ttyUSB0

拔出線:
$ dmesg|grep tty
[ 0.000000] console [tty0] enabled
[ 1.248404] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 1.624590] 00:07: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[ 12.618301] usb 5-2: pl2303 converter now attached to ttyUSB0
[ 375.392303] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0

這樣程序中應該不難判斷了吧。
【參考】http://www.cyberciti.biz/faq/find-out-linux-serial-ports-with-setserial/

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:336
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:944
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:742
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:372