欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python串口如何讀取byte類型數(shù)據(jù)并訪問

 更新時間:2023年09月08日 08:46:22   作者:千里飛刀客  
這篇文章主要介紹了python串口如何讀取byte類型數(shù)據(jù)并訪問方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python串口讀取byte類型數(shù)據(jù)并訪問

以讀取SBT力傳感器數(shù)據(jù)為例

#! usr/bin/env pyhton
# coding:utf-8
import serial
import time
import csv
import os
originaltime = 0.0
starttime = 0.0
endtime = 0.0
endtimebefore = 0.0
def savedis(force , csvfile):
? ? f = open(csvfile, 'a', encoding='utf-8', newline='') ?# 'w'覆蓋寫 'a'追加寫
? ? csv_writer = csv.writer(f)
? ? global endtime
? ? time_head = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(endtime))
? ? time_sec = (endtime - int(endtime)) * 1000
? ? timesample = "%s.%03d" % (time_head, time_sec)
? ? csv_writer.writerow([timesample, str(force)])
? ? # f.close()
def main():
? ? # 文件處理
? ? current_path = os.path.abspath(__file__)[:-len('sbt_test.py')]
? ? currenttime = time.localtime(time.time())
? ? csvfile = current_path + str(currenttime.tm_hour) + '-' + str(currenttime.tm_min) +'-'+ str(currenttime.tm_sec) + '.csv'
? ? f = open(csvfile, 'a', encoding='utf-8', newline='') ?# 'w'覆蓋寫 'a'追加寫
? ? csv_writer = csv.writer(f)
? ? csv_writer.writerow(['timestamp(ms)', 'display(N)'])
? ? f.close()
? ? # 串口聲明
? ? port = 'COM7' #根據(jù)設(shè)備管理器的端口號更改
? ? baud = 115200
? ? timex = 0.02 ?# 超時設(shè)置,None:永遠等待操作,0為立即返回請求結(jié)果,其他值為等待超時時間(單位為秒)
? ? global starttime
? ? global endtime
? ? global endtimebefore
? ? sbtforce = serial.Serial(port, baud, timeout=timex) #此行不能放在循環(huán)內(nèi),因為耗時長
? ? sendmessage = sbtforce.write(bytes.fromhex('FE010701000001CFFCCCFF'))
? ? offset = 20 ?# 收到的數(shù)據(jù)跟放大器上的示數(shù)不對應(yīng),需要減去一個偏差值,根據(jù)示數(shù)調(diào)整
? ? while True:
? ? ? ? endtime = time.time()
? ? ? ? startdata = sbtforce.read(1)
? ? ? ? if startdata == b'\xFE':
? ? ? ? ? ? buffer = sbtforce.read(10)
? ? ? ? ? ? if (buffer[9] == 255):
? ? ? ? ? ? ? ? print("buffer=",buffer)
? ? ? ? ? ? ? ? force = buffer[2:6]
? ? ? ? ? ? ? ? print('force = ',force)
? ? ? ? ? ? ? ? force = force[0] << 24 | force[1] << 16 | force[2] << 8 | force[3]
? ? ? ? ? ? ? ? if force > 100000000:
? ? ? ? ? ? ? ? ? ? force = force - 2**32 - offset
? ? ? ? ? ? ? ? if force > 0:
? ? ? ? ? ? ? ? ? ? force -= offset
? ? ? ? ? ? ? ? force = force / 100.0
? ? ? ? ? ? ? ? print('壓力值:', force)
? ? ? ? ? ? ? ? savedis(force, csvfile)
if __name__ == '__main__':
? ? main()

聲明串口對象:

sbtforce = serial.Serial(port, baud, timeout=timex)

然后從串口讀取字節(jié),比如一次讀取10個字節(jié):

data = sbtforce.read(10)

然后,假如要訪問data的第一個字節(jié),如果直接寫

data[0]

那么返回的將是第一個字節(jié)對應(yīng)的整形,也就是說type(data[0])=int

那么如何拿到byte類型呢,可以這樣做:

data[0:1]

這樣返回的將會是字節(jié)類型的第一個字節(jié)的數(shù)據(jù)。

python串口使用問題

python串口數(shù)據(jù)

python串口收發(fā)的都是bytes類型數(shù)據(jù),即使是字符串,也會編碼器后進行傳輸

設(shè)置串口,并打開

ser1 = serial.Serial(port='/dev/ttyUSB1', baudrate=115200, bytesize=8, parity='N', stopbits=1)

這樣創(chuàng)建一個串口對象后,串口直接自動打開

python中發(fā)送數(shù)據(jù)使用write()函數(shù)

 s.write(data) #不指明編碼方式,直接發(fā)送
 s.write(data.encode('utf-8'))  # utf-8 編碼發(fā)送
 s.write(data.encode('hex'))  # 轉(zhuǎn)成16進制后發(fā)送

python中接收數(shù)據(jù)使用read_all()和read(len)函數(shù)

  • read_all()會讀取緩沖區(qū)內(nèi)的數(shù)據(jù)
  • read(len)讀取len長的數(shù)據(jù)

python2和python3之間的區(qū)別

bytes是Python 3中特有的,Python 2 里不區(qū)分bytes和str。bytes是byte的序列,而str是unicode的序列。

python2中:

str 使用encode()方法轉(zhuǎn)化為

bytesbytes通過decode()轉(zhuǎn)化為str

在python3中:常見容易忽略的錯誤

串口接收到數(shù)據(jù)很亂,更預(yù)計差別很大,首先檢查波特率對不對

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論