python 控制IT6302(serial)

IT6302有编程文档,里面有控制命令,python只需要调用串口,然后发送对应命令给串口,即可控制IT6302,并且可以通过串口读取相应的返回值。

可行源码:

import serial

# system
get_id = '*idn?\n'
set_remote_ctrl = 'SYST:REM\n'
get_sys_version = 'SYST:VERS?\n'
# ch control
select_ch1 = 'INST FIRst\n'
select_ch2 = 'INST SECOnd\n'
select_ch3 = 'INST THIrd\n'

open_output = 'OUTP 1\n'#打开电源输出
close_output = 'OUTP 0\n'#关闭电源输出


class PowerSupplyCtrl:    
    def __init__(self, com_port):
        self.com = com_port
        self.send_cmd(set_remote_ctrl)
        self.send_cmd(get_id)
        print(self.get_cmd_return())
        self.send_cmd(get_sys_version)
        print(self.get_cmd_return())
        self.send_cmd(close_output)

    def send_cmd(self, cmd):
        self.com.write(cmd.encode('ascii'))    

    def get_cmd_return(self):
        return self.com.readline().decode('utf-8','ignore')

    def select_ch(self, ch):#选择当前通道
        if ch == 1:
            self.send_cmd(select_ch1)        
        elif ch == 2:
            self.send_cmd(select_ch2)
        elif ch == 3:
            self.send_cmd(select_ch3)
        else:
            print('error ch')
            self.set_output_voltage('0')
            return False            
        self.send_cmd(open_output)
        return True

    def set_output_voltage(self, set_voltage):#设定当前通道电压值
        voltage = str(set_voltage)
        temp = 'VOLT ' + voltage + 'V\n'
        print('set output voltage = ' + voltage + 'V')
        self.send_cmd(temp)

    def set_output_current(self, set_current):#当前通道设定电流值
        current = str(set_current)
        temp = 'CURR ' + current + 'A\n'
        print('set output current = ' + current + 'A')
        self.send_cmd(temp)

    def stop_power_supply(self):
        self.send_cmd(close_output)
        self.com.close()      


if __name__ == "__main__":     
    com =serial.Serial(port = 'COM3', baudrate = 9600, timeout = 1)
    power = PowerSupplyCtrl(com)

    power.select_ch(2)
    power.set_output_voltage(5)
    power.set_output_current(1)
    power.send_cmd(close_output)

    power.send_cmd('SYST:LOC\n')
    power.send_cmd('SYSTem:BEEPer\n')
    power.stop_power_supply()
       
    
   

 

posted @ 2020-06-17 20:14  菜芽caiya  阅读(1045)  评论(0编辑  收藏  举报