python編程調(diào)用設(shè)備串口發(fā)送數(shù)據(jù)方式
更新時間:2023年09月06日 10:15:18 作者:樂觀的lishan
這篇文章主要介紹了python編程調(diào)用設(shè)備串口發(fā)送數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
python調(diào)用設(shè)備串口發(fā)送數(shù)據(jù)
為了通過python編程控制串口發(fā)送數(shù)據(jù)給單片機,編寫此程序
使用serial模塊完成串口的讀取和數(shù)據(jù)的收發(fā)
重點掌握以下幾個api的使用方法
- 讀取串口設(shè)備列表:list(serial.tools.list_ports.comports())
- 初始化串口對象: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("無串口設(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("默認選擇串口: %s" % port_num)
# 串口號: port_num, 波特率: 115200, 數(shù)據(jù)位: 7, 停止位: 2, 超時時間: 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("打開串口成功, 串口號: %s" % ser.name)
# 串口發(fā)送字符串數(shù)據(jù)
data = "%d:%d" % (130, 1)
print("發(fā)送數(shù)據(jù): %s" % data)
write_len = ser.write(data.encode('utf-8'))
print("串口發(fā)出{}個字節(jié)".format(write_len))
# 串口發(fā)送十六進制數(shù)據(jù)
# data = 0xAB
# print("發(fā)送數(shù)據(jù): %X" % data)
# write_len = ser.write(bytearray([data]))
# print("串口發(fā)出{}個字節(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)閉")在電腦上插入USB轉(zhuǎn)串口模塊連接到單片機,
運行結(jié)果

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?Collections庫的高級功能使用示例詳解
Python的collections庫提供了一系列有用的數(shù)據(jù)類型,擴展了內(nèi)建的數(shù)據(jù)類型,為開發(fā)者提供了更多高級功能,本文將深入探討collections庫的一些高級功能,通過詳細的示例代碼演示,幫助大家更好地理解和應(yīng)用這些功能2023-12-12
利用Python腳本實現(xiàn)ping百度和google的方法
最近在做SEO的時候,為了讓發(fā)的外鏈能夠快速的收錄,想到了利用ping的功能,google和百度都有相關(guān)的ping介紹,有興趣的朋友可以去看看相關(guān)的知識。下面這篇文章主要介紹了利用Python腳本實現(xiàn)ping百度和google的方法,需要的朋友可以參考借鑒,一起來看看吧。2017-01-01
Python3 Post登錄并且保存cookie登錄其他頁面的方法
今天小編就為大家分享一篇Python3 Post登錄并且保存cookie登錄其他頁面的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
五個簡單有效的Python清理數(shù)據(jù)腳本分享
通常情況下,在機器學(xué)習(xí)中的數(shù)據(jù)清理往往是一件令人頭疼的事情,本文整理了一份清單,列出了5個常用的Python腳本,用于自動化數(shù)據(jù)清理,需要的可以參考一下2022-09-09
Python實現(xiàn)連接dr校園網(wǎng)示例詳解
在校園里認證上網(wǎng)很麻煩需要web輸入賬號密碼有時還會忘記web地址,所以為了解決這一問題,本文將為大家介紹通過Python實現(xiàn)校園網(wǎng)的連接,快來跟隨小編一起學(xué)習(xí)吧2021-11-11
Python探索之靜態(tài)方法和類方法的區(qū)別詳解
這篇文章主要介紹了Python探索之靜態(tài)方法和類方法的區(qū)別詳解,小編覺得還是挺不錯的,這里分享給大家,供需要的朋友參考。2017-10-10

