• python 控制IT6302(serial)


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

    可行源码:

    import serial
    
    # system
    get_id = '*idn?
    '
    set_remote_ctrl = 'SYST:REM
    '
    get_sys_version = 'SYST:VERS?
    '
    # ch control
    select_ch1 = 'INST FIRst
    '
    select_ch2 = 'INST SECOnd
    '
    select_ch3 = 'INST THIrd
    '
    
    open_output = 'OUTP 1
    '#打开电源输出
    close_output = 'OUTP 0
    '#关闭电源输出
    
    
    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
    '
            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
    '
            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
    ')
        power.send_cmd('SYSTem:BEEPer
    ')
        power.stop_power_supply()
           
        
       
  • 相关阅读:
    C++基础学习笔记----第十三课(操作符重载-下)
    SSH搭建
    java(17)
    英语语法检查软件
    Ubuntu sudo apt-get 安装下载更新软件包命令详解
    MATLAB:控制系统模型变换
    solvepnp
    MATLAB:控制系统模型变换
    MATLAB实现传递函数
    visual studio编译cmake opencv一直出错
  • 原文地址:https://www.cnblogs.com/caiya/p/13154535.html
Copyright © 2020-2023  润新知