首页 > 编程语言 > 详细

python 控制IT6302

时间:2020-06-17 20:55:06      阅读:62      评论:0      收藏:0      [点我收藏+]

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()
       
    
   

 

python 控制IT6302

原文:https://www.cnblogs.com/caiya/p/13154535.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!