使用Python控制仅通过Hyperterminal交互的设备

1 投票
1 回答
1412 浏览
提问于 2025-04-16 11:06

我正在尝试写一个基本的驱动程序,用来控制一个只能通过超终端(hyperterminal)进行交互的设备。这个设备的所有输入都需要特定格式的数据,而它返回的数据则包含很多等号、换行符和空格,以便让人看起来更清晰。我觉得返回的数据可能是出问题的地方,因为我经常遇到异常,但我不太确定还有什么更好的处理方法。有没有人知道更好的解决办法?

设备手册: http://us.startech.com/media/products/PCM815SHNA/Manuals/PCM815SHNA.pdf

import visa
import re


class iopower8(visa.SerialInstrument):
    #initialization
    def __init__(self,loc):
        visa.SerialInstrument.__init__(self,loc)
        self.term_chars = '\r' #set terminal characters
        self.write('\r\r') #start faux 'Hyperterminal'

    def on(self, bank, out):
        self.ask("on " + str(bank) + " " + str(out))
        for i in range (1,3):#read buffer into void to prevent issues
            try:
                self.read_raw()
            except(visa_exceptions.VisaIOError):
                self.buffer_clear()
                break
        return self.status(bank, out)

    def off(self, bank, out):
        self.ask("of " + str(bank) + " " + str(out))
        for i in range (1,3):#read buffer into void to prevent issues
            try:
                self.read_raw()
            except(visa_exceptions.VisaIOError):
                self.buffer_clear()
                break
        return self.status(bank, out)

    def status(self, bank, port): #enter bank and port # you want to check
        self.ask("st " + str(bank))
        result = 'Error' #send error message regardless
        for i in range (1,13):#all 12 lines need to be read out of the buffer to prevent issues later
            try:
                stuff = self.read()#read the line to a holding srting, and dump in void if wriong line to clear buffer
                if stuff.strip() == ('='*44):
                    break
            except(visa_exceptions.VisaIOError):
                break
        for i in range(1,9):
            try:
                stuff = self.read()#read the line to a holding string, and dump in void if wriong line to clear buffer.
                if i == port: #this offset will get you to the line with the relevant port's status
                    result = re.match('(.*?)(ON|OFF)', stuff) #regex to the find the on/off status
                    #convert to boolean
                    if result.group(2) == 'ON':
                        result = True
                    elif result.group(2) =='OFF':
                        result = False
                    else:
                        result = 'ERROR'
            except(visa_exceptions.VisaIOError):
                self.buffer_clear()
                break
        return result

    def buffer_clear(self): #in case of buffer jamming
        while True:
            try:
                self.read_raw()
            except(visa_exceptions.VisaIOError):
                break

    def all_on(self, bank):
        self.ask("on " + str(bank) + " 0")
        for i in range (1,3):#read buffer into void to prevent issues
            try:
                hold = self.read_raw()
            except(visa_exceptions.VisaIOError):
                self.buffer_clear()
                break

    def all_off(self, bank):
        self.ask("of " + str(bank) + " 0")
        for i in range (1,3):#read buffer into void to prevent issues
            try:
                self.read_raw()
            except(visa_exceptions.VisaIOError):
                self.buffer_clear()
                break

1 个回答

0

HiperTerminal 没有什么特别的地方。换行符通常是 '\r\n' 或者单独的 '\n'

撰写回答