py_mc-usb-sp4t-63/SP4T.py

227 lines
6.3 KiB
Python

# Title: Python bindings for direct USB programming of the Mini-Circuits USB-SP4T-63
# Author: George 'Joj' Travers
#
# Reference: https://www.minicircuits.com/softwaredownload/Prog_Manual-Solid_State_Switch.pdf
#
# command data is sent and recieved in decimal for some reason, don't ask why idk
# ex: command 205_1 refers to the first two bytes sent in the 64-byte transmit array
def get_device_model(dev):
# Returns the Mini-Circuits part number of the connected switch
# command 40
name = ""
command = [40]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 40 :
return "Error: Invalid response"
for i in range(1, len(ret_arr)) :
if ret_arr[i] == 0 :
break
name += chr(ret_arr[i])
return name
def get_device_serial(dev):
# Returns the serial number of the connected switch
# command 41
serial = ""
command = [41]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 0x29 :
return "Error: Invalid response"
for i in range(1, len(ret_arr)) :
if ret_arr[i] == 0 :
break
serial += chr(ret_arr[i])
return serial
def set_sp4t_state(dev, port):
# Sets the state of the SP4T switch
# command 1-4
command = [port]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != port :
return "Error: Invalid response"
return port
def get_sp4t_state(dev):
# Returns the state of the SP4T switch
# command 15
command = [15]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 15 :
return f"Error: Invalid response: {ret_arr[0]}"
return ret_arr[1]
def get_firmware(dev):
# Returns the internal firmware version of the switch box
# command 99
command = [99]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 99 :
return "Error: Invalid response"
return chr(ret_arr[5]) + chr(ret_arr[6])
def set_num_switching_steps(dev, steps):
# Sets the number of steps to be configured for the pre-defined switching sequence
# command 204_0
command = [204,0,steps]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return f"{steps} steps set"
def get_num_switching_steps(dev):
# Returns the number of steps in the switching sequence
# command 205_0
command = [205,0]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 205 :
return "Error: Invalid response"
return f"Number of steps: {ret_arr[1]}"
def set_switching_step(dev, step_index, switch_state, dwell_0, dwell_1, dwell_units):
# Configures the state and dwell time of a single step within the pre-defined switching sequence
# command 204_1
command = [204,1,step_index,switch_state,dwell_0,dwell_1,dwell_units]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return "Switching step configured"
def get_switching_step(dev, step_index):
# Returns the state and dwell time of a single step within the pre-defined switching sequence
# command 205_1
command = [205,1,step_index]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 205 :
return "Error: Invalid response"
return ret_arr[:5]
def set_switching_direction(dev, direction):
# Sets the direction in which the sequence of switch states which will be executed
# command 204_2
command = [204,2,direction]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return "Switching direction configured"
def get_switching_direction(dev):
# Returns the direction in which the sequence of switch states will be executed
# command 205_2
command = [205,2]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 205 :
return "Error: Invalid response"
if ret_arr[1] == 0 :
return "Forward"
elif ret_arr[1] == 1 :
return "Reverse"
elif ret_arr[1] == 2 :
return "Bi_Directional"
else :
return "Error: Invalid response"
def set_num_switching_cycles(dev, cycles_0, cycles_1):
# Sets the number of times that the complete switching sequence will be executed
# This setting will beignored if the switch sequence has been configured to execute in continuous mode
# command 204_4
command = [204,4,cycles_0,cycles_1]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return "Switching cycles configured"
def get_num_switching_cycles(dev):
# Returns the number of times that the complete switching sequence will be executed
# This setting will be ignored if the switch sequence has been configured to execute in continuous mode
# command 205_4
command = [205,4]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 205 :
return "Error: Invalid response"
return ret_arr[:3]
def set_switching_mode(dev, mode):
# Configures whether the switch sequence will be executed continuously or for a pre-defined number of cycles
# With continuous mode enabled, the sequence will repeat from the time the sequence is enabled by the user until the time it is disabled by the user; the setting for number of cycles will be ignored
# With continuous mode disabled the sequence will repeat according to the setting for number of cycle
# command 204_3
command = [204,3, mode]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return "Switching mode set"
def get_switching_mode(dev):
# Indicates whether or not the switching sequence is configured to operate in continuous mode
# With continuous mode enabled, the sequence will repeat from the time the sequence is enabled by the user until the time it is disabled by the user; the setting for number of cycles will be ignored
# With continuous mode disabled the sequence will repeat according to the setting for number of cycles.
# command 205_3
command = [205,3]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 205 :
return "Error: Invalid response"
return f"Continuous mode: {ret_arr[1]}"
def start_stop_switching_mode(dev, mode):
# Starts or stops the pre-defined switching sequence
# The sequence will not operate unless all required parameters have been configured correctly
# command 204_5
command = [204,5,mode]
dev.write(0x01,command)
ret_arr = dev.read(0x81,64)
if ret_arr[0] != 204 :
return "Error: Invalid response"
return "Switching sequence set"