python編程調(diào)用設(shè)備串口發(fā)送數(shù)據(jù)方式
python調(diào)用設(shè)備串口發(fā)送數(shù)據(jù)
為了通過(guò)python編程控制串口發(fā)送數(shù)據(jù)給單片機(jī),編寫此程序
使用serial模塊完成串口的讀取和數(shù)據(jù)的收發(fā)
重點(diǎn)掌握以下幾個(gè)api的使用方法
- 讀取串口設(shè)備列表:list(serial.tools.list_ports.comports())
- 初始化串口對(duì)象:ser=serial.Serial()
- 打開串口:ser.isOpen()
- 發(fā)送數(shù)據(jù):ser.write()
- 讀取數(shù)據(jù):ser.read()
- 關(guān)閉串口:ser.close()
完整代碼
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# ============================================================
# @Date : 2022/05/16 21:50:12
# @Author : miles
# @Email : lishan@st.xatu.edu.cn
# @File : serial_demo.py
# @IDE : PyCharm
# @Func : Describes the function of the file
# @Note : pip install pyserial
# ============================================================
"""
import time
import serial.tools.list_ports
if __name__ == '__main__':
# 讀取串口列表
ports_list = list(serial.tools.list_ports.comports())
if len(ports_list) <= 0:
print("無(wú)串口設(shè)備")
else:
print("可用的串口設(shè)備如下: ")
print("%-10s %-30s %-10s" % ("num", "name", "number"))
for i in range(len(ports_list)):
comport = list(ports_list[i])
comport_number, comport_name = comport[0], comport[1]
print("%-10s %-30s %-10s" % (i, comport_name, comport_number))
# 打開串口
port_num = ports_list[0][0]
print("默認(rèn)選擇串口: %s" % port_num)
# 串口號(hào): port_num, 波特率: 115200, 數(shù)據(jù)位: 7, 停止位: 2, 超時(shí)時(shí)間: 0.5秒
ser = serial.Serial(port=port_num, baudrate=115200, bytesize=serial.SEVENBITS, stopbits=serial.STOPBITS_TWO,
timeout=0.5)
if not ser.isOpen():
print("打開串口失敗")
else:
print("打開串口成功, 串口號(hào): %s" % ser.name)
# 串口發(fā)送字符串?dāng)?shù)據(jù)
data = "%d:%d" % (130, 1)
print("發(fā)送數(shù)據(jù): %s" % data)
write_len = ser.write(data.encode('utf-8'))
print("串口發(fā)出{}個(gè)字節(jié)".format(write_len))
# 串口發(fā)送十六進(jìn)制數(shù)據(jù)
# data = 0xAB
# print("發(fā)送數(shù)據(jù): %X" % data)
# write_len = ser.write(bytearray([data]))
# print("串口發(fā)出{}個(gè)字節(jié)".format(write_len))
# 等待串口返回信息并輸出
t0 = time.time()
while True:
com_input = ser.read(10)
t1 = time.time()
t = t1 - t0
print("\r等待串口接收數(shù)據(jù), %.2f 秒" % t, end="")
if com_input or t >= 3:
if com_input:
print("\n%s" % com_input)
else:
print("\n%s" % "沒有接收到任何數(shù)據(jù)")
break
# 關(guān)閉串口
ser.close()
if ser.isOpen():
print("串口未關(guān)閉")
else:
print("串口已關(guān)閉")在電腦上插入U(xiǎn)SB轉(zhuǎn)串口模塊連接到單片機(jī),
運(yùn)行結(jié)果

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch創(chuàng)建tensor函數(shù)詳情
這篇文章主要介紹了pytorch創(chuàng)建tensor函數(shù)詳情,文章圍繞tensor函數(shù)的相關(guān)自來(lái)哦展開詳細(xì)內(nèi)容的介紹,需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-03-03
Python中多個(gè)裝飾器執(zhí)行順序驗(yàn)證深入講解
裝飾器是Python中一種強(qiáng)大的語(yǔ)法結(jié)構(gòu),本質(zhì)上是一個(gè)特殊的函數(shù),它的主要作用是在不改變?cè)瘮?shù)代碼的基礎(chǔ)上,為其他函數(shù)或類添加額外的功能,這篇文章主要介紹了Python中多個(gè)裝飾器執(zhí)行順序驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2025-10-10
Python自動(dòng)化操作Excel生成專業(yè)數(shù)據(jù)透視表的實(shí)戰(zhàn)指南
在數(shù)據(jù)分析的廣闊領(lǐng)域中,數(shù)據(jù)透視表無(wú)疑是處理和理解海量數(shù)據(jù)的強(qiáng)大工具,本文將深入探討如何利用Python庫(kù),實(shí)現(xiàn)Excel數(shù)據(jù)透視表的自動(dòng)化生成、配置與格式化,感興趣的可以了解下2025-12-12
PyCharm打代碼時(shí)出現(xiàn)白色光標(biāo)問(wèn)題(筆記本的解決方案)
PyCharm中白色光標(biāo)通常是虛擬空格功能導(dǎo)致的,可以通過(guò)按下Insert鍵或在設(shè)置中取消勾選“Show virtual space at line end”選項(xiàng)來(lái)解決2025-02-02
Python?round函數(shù)的基本用法與實(shí)例代碼
round()函數(shù)是Python中用于對(duì)浮點(diǎn)數(shù)進(jìn)行四舍五入的內(nèi)置函數(shù),這篇文章詳細(xì)介紹了round()函數(shù)的基本用法、參數(shù)詳解、特殊情況處理以及應(yīng)用場(chǎng)景,并提供了豐富的示例代碼,需要的朋友可以參考下2024-11-11
Python空間數(shù)據(jù)處理之GDAL讀寫遙感圖像
這篇文章主要介紹了Python空間數(shù)據(jù)處理之GDAL讀寫遙感圖像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法
今天小編就為大家分享一篇Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

