当前位置:网站首页>Raspberry pie serial port
Raspberry pie serial port
2022-07-28 07:08:00 【The most beautiful wish must be the craziest】
Serial port usage routines and instructions :http://elinux.org/Serial_port_programming
http://elinux.org/RPi_Serial_Connection
Pyserial API https://pyserial.readthedocs.io/en/latest/pyserial_api.html
sudo apt-get install python-serial
python -m serial.tools.list_ports This command can view the file node of the device .
class serial.Serial
__init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None)
port: Read or write port
baudrate: Baud rate
bytesize: Byte size
parity: Check bit
stopbits: Stop bit
timeout: Read timeout settings
writeTimeout: Write timeout
xonxoff: Software flow control
rtscts: Hardware flow control
dsrdtr: Hardware flow control
interCharTimeout: Character interval timeout
timeout And read():
timeout = None Have been waiting for , Until the data is received
timeout = 0: Non blocking
timeout = x: The waiting time
write() Default block , Unless write_timeout Set up .
read(size=1): Read from serial port size Bytes . Returns the data read . If a timeout is specified , It is possible to return fewer bytes after timeout ; If no timeout is specified , Will wait until the specified number of bytes is received .
write(data): send out data, And return the number of bytes sent . If bytes and bytearray You can use (python 2.6 above ), Then accept it as a parameter ; Otherwise, accept str As a parameter .
abnormal :SerialTimeoutException—— This exception occurs when write timeout is configured and timeout occurs .
inWaiting(): Returns the number of bytes in the receive cache
routine :
#!/usr/bin/env python
import serial
import time
def uart(port):
rv_buf=""
ch=''
while True:
ch=port.read();
rv_buf+=ch
if ch=='' or ch=='\r':
return rv_buf
send_buf=""
port=serial.Serial("/dev/ttyUSB0",baudrate=115200,timeout=None)
while True:
send_buf=uart(port)
port.write(send_buf)
print(send_buf)Accept data through serial port , We have successfully debugged the simple acceptance of single channel . But in the process of practical application , Multiple channels are required . So the current idea is to pass select To achieve .
First of all, let's simply learn python Medium select.
Python Of select() Method to call the IO Interface , It monitors sockets,open files, and pipes( All take fileno() Method ) When to become readable and writeable, Or communication errors .
Select Is very simple to use ,select( iwtd, owtd, ewtd[, timeout]) The first three parameters are ‘ Wait for the person ’. These three parameters are waiting for input in sequence 、 Output and abnormal state . Empty arrays are allowed timeout Parameter accepts the seconds represented by floating-point numbers . If omitted timeout Will block until at least one file descriptor is ready .timeout by 0 It means never blocking . Here is the blocking operation in the serial port receiving process , It's been blocking , Waiting for data , Prevent data loss .
The return value contains the prepared objects in the first three parameters 3 A list . If it has timed out but no object is ready , Then the return 3 An empty list
#!/usr/bin/env python
import select
import serial
import time
import multiprocessing
def uart(port):
rv_buf=""
ch=''
while True:
ch=port.read()
rv_buf+=ch
if ch=='' or ch=='\r':
return rv_buf
def uart_trans():
send_buf0=""
send_buf1=""
i=0
port0=serial.Serial("/dev/myuart0",baudrate=115200,timeout=0)
port1=serial.Serial("/dev/ttyUSB1",baudrate=115200,timeout=0)
fd0=file("/dev/myuart0","r+")
fd1=file("/dev/ttyUSB1","r+")
while True:
rlist,wlist,elist=select.select([fd0,fd1],[],[],)
for fd in rlist:
if(fd==fd0):
send_buf0=uart(port0)
port0.write(send_buf0)
if(fd==fd1):
send_buf1=uart(port1)
port1.write(send_buf1)
if __name__ =="__main__":
uart_trans()Test source :https://github.com/IJustLoveMyself/Raspberry-Pi/blob/master/test1/uart.py
https://github.com/IJustLoveMyself/Raspberry-Pi/blob/master/test1/select.py
边栏推荐
- shell---sed语句练习
- VLAN的配置
- MOOC翁恺C语言 第四周:进一步的判断与循环:1.逻辑类型与运算2.级联和嵌套的判断
- Shell script -- program conditional statements (conditional tests, if statements, case branch statements, echo usage, for loops, while loops)
- Icc2 analysis timing artifact analyze_ design_ violations
- Uniapp monitor whether the app has a network connection
- MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类
- 一个定时任务提醒工具
- shell---函数
- metasploit渗透ms7_010练习
猜你喜欢

Result fill in the blank (dfs*c language)

shell---函数

爬虫学习总结

RAID disk array

MOOC Weng Kai C language week 6: arrays and functions: 1. Arrays 2. Definition and use of functions 3. Parameters and variables of functions 4. Two dimensional arrays

freemarker合并单元格,if、else标签的使用,null、空字符串处理

Principle and configuration of NAT and pat

组管理和权限管理

DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling

MOOC翁恺C语言第八周:指针与字符串:1.指针2.字符类型3.字符串4.字符串计算
随机推荐
MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组
MOOC Weng Kai C language week 8: pointer and string: 1. Pointer 2. Character type 3. String 4. String calculation
Results fill in the blank. How many days of national day are Sundays (solved by pure Excel)
Custom component -- data listener
MOOC翁恺 C语言 第三周:判断与循环:1.判断
MOOC Weng Kai C language fourth week: further judgment and circulation: 1. Logical types and operations 2. Judgment of cascading and nesting
Result fill in the blank (dfs*c language)
anaconda3无法打开navigator的解决办法
joern运行后.joernindex数据库无内容
Ubuntu MySQL setting remote access permissions
Joern's code uses -devign
PXE无人值守安装管理
Esxi arm edition version 1.10 update
MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类
shell---条件语句练习
codesensor:将代码转化为ast后再转化为文本向量
Implementation method of converting ast into word vector before converting word vector
DOM -- page rendering, style attribute operation, preloading and lazy loading, anti shake and throttling
VLAN configuration
DHCP服务