Linux使用python調(diào)用串口<Ubuntu>方式
要在 Ubuntu 上使用 /dev/ttyUSB0 設(shè)備編寫一個(gè)簡單的串口收發(fā)程序,你可以使用 Python,結(jié)合 pyserial 庫來實(shí)現(xiàn)。
這種方法相對(duì)簡單,適用于各種串行通信任務(wù)。
以下是如何在 Python 中編寫串口收發(fā)程序的步驟及代碼示例:
步驟 1: 安裝 PySerial
首先確保安裝了 pyserial,這是一個(gè)流行的 Python 庫,用于處理串行通信:
pip install pyserial
步驟 2: 編寫串口收發(fā)代碼
以下是一個(gè)簡單的 Python 腳本,用于打開 /dev/ttyUSB0 串口,配置波特率和其他參數(shù),然后接收和發(fā)送數(shù)據(jù)。
import serial
import time
def open_serial(port, baud_rate):
"""打開串口并配置基本參數(shù)"""
try:
ser = serial.Serial(port, baud_rate, timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
if ser.is_open:
print(f"Serial port {port} opened successfully")
return ser
except Exception as e:
print(f"Failed to open serial port: {e}")
return None
def read_from_serial(ser):
"""從串口讀取數(shù)據(jù)"""
try:
data = ser.readline() # 讀取一行數(shù)據(jù)
if data:
print(f"Received: {data.decode().strip()}")
except Exception as e:
print(f"Failed to read data: {e}")
def write_to_serial(ser, data):
"""向串口發(fā)送數(shù)據(jù)"""
try:
ser.write(data.encode())
print(f"Sent: {data}")
except Exception as e:
print(f"Failed to send data: {e}")
def main():
port = "/dev/ttyUSB0"
baud_rate = 9600
# 打開串口
ser = open_serial(port, baud_rate)
if ser and ser.is_open:
try:
# 循環(huán)接收和發(fā)送數(shù)據(jù)
while True:
read_from_serial(ser)
time.sleep(1)
write_to_serial(ser, "Hello from Python!")
time.sleep(1)
finally:
ser.close()
print("Serial port closed")
if __name__ == "__main__":
main()
程序說明
- 打開串口:
open_serial函數(shù)嘗試打開指定的串口并配置波特率等參數(shù)。 - 讀取數(shù)據(jù):
read_from_serial函數(shù)從串口讀取一行數(shù)據(jù),并將其解碼并打印。 - 發(fā)送數(shù)據(jù):
write_to_serial函數(shù)向串口發(fā)送字符串。 - 主循環(huán):
main函數(shù)中的循環(huán)演示了如何連續(xù)讀取和發(fā)送數(shù)據(jù)。
注意事項(xiàng)
- 確保你有足夠的權(quán)限訪問
/dev/ttyUSB0。如果沒有,你可能需要使用sudo來運(yùn)行你的腳本,或?qū)⒂脩籼砑拥?dialout組。 - 波特率和其他串口參數(shù)應(yīng)該與你要通信的設(shè)備相匹配。
這個(gè)簡單的示例提供了使用 Python 和 PySerial 進(jìn)行串口通信的基礎(chǔ)。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas使用stack和pivot實(shí)現(xiàn)數(shù)據(jù)透視的方法
筆者最近正在學(xué)習(xí)Pandas數(shù)據(jù)分析,將自己的學(xué)習(xí)筆記做成一套系列文章。本節(jié)主要記錄Pandas中使用stack和pivot實(shí)現(xiàn)數(shù)據(jù)透視。感興趣的小伙伴們可以參考一下2021-09-09
Python OpenCV Hough直線檢測(cè)算法的原理實(shí)現(xiàn)
這篇文章主要介紹了Python OpenCV Hough直線檢測(cè)算法的原理實(shí)現(xiàn),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
淺談python的elementtree模塊處理中文注意事項(xiàng)
這篇文章主要介紹了淺談python的elementtree模塊處理中文注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
pyqt5 tablewidget 利用線程動(dòng)態(tài)刷新數(shù)據(jù)的方法
今天小編就為大家分享一篇pyqt5 tablewidget 利用線程動(dòng)態(tài)刷新數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python?中的異步上下文生成器?@asynccontextmanager詳解
@asynccontextmanager是Python?contextlib模塊提供的異步上下文管理器裝飾器,專為異步編程設(shè)計(jì),本文給大家介紹Python中的異步上下文生成器?@asynccontextmanager的相關(guān)操作,感興趣的朋友跟隨小編一起看看吧2026-02-02
Python教程pandas數(shù)據(jù)分析去重復(fù)值
Pandas指定行進(jìn)行去重更新值,加載數(shù)據(jù)sample抽樣函數(shù),指定需要更新的值append直接添加append函數(shù)用法,根據(jù)某一列key值進(jìn)行去重key唯一2021-09-09

