Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Python Code for Power Supply
- Hamamatsu c11204-02
- """
- import serial
- import binascii
- import numpy as np
- import time
- import sys
- # VARIABLES
- V_conversion=1.812*10**(-3) #voltage conversion factor
- I_conversion=4.980*10**(-3) #current conversion factor (mA)
- STX = '02' #start of text - fixed
- ETX = '03' #end of text - fixed
- CR = '0D' #delimiter - fixed
- # SERIAL PORT
- ser = serial.Serial('/dev/ttyUSB0') # open serial port - linux
- #ser = serial.Serial('COM6') # open serial port - windows
- ser.baudrate = 38400 # set baudrate
- ser.parity = serial.PARITY_EVEN # set parity
- ser.stopbits=serial.STOPBITS_ONE
- ser.bytesize=serial.EIGHTBITS
- print(ser.name) # check which port was really used
- def convert(command):
- com = command.encode(encoding='utf-8', errors='strict')
- command_str = binascii.hexlify(com).decode('utf-8')
- sum_command = sum(bytearray(com))
- return (command_str, sum_command)
- def checksum(sum_command,sum_voltage):
- #CHECKSUM CALCULATION
- CS=hex(int(STX,16)+sum_command+int(ETX,16)+sum_voltage)
- CS=CS.lstrip('0x')
- CS=CS.upper()
- CS=CS[-2:]
- CS_str,CS_sum=convert(CS)
- return(CS_str,CS_sum)
- def setValue(voltage_dec):
- if voltage_dec > 58:
- print ("Voltage is too high")
- sys.exit()
- elif voltage_dec < 40:
- print ("Voltage is too low")
- sys.exit()
- else:
- print (voltage_dec)
- voltage_conv=float(voltage_dec)/V_conversion
- voltage = int(round(voltage_conv))
- voltage_hex=hex(voltage) #convert voltage from decimal to hexadecimal number
- voltage_hex=voltage_hex.lstrip('0x')
- voltage_str,sum_voltage=convert(voltage_hex)
- command_str,sum_command=convert('HBV')
- CS_str,CS_sum = checksum(sum_command,sum_voltage)
- #FINAL COMMAND
- command_tosend = STX + command_str + voltage_str + ETX + CS_str + CR
- command_x = "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
- print("send command:" + command_x)
- tx = ser.write(command_x.encode())
- rx = ser.read(3)
- print("read command:" + str(rx))
- def getValue():
- command_str,sum_command=convert('HPO')
- CS_str,CS_sum = checksum(sum_command,0)
- #FINAL COMMAND
- command_tosend = STX + command_str + ETX + CS_str + CR
- print(command_tosend)
- command_x = "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
- print(command_x)
- tx = ser.write(command_x.encode())
- rx = ser.read(28)
- #gets fucked somewhere around here due to python3 conversion
- volt_out= (int(rx[12:16], 16) * V_conversion)
- mA_out = (int(rx[17:21], 16) * I_conversion)
- print("Voltage (V): {0}".format(volt_out))
- print("Current (mA): {0}".format(mA_out))
- def HighVoltageOn():
- command_str,sum_command=convert('HON')
- CS_str,CS_sum = checksum(sum_command,0)
- #FINAL COMMAND
- command_tosend = STX + command_str + ETX + CS_str + CR
- print(command_tosend)
- command_x = "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
- print(command_x)
- tx = ser.write(command_x.encode())
- rx = ser.read(3)
- print("read command:" + str(rx))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement