Last active
February 3, 2024 13:38
-
-
Save mipsparc/802e72547e3007d944dee9f5cbcdecea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import minimalmodbus | |
from serial.tools import list_ports | |
for d in list_ports.comports(): | |
if d.description.startswith('USB Serial'): | |
device = d.device | |
class RM_PP03: | |
def __init__(self, device): | |
self.modbus = minimalmodbus.Instrument(device, 1) | |
self.modbus.serial.baudrate = 115200 | |
self.modbus.serial.timeout = 0.3 | |
def get_volts(self): | |
return self.modbus.read_register(0, 3, 4, True) | |
def get_peak_volts(self): | |
return self.modbus.read_register(1, 3, 4, True) | |
def get_current(self): | |
return self.modbus.read_register(2, 3, 4, True) | |
def get_peak_currrent(self): | |
return self.modbus.read_register(3, 3, 4, True) | |
def set_out_enable(self): | |
self.modbus.write_bit(0, 1, 5) | |
def set_out_disable(self): | |
self.modbus.write_bit(0, 0, 5) | |
def get_out_status(self): | |
return self.modbus.read_bit(0, 1) | |
def set_eb_disable(self): | |
self.modbus.write_bit(1, 0, 5) | |
def get_eb_status(self): | |
return self.modbus.read_bit(1, 1) | |
def set_output(self, way, level): | |
if not 0 <= level <= 32767: | |
raise ValueError('出力範囲(0-32767)を満たしてください') | |
if not type(way) is bool: | |
raise ValueError('方向は真(順方向)偽(逆方向)をboolで与えてください') | |
if not way: | |
level *= -1 | |
self.modbus.write_register(0, level, 0, 6, True) | |
print('register: ', self.modbus.read_register(0, 0, 3)) | |
pp03 = RM_PP03(device) | |
pp03.set_eb_disable() | |
pp03.set_out_enable() | |
print('out: ', pp03.get_out_status()) | |
print('eb: ', pp03.get_eb_status()) | |
way = True | |
while True: | |
level = input('Level(0-32767): ') | |
# なにも入力しなかった場合は方向転換 | |
if level == '': | |
way = not way | |
try: | |
level = int(level) | |
except ValueError: | |
continue | |
pp03.set_output(way, level) | |
print('level: ', level) | |
print('volts: ', pp03.get_volts()) | |
print('current: ', pp03.get_current()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment