python串口讀取數(shù)據(jù)的實(shí)例
python串口讀取數(shù)據(jù)
python庫(kù) serial
初學(xué)者學(xué)習(xí)使用串口接收數(shù)據(jù),serial為python提供的串口通信庫(kù)
串口調(diào)試工具
串口調(diào)試工具使用:ssscom(習(xí)小猛版)
使用步驟:
1、連接設(shè)備至電腦,檢查是否識(shí)別具有驅(qū)動(dòng)程序(我的電腦-管理-設(shè)備管理器-端口)
2、打開(kāi)ssscom,自動(dòng)識(shí)別設(shè)備,左上角點(diǎn)擊通訊端口,選擇使用串口的設(shè)備
3、設(shè)置串口設(shè)置
4、點(diǎn)擊打開(kāi)串口、接收數(shù)據(jù)
5、觀察接收數(shù)據(jù)可得出通信頻率
注:port為端口號(hào)、baud rate為波特率、data bits為數(shù)據(jù)字節(jié),stop bits為停止字節(jié),parity為校驗(yàn)位
serial庫(kù)
1、引入庫(kù) import serial
2、串口常用的屬性
name
:設(shè)備名字port
:端口baudrate
:波特率bytesize
:字節(jié)大小parity
:校驗(yàn)位stopbits
:停止位timeout
:讀超時(shí)設(shè)置
3、串口常用的方法
open()
:打開(kāi)串口close()
:關(guān)閉串口inWaiting()
:返回緩存中的字節(jié)數(shù),搭配time.sleep使用,否則數(shù)據(jù)會(huì)沒(méi)有進(jìn)入緩存區(qū)而結(jié)束程序
注:如果需要持續(xù)接收數(shù)據(jù),可設(shè)置一個(gè)定時(shí)器,反復(fù)調(diào)用接收串口數(shù)據(jù)
編程實(shí)例
import serial import time serialport = serial.Serial() serialport.port = 'COM4' serialport.baudrate = 115200 serialport.bytesize = 8 serialport.parity = serial.PARITY_NONE serialport.stopbits = 1 serialport.timeout = 0.001 serialport.close() if not serialport.is_open: serialport.open() time.sleep(0.05) #時(shí)間設(shè)置參考串口傳輸速率 num = serialport.inWaiting() if num > 0: data = serialport.read(num) print(data)
python串口讀取數(shù)據(jù)及下發(fā)數(shù)據(jù)
import sqlite3 import threading from datetime import datetime import serial import time def dh(): t=threading.Timer(1, run) t.start() def run(): dh() serialport = serial.Serial() serialport.port = 'COM4' serialport.baudrate = 115200 serialport.bytesize = 8 serialport.parity = serial.PARITY_NONE serialport.stopbits = 1 serialport.timeout = 20 serialport.close() if not serialport.is_open: serialport.open() time.sleep(0.5) # 時(shí)間設(shè)置參考串口傳輸速率 num = serialport.inWaiting() if num > 0: data = serialport.read(num)# 讀取數(shù)據(jù)解析數(shù)據(jù)插入sqlite 數(shù)據(jù)庫(kù) print( data.decode().strip().split(",")[0]) sql = "insert into t_table (code,weight,feedIntake,time)values(?,?,?,?)" conn = sqlite3.connect("test.db") c = conn.cursor() try: c.execute(sql, data.decode().strip().split(",")) conn.commit() print("數(shù)據(jù)插入成功") except Exception as e: print("數(shù)據(jù)插入失敗", e) finally: code = data.decode().strip().split(",")[0] gg = tuple([code]) #searchcode(gg) conn.close() return "數(shù)據(jù)插入成功" dh()
# 串口發(fā)送數(shù)據(jù) def FeedComposia(a,b,c): serialport = serial.Serial() serialport.port = 'COM3' serialport.baudrate = 115200 serialport.bytesize = 8 serialport.parity = serial.PARITY_NONE serialport.stopbits = 1 serialport.timeout = 20 serialport.close() if not serialport.is_open: serialport.open() time.sleep(0.5) # 時(shí)間設(shè)置參考串口傳輸速率 t=a,b,c #數(shù)據(jù)要轉(zhuǎn)換一下,才能接受 v = json.dumps(t, ensure_ascii=False) serialport.write(v.encode()) print("a", a) print("b", b) return a, b
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python中串口通信庫(kù)pyserial基礎(chǔ)知識(shí)
- Python讀取串口數(shù)據(jù)的實(shí)現(xiàn)方法
- python讀取串口數(shù)據(jù)有幾種方法
- python serial串口通信示例詳解
- 玩轉(zhuǎn)串口通信:利用pyserial庫(kù),Python打開(kāi)無(wú)限可能
- Python通過(guò)串口實(shí)現(xiàn)收發(fā)文件
- Python串口通信的接收與發(fā)送的實(shí)現(xiàn)
- python實(shí)現(xiàn)串口通信的示例代碼
- Python?Serial串口的簡(jiǎn)單數(shù)據(jù)收發(fā)方式
- 使用Python玩轉(zhuǎn)串口(基于pySerial問(wèn)題)
- 使用 Python 列出串口的實(shí)現(xiàn)方法
- Python中串口操作的實(shí)現(xiàn)示例
相關(guān)文章
聊聊boost?python3依賴(lài)安裝問(wèn)題
這篇文章主要介紹了boost?python3依賴(lài)安裝,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Sublime如何配置Python3運(yùn)行環(huán)境
這篇文章主要介紹了Sublime如何配置Python3運(yùn)行環(huán)境問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11完美解決python遍歷刪除字典里值為空的元素報(bào)錯(cuò)問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決python遍歷刪除字典里值為空的元素報(bào)錯(cuò)問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09Python中等待5秒并暫停執(zhí)行的方法總結(jié)
Python 具有各種功能和庫(kù)來(lái)創(chuàng)建交互式應(yīng)用程序,用戶(hù)可以在其中提供輸入和響應(yīng), 我們可以創(chuàng)建需要暫停應(yīng)用程序執(zhí)行的情況,本文主要和大家分享三個(gè)Python 中等待 5 秒并暫停執(zhí)行的方法,有需要的可以參考下2023-10-10Python自定義進(jìn)程池實(shí)例分析【生產(chǎn)者、消費(fèi)者模型問(wèn)題】
這篇文章主要介紹了Python自定義進(jìn)程池,結(jié)合實(shí)例分析了Python使用自定義進(jìn)程池實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者模型問(wèn)題,需要的朋友可以參考下2016-09-09python中__new__和__init__的實(shí)現(xiàn)
在Python中,每個(gè)對(duì)象都有兩個(gè)特殊的方法__new__和__init__,本文主要介紹了python中__new__和__init__的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05