Python使用pyserial進行串列埠通訊

2021-07-26 00:02:35 字數 1309 閱讀 9578

安裝pyserial

pip install pyserial
檢視可用的埠

# coding:utf-8

import serial.tools.list_ports

plist = list(serial.tools.list_ports.comports())

if len(plist) <= 0:

print("沒有發現埠!")

else:

plist_0 = list(plist[0])

serialname = plist_0[0]

serialfd = serial.serial(serialname, 9600, timeout=60)

print("可用埠名》", serialfd.name)

所發十六進製制需轉換為以下格式

# 所發十六進製制字串010591f50000f104

cmd = [0x01, 0x05, 0x91, 0xf5, 0x00, 0x00, 0xf1, 0x04]

串列埠通訊

windows下埠為com*, ubuntu下為/dev/ttys0

import serial

class

ser(object):

def__init__

(self):

# 開啟埠

self.port = serial.serial(port='com4', baudrate=9600, bytesize=8, parity='e', stopbits=1, timeout=2)

# 傳送指令的完整流程

defsend_cmd

(self, cmd):

self.port.write(cmd)

response = self.port.readall()

response = self.convert_hex(response)

return response

# 轉成16進製制的函式

defconvert_hex

(self, string):

res =

result =

for item in string:

for i in res:

return result

python元類的使用 python使用元類

原文 type 動態語言和靜態語言最大的不同,就是函式和類的定義,不是編譯時定義的,而是執行時動態建立的。比方說我們要定義乙個hello的class,就寫乙個hello.py模組 當python直譯器載入hello模組時,就會依次執行該模組的所有語句,執行結果就是動態建立出乙個hello的class...

Python精通 Python函式使用

在程式設計意義上的函式其實是指完成某種操作的 塊,當然這個是個人的理解,但是這個概念在所有的程式語言中都是通用的。這個 塊用來完成某寫特定的操作。但是在數學上的函式卻是表示某種對應關係,這兩者之間還是有一定的區別的。但是在某種角度上講我們所程式設計的這種 塊其實就是表示的是引數與返回值之間的關係。從...

python 元組使用 使用元組

usr bin python filename using tuple.py zoo wolf elephant penguin print number of animals in the zoo is len zoo new zoo monkey dolphin zoo print number...