Python應(yīng)用開發(fā)之實(shí)現(xiàn)串口通信
前言
在嵌入式開發(fā)中我們經(jīng)常會(huì)用到串口,串口通信簡(jiǎn)單,使用起來方便,且適用場(chǎng)景多,因此串口常常用來輸出調(diào)試Log或者跟其他外設(shè)進(jìn)行通訊,也可以用作上位機(jī)和下位機(jī)之間的通訊。那這一講我就簡(jiǎn)單介紹一下如何用Python編寫一個(gè)串口收發(fā)程序。
1.環(huán)境搭建
Python的環(huán)境這里就不具體介紹了,網(wǎng)上教程也很多,我主要介紹一下我們這一講用到的類庫以及當(dāng)前的版本。
package | version |
---|---|
time | 注:python標(biāo)準(zhǔn)庫,無需安裝 |
pyserial | v3.5 |
注:不懂如何安裝庫的同學(xué)請(qǐng)自行查閱資料。
特別說明:本文舉的例子皆是基于windows系統(tǒng)下python3來實(shí)現(xiàn)的,其他平臺(tái)不一定完全適配。
2.硬件準(zhǔn)備
我們要通過電腦的USB接口發(fā)送串口數(shù)據(jù),在硬件上首先得保證有可用的串口設(shè)備接入,并且正確的安裝了驅(qū)動(dòng)。
我這里用的是一個(gè)CH340的串口轉(zhuǎn)TTL的模塊來測(cè)試的。
可以用兩個(gè)這種模塊相互收發(fā),也可以單個(gè)模塊自發(fā)自收,或者接入其他嵌入式設(shè)備也行。
硬件接入之后我們可以在設(shè)備管理器看到設(shè)備的端口號(hào),如下圖所示:
3.代碼編寫與測(cè)試
3.1 簡(jiǎn)單測(cè)一下串口收發(fā)
示例代碼如下:
import serial #導(dǎo)入模塊 try: # 端口號(hào),根據(jù)自己實(shí)際情況輸入,可以在設(shè)備管理器查看 port = "COM6" # 串口波特率,根據(jù)自己實(shí)際情況輸入 bps = 9600 # 超時(shí)時(shí)間,None:永遠(yuǎn)等待操作,0為立即返回請(qǐng)求結(jié)果,其他值為等待超時(shí)時(shí)間(單位為秒) time = 5 # 打開串口,并返回串口對(duì)象 uart = serial.Serial(port, bps, timeout = time) # 串口發(fā)送一個(gè)字符串 len = uart.write("hello world".encode('utf-8')) print("send len: ", len) # 串口接收一個(gè)字符串 str = '' for i in range(len): str += uart.read().decode("utf-8") print("receive data: ", str) # 關(guān)閉串口 uart.close() except Exception as result: print("******error******:", result)
運(yùn)行測(cè)試:
我這里是自發(fā)自收,如果運(yùn)行沒有出錯(cuò)并且成功發(fā)送和接收到正確的數(shù)據(jù),說明串口通信是走通了,那么接下來就可以繼續(xù)完善這個(gè)流程。
如果出現(xiàn)其他問題,則需要檢查硬件和軟件,確保所有配置都沒問題才能繼續(xù)進(jìn)行下一步。
3.2 補(bǔ)充細(xì)節(jié)
1、串口發(fā)送各種不同類型的數(shù)據(jù)。
串口數(shù)據(jù)常用字符串和十六進(jìn)制(hex)表示。下面列舉了一些例子,可以作為一種參考。
示例如下:
data1 = "hello world" # 字符串 data2 = b"hello world" # bytes data3 = "你好" # 中文字符串 data4 = 0x0A # 整形(以16進(jìn)制表示) data5 = [0x10, 0x11, 0x12] # 列表/數(shù)組(以16進(jìn)制表示) len = uart.write(data1.encode('utf-8')) # 發(fā)送字符串"hello world" len = uart.write(data2) # 發(fā)送字符串"hello world" len = uart.write(data3.encode('utf-8')) # 以u(píng)tf-8編碼方式發(fā)送字符串"你好"(6字節(jié)) len = uart.write(data3.encode('gbk')) # 以gbk編碼方式發(fā)送字符串"你好"(4字節(jié)) len = uart.write(chr(data4.encode("utf-8")) # 發(fā)送16進(jìn)制數(shù)據(jù)0x0A(1字節(jié)) for x in data5: # 遍歷列表/數(shù)組的所有元素并依次發(fā)送 len = uart.write(chr(x).encode("utf-8"))
2、完善串口接收流程
串口接收跟發(fā)送一樣,接收數(shù)據(jù)也是常用字符串和十六進(jìn)制(hex)表示。
可以根據(jù)以下兩種方式接收數(shù)據(jù):
str = uart.read(uart.in_waiting).decode("utf-8") # 以字符串接收 str = uart.read().hex() # 以16進(jìn)制(hex)接收
接收的時(shí)間很短,大部分時(shí)間其實(shí)是在等待接收,所以我們需要加一個(gè)死循環(huán)或者循環(huán)線程來確保串口一直在等待接收。
示例如下:
while True: if uart.in_waiting: # str = uart.read(uart.in_waiting).decode("utf-8") # 以字符串接收 str = uart.read().hex() # 以16進(jìn)制(hex)接收 print(str) # 打印數(shù)據(jù)
3、掃描端口
在不知道串口端口號(hào)的情況下可以先掃描一下可用的端口。
示例如下:
import serial import serial.tools.list_ports port_list = list(serial.tools.list_ports.comports()) print(port_list) if len(port_list) == 0: print('無可用串口') else: for i in range(0,len(port_list)): print(port_list[i])
3.3 完善整個(gè)收發(fā)流程
根據(jù)上面舉的一些例子,我們把整個(gè)流程補(bǔ)充完整。
示例如下:
# 導(dǎo)入模塊 import threading import time import serial import serial.tools.list_ports # 自定義變量 port = "COM6" # 端口號(hào),根據(jù)自己實(shí)際情況輸入,可以在設(shè)備管理器查看 bps = 9600 # 串口波特率,根據(jù)自己實(shí)際情況輸入 timeout = 5 # 超時(shí)時(shí)間,None:永遠(yuǎn)等待操作,0為立即返回請(qǐng)求結(jié)果,其他值為等待超時(shí)時(shí)間(單位為秒) rxdata = '' # 接收的數(shù)據(jù) # 掃描端口 def check_uart_port(): port_list = list(serial.tools.list_ports.comports()) # print(port_list) if len(port_list) == 0: print('can not fine uart port') return False else: for i in range(0,len(port_list)): print(port_list[i]) return True # 打開串口 def open_uart(port, bps, timeout): try: # 打開串口,并返回串口對(duì)象 uart = serial.Serial(port, bps, timeout=timeout) return uart except Exception as result: print("can not open uart") print(result) return False # 發(fā)送數(shù)據(jù) def uart_send_data(uart, txbuf): len = uart.write(txbuf.encode('utf-8')) # 寫數(shù)據(jù) return len # 接收數(shù)據(jù) def uart_receive_data(uart): if uart.in_waiting: rxdata = uart.read(uart.in_waiting).decode("utf-8") # 以字符串接收 # rxdata = uart.read().hex() # 以16進(jìn)制(hex)接收 print(rxdata) # 打印數(shù)據(jù) # 關(guān)閉串口 def close_uart(uart): uart.close() # 創(chuàng)建一個(gè)線程用來等待串口接收數(shù)據(jù) class myThread (threading.Thread): # 繼承父類threading.Thread def __init__(self, uart): threading.Thread.__init__(self) self.uart = uart def run(self): # 把要執(zhí)行的代碼寫到run函數(shù)里面 線程在創(chuàng)建后會(huì)直接運(yùn)行run函數(shù) while True: # print("thread_uart_receive") uart_receive_data(self.uart) # 接收數(shù)據(jù) # time.sleep(0.01) # 主函數(shù) def main(): # 掃描端口 result = check_uart_port() if(result == False): return # 打開串口 result = open_uart(port, bps, timeout) if (result == False): return else: uart1 = result # 創(chuàng)建一個(gè)線程用來接收串口數(shù)據(jù) thread_uart = myThread(uart1) thread_uart.start() while True: # 定時(shí)發(fā)送數(shù)據(jù) txbuf = "hello world" len = uart_send_data(uart1, txbuf) print("send len: ", len) time.sleep(1) # 啟動(dòng)主函數(shù) main()
運(yùn)行測(cè)試:
這里還是自發(fā)自收,但是改成定時(shí)1s循環(huán)發(fā)送一幀數(shù)據(jù),如果是實(shí)際使用的話就不要短接TX和RX了,串口是全雙工,收發(fā)是可以同步進(jìn)行的。
以上就是Python應(yīng)用開發(fā)之實(shí)現(xiàn)串口通信的詳細(xì)內(nèi)容,更多關(guān)于Python串口通信的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入了解Python中計(jì)數(shù)器Counter的使用
計(jì)數(shù)器counter是包含在collections模塊中的容器,這篇文章主要來和大家聊聊計(jì)數(shù)器counter的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09Google colab中從kaggle中接入數(shù)據(jù)的操作方法
這篇文章主要介紹了Google colab中如何從kaggle中接入數(shù)據(jù),本文涉及到兩大平臺(tái)內(nèi)容,所以我默認(rèn)你已經(jīng)擁有了,并且使用過了一段時(shí)間的google賬號(hào)和kaggle賬號(hào),需要的朋友可以參考下2024-03-03聊聊python 邏輯運(yùn)算及奇怪的返回值(not,and,or)問題
在Python中,真值為假的對(duì)象,包括False,None,數(shù)字0,空字符串以及空的容器類型,除此以外的任何對(duì)象均為真,本文重點(diǎn)給大家介紹python 邏輯運(yùn)算及奇怪的返回值(not,and,or)問題,感興趣的朋友一起看看吧2022-03-03python numpy矩陣信息說明,shape,size,dtype
這篇文章主要介紹了python numpy矩陣信息說明,shape,size,dtype,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05python re正則表達(dá)式模塊(Regular Expression)
Python 的 re 模塊(Regular Expression 正則表達(dá)式)提供各種正則表達(dá)式的匹配操作,在文本解析、復(fù)雜字符串分析和信息提取時(shí)是一個(gè)非常有用的工具.2014-07-07