樹莓派 Linux串列埠程式設計 實現接收

2021-09-11 03:53:38 字數 1692 閱讀 2136

recv.h

#ifndef _recv_h

#define _recv_h

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define baudrate b115200

///baud rate : 115200

#define device "/dev/ttyama0"

#define size 1024

#endif

recv.c

#include

"recv.h"

int nfd =0;

struct termios stnew;

struct termios stold;

//open port & set port

intserialinit()

if((fcntl

(nfd, f_setfl,0)

)<0)

if(tcgetattr

(nfd,

&stold)!=0

) stnew = stold;

cfmakeraw

(&stnew)

;//將終端設定為原始模式,該模式下全部的輸入資料以位元組為單位被處理

//set speed

cfsetispeed

(&stnew, baudrate)

;//115200

cfsetospeed

(&stnew, baudrate)

;//set databits

stnew.c_cflag |

=(clocal|cread)

; stnew.c_cflag &

=~csize;

stnew.c_cflag |

= cs8;

//set parity

stnew.c_cflag &

=~parenb;

stnew.c_iflag &

=~inpck;

//set stopbits

stnew.c_cflag &

=~cstopb;

stnew.c_cc[vtime]=0

;//指定所要讀取字元的最小數量

stnew.c_cc[vmin]=1

;//指定讀取第乙個字元的等待時間,時間的單位為n*100ms

//假設設定vtime=0,則無字元輸入時read()操作無限期的堵塞

tcflush

(nfd,tciflush)

;//清空終端未完畢的輸入/輸出請求及資料。if(

tcsetattr

(nfd,tcsanow,

&stnew)!=0

)return nfd;

}int

main

(int argc,

char

**ar**)

bzero

(buf, size)

;while(1

)if(0

< nret)

}close

(nfd)

;return0;

}

參考:

樹莓派串列埠程式設計

初次使用樹莓派串列埠程式設計,需要配置。修改 cmdline.txt檔案 cd boot sudo vim cmdline.txt 刪除 之間的部分 dwc otg.lpm enable 0 console ttyama0,115200 kgdboc ttyama0,115200 console t...

樹莓派串列埠

串列埠使用例程及說明 pyserial api sudo apt get install python serial python m serial.tools.list ports 此命令可以檢視裝置的檔案節點。class serial.serial init port none,baudrate...

樹莓派串列埠通訊python 樹莓派串列埠通訊設定

實驗環境樹莓派 3b 開發板 2018 06 27 raspbian stretch 樹莓派作業系統 使用 windows 10 通過網線連線遠端登陸訪問方式控制樹莓派 實驗目的 為了將樹莓派構建成乙個智慧型家居的資料中心,我們需要在樹莓派上連線 zigbee 無線通訊模組,實現與感測器的一對多通訊...