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()
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?