[英]Issue communicating with USB instrument (AlphaLab Gaussmeter) with serial via Python
我正在嘗試通過其 USB 端口與 AlphaLabs GM-2 高斯計 ( https://www.alphalabinc.com/product/gm2/ ) 進行通信,串口為 python。高斯計是一種非常簡單的設備,僅顯示數字值在前面板上測量的磁場。 如果我們可以讀取測量值和 plot 它與時間的關系,我們希望達到目的。
目前,我們在與設備通信時遇到問題,希望得到幫助! 我一直在嘗試按照他們的系統數據采集手冊 ( https://www.alphalabinc.com/wp-content/uploads/2018/02/alphaapp_comm_protocol.pdf )...大路障。
根據手冊,如果我想向設備發送 ID_METER_PROP 命令,我需要向設備提供命令字節:0x01 后跟“內容無關緊要的五個字節”。 這應該給我們一個 ASCII 塊,后跟一個終止字節或一個表示有更多數據的字節。
從我們的代碼中,我們可以獲得一個 ASCII 塊,后跟這個“確認字節”(表明它們是從高斯計發送的更多數據......)但我們似乎無法退出程序來接收所述數據。 一旦我們調用這個程序,它就會凍結高斯計……就像它試圖發送更多數據但就是不能。
感謝您的任何建議!
我曾嘗試聯系 Alpha Labs 的技術支持,但遺憾的是他們無法在預制 GUI 之外提供任何編碼幫助。
'''Python
# Define the command to send to the device
command = serial.to_bytes([0x01, 0x03, 0x03, 0x03, 0x03, 0x03])
#print(command)
# Send command to device and save its return
ret=gaussmeter.getIdentification(command)
print(ret) # print return variable
#-----
#Defined Function getIdentification for reference
#-----
def getIdentification(self, command):
time.sleep(self.DEFAULT_SLEEP_TIME)
self.port.write(command)
identification = self.port.read(self.DEFAULT_READ_SIZE)
test = self.port.read(self.DEFAULT_READ_SIZE)
return identification, test
'''
上面的代碼輸出:(b':METER_NAME=GM2_GAUS\x08', b'')
'\x08 是上面和手冊中定義的“確認字節”。 調用此代碼會凍結高斯計設備,重置它的唯一方法是拔掉它並重新插入。
我們希望看到手冊中定義的更多設備 ASCII 設置,我們絕對不希望設備出現故障
首先,我相信您應該在調用write 之后調用sleep。
並且文檔說,如果您獲得了“確認字節”,則應該重復該過程。
因此,您發送命令,進行讀取,如果收到確認字節,請重復執行。
當 ACKNOWLEDGE (0x08) 在字節 21 中時,您的代碼缺少將 ACKNOWLEDGE (0x08) 發送回高斯計。因此,高斯計在發送更多信息之前凍結等待接收 ACKNOWLEDGE。
這段代碼對我有用:
def getIdentification(self):
time.sleep(self.DEFAULT_SLEEP_TIME)
self.send_cmd(self.ID_METER_PROP)
identification = self.port.read(self.DEFAULT_READ_SIZE)
if (len(identification) == 21):
print(identification[0:20])
more_to_read = (identification[20] == self.ACKNOWLEDGE)
while(more_to_read):
self.send_cmd( self.ACKNOWLEDGE)
identification = self.port.read(self.DEFAULT_READ_SIZE)
print(identification[0:20])
more_to_read = (identification[20] == 0x08)
else:
print("Error reading from gaussmeter port")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.