Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. """
  2. Python Code for Power Supply
  3. Hamamatsu c11204-02
  4.  
  5. """
  6. import serial
  7. import binascii
  8. import numpy as np
  9. import time
  10. import sys
  11.  
  12. # VARIABLES
  13. V_conversion=1.812*10**(-3)     #voltage conversion factor
  14. I_conversion=4.980*10**(-3)     #current conversion factor (mA)
  15. STX = '02'          #start of text - fixed
  16. ETX = '03'          #end of text - fixed
  17. CR = '0D'           #delimiter - fixed
  18.  
  19. # SERIAL PORT
  20. ser = serial.Serial('/dev/ttyUSB0')     # open serial port - linux
  21. #ser = serial.Serial('COM6')             # open serial port - windows
  22. ser.baudrate = 38400                    # set baudrate
  23. ser.parity = serial.PARITY_EVEN         # set parity
  24. ser.stopbits=serial.STOPBITS_ONE
  25. ser.bytesize=serial.EIGHTBITS
  26. print(ser.name)                         # check which port was really used
  27.  
  28. def convert(command):
  29.     com = command.encode(encoding='utf-8', errors='strict')
  30.     command_str = binascii.hexlify(com).decode('utf-8')
  31.     sum_command = sum(bytearray(com))
  32.     return (command_str, sum_command)
  33.  
  34. def checksum(sum_command,sum_voltage):
  35.     #CHECKSUM CALCULATION
  36.     CS=hex(int(STX,16)+sum_command+int(ETX,16)+sum_voltage)
  37.     CS=CS.lstrip('0x')
  38.     CS=CS.upper()
  39.     CS=CS[-2:]
  40.     CS_str,CS_sum=convert(CS)
  41.     return(CS_str,CS_sum)
  42.  
  43. def setValue(voltage_dec):
  44.     if voltage_dec > 58:
  45.         print ("Voltage is too high")
  46.         sys.exit()
  47.     elif  voltage_dec < 40:
  48.         print ("Voltage is too low")
  49.         sys.exit()
  50.     else:
  51.         print (voltage_dec)
  52.         voltage_conv=float(voltage_dec)/V_conversion
  53.         voltage = int(round(voltage_conv))
  54.         voltage_hex=hex(voltage) #convert voltage from decimal to hexadecimal number
  55.         voltage_hex=voltage_hex.lstrip('0x')
  56.         voltage_str,sum_voltage=convert(voltage_hex)
  57.         command_str,sum_command=convert('HBV')
  58.         CS_str,CS_sum = checksum(sum_command,sum_voltage)
  59.  
  60.     #FINAL COMMAND
  61.     command_tosend = STX + command_str + voltage_str + ETX + CS_str + CR
  62.     command_x =  "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
  63.     print("send command:" + command_x)
  64.     tx = ser.write(command_x.encode())
  65.     rx = ser.read(3)
  66.     print("read command:" + str(rx))
  67.  
  68. def getValue():
  69.     command_str,sum_command=convert('HPO')
  70.     CS_str,CS_sum = checksum(sum_command,0)
  71.  
  72.     #FINAL COMMAND
  73.     command_tosend = STX + command_str + ETX + CS_str + CR
  74.     print(command_tosend)
  75.     command_x =  "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
  76.     print(command_x)
  77.     tx = ser.write(command_x.encode())
  78.     rx = ser.read(28)
  79.     #gets fucked somewhere around here due to python3 conversion
  80.     volt_out= (int(rx[12:16], 16) * V_conversion)
  81.     mA_out = (int(rx[17:21], 16) * I_conversion)
  82.     print("Voltage (V): {0}".format(volt_out))
  83.     print("Current (mA): {0}".format(mA_out))
  84.  
  85. def HighVoltageOn():
  86.     command_str,sum_command=convert('HON')
  87.     CS_str,CS_sum = checksum(sum_command,0)
  88.  
  89.     #FINAL COMMAND
  90.     command_tosend = STX + command_str + ETX + CS_str + CR
  91.     print(command_tosend)
  92.     command_x =  "".join(chr(int(command_tosend[n : n+2],16)) for n in range(0, len(command_tosend), 2))
  93.     print(command_x)
  94.     tx = ser.write(command_x.encode())
  95.     rx = ser.read(3)
  96.     print("read command:" + str(rx))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement