用python編寫串列埠測試指令碼

2021-09-26 21:44:06 字數 3078 閱讀 6421

用python編寫串列埠測試指令碼

最近接到了乙個測試硬體的內容,有大概十臺左右,給了乙個串列埠測試的文件,讓我來測硬體,測試的前兩天使用的手工測試,實在有點麻煩,晚上加班寫了個指令碼來簡單的自動執行一下。

import serial.tools.list_ports

import time

import serial

ser = serial.serial(

"com4"

,115200

, timeout=20)

# 測試adc狀態,開關鎖,獲取開關鎖狀態,led燈,

defteststatus()

:if ser.isopen():

print

("open success"

)for column in

range(1

,4):

for i in

range(0

,7):

ca =

str(column)

ia =

str(i)

# can't concat str to bytes出現以下錯誤

# ia.encode('utf-8')

ser.write(b'+ch'

+ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':opendoor:'

+ b'\n'

)print

(ser.readline())

time.sleep(1)

ser.write(b'+ch'

+ ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':getlock:'

+ b'\n'

)print

(ser.readline())

time.sleep(

0.5)

ser.write(b'+ch'

+ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':closedoor:'

+ b'\n'

)print

(ser.readline())

time.sleep(1)

ser.write(b'+ch'

+ ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':getlock:'

+ b'\n'

) ser.write(b'+ch1'

+ ia.encode(

'utf-8'

)+ b':getlock:'

+ b'\n'

)print

(ser.readline())

time.sleep(

0.5)

ser.write(b'+ch'

+ ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':closeluminary:'

+ b'\n'

)print

(ser.readline())

time.sleep(1)

ser.write(b'+ch'

+ ca.encode(

'utf-8'

)+ ia.encode(

'utf-8'

)+ b':openluminary:'

+ b'\n'

)print

(ser.readline())

time.sleep(

0.5)

else

:print

("open failed"

) ser.close(

)if __name__ ==

"__main__"

: teststatus(

)

這裡涉及乙個轉換string 轉bytes問題

因為串列埠輸入的是需要bytes型別的 ,所以在這裡大概介紹幾種相互轉換的方法

#bytes object 

by = b'example'

#str object

st =

'example'

str與bytes進行轉換

#str to bytes  

bytes

(s, encoding =

"utf8"

)#bytes to str

str(b, encoding =

"utf-8"

)#an alternative method

#str to bytes

str.encode(s)

#bytes to str

bytes

.decode(b)

raise serialraise serialexception("could not open port : ".format(self.portstr, ctypes.winerror()))    

serial.serialutil.serialexception: could not open port 'com4': permissionerror(13, '拒絕訪問。', none, 5)

注2:未連線串列埠時出現的報錯

raise serialexception("could not open port : ".format(self.portstr, ctypes.winerror()))

serial.serialutil.serialexception: could not open port 'com4': filenotfounderror(2, '系統找不到指定的檔案。', none, 2)

用Python編寫指令碼進行軟體自動安裝配置

更新 2010 04 11 字型 大 中 小 測試機器 10.10.33.119 os redhat linux4 測試路徑 home sonky 指令碼1 使用exec 系列函式,在python裡面,os.exec 系列命令會完全代替python直譯器,如果執行os.exec 函式,則python...

用ant編寫自動編譯指令碼

第一步 ant的配置 1 在ant安裝之前需要安裝好jdk,並配置好j a home 2 新建環境變數ant home 值為ant的主目錄 3 在path中配置 ant home bin 4 在classpath中配置 ant home lib 5 配置完成之後,開啟命令列,輸入ant,當出現 bu...

Python測試驅動編寫

通過gr modtool會自動生成測試驅動程式的模板,只需要修改模板即可,模板如下 from gnuradio import gr,gr unittest from gnuradio import blocks import double diff swig as double diff class...