最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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í)設(shè)置,None:永遠(yuǎn)等待操作,0為立即返回請求結(jié)果,其他值為等待超時(shí)時(shí)間(單位為秒)
? ? global starttime
? ? global endtime
? ? global endtimebefore
? ? sbtforce = serial.Serial(port, baud, timeout=timex) #此行不能放在循環(huán)內(nèi),因?yàn)楹臅r(shí)長
? ? sendmessage = sbtforce.write(bytes.fromhex('FE010701000001CFFCCCFF'))
? ? offset = 20 ?# 收到的數(shù)據(jù)跟放大器上的示數(shù)不對應(yīng),需要減去一個(gè)偏差值,根據(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個(gè)字節(jié):

data = sbtforce.read(10)

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

data[0]

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

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

data[0:1]

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

python串口使用問題

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

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

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

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

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

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進(jìn)制后發(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中:常見容易忽略的錯(cuò)誤

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

總結(jié)

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

相關(guān)文章

最新評論

济宁市| 滕州市| 金秀| 柘城县| 上犹县| 泰州市| 大余县| 远安县| 梓潼县| 罗城| 行唐县| 绥芬河市| 武强县| 莲花县| 徐水县| 墨玉县| 高安市| 临桂县| 织金县| 西畴县| 临安市| 宣恩县| 弥勒县| 无为县| 天台县| 张家川| 家居| 资兴市| 万宁市| 安丘市| 尼勒克县| 久治县| 郑州市| 基隆市| 德阳市| 繁峙县| 阳春市| 正定县| 泸定县| 陈巴尔虎旗| 洪洞县|