Thursday, May 30, 2013

bk precision 1786B Power supply - set and read voltage and current python script

#----- python ----


import sys
import serial
import time
import string
import binascii
import time
import os

try:

 filename1 = "Power-Consumption"+time.strftime("%Y-%m-%d-%H%M%S")+".csv"
 print filename1
 on_time = 1 # in seconds
 mac_v = 12
 actual_v = 12
 actual_a = 3   # Amp
 sleep_cmd_int = 0.1 # time for gap between commands
 v1 = 0 # currect mA
 a1 = 0 # voltage V

 ser = serial.Serial('COM14',38400,timeout=1)
 print ser.portstr


#------------- enable remote management ---------------------------
 a0 = 'AA';a1 = '00';a2 = '20';a3 = '01';a4 = '00';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")

# ---- sleep ---

 time.sleep(sleep_cmd_int)

#-------------------Set V---------------------

 a0 = 'AA';a1 = '00';a2 = '23';a3 = 'E0';a4 = '2E';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")

# ----

 time.sleep(sleep_cmd_int)

#-----------------------set the current---------------------

 a0 = 'AA';a1 = '00';a2 = '24';a3 = 'B8';a4 = '0B';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")
# ----

 time.sleep(sleep_cmd_int)

# -------   turn on ---
 a0 = 'AA';a1 = '00';a2 = '21';a3 = '01';a4 = '00';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")

# ------------

 time.sleep(on_time)

#---- flush ---

 ser.flush()
 while 1==1:
  ser.flush()

# ----------read current -------------

  a0 = 'AA';a1 = '00';a2 = '26';a3 = 'B8';a4 = '0B';a5 = '00';a6 = '00'
  a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
  a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
  a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

  btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

  cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

  btsend1[25] = int(cksum1,16)

  s = bytearray(btsend1)

  ser.write(s)
  ser.write("\r\n")

# ---- sleep ---

  time.sleep(.1)

# --- flush ---

  ser.flush()

# ---- sleep ---

  time.sleep(.1)

  msg2 = ser.read(1000)

  btreceived1 = list(msg2)
#print btreceived1
  if '\x26' in btreceived1:
   a11 = btreceived1[btreceived1.index('\x26')+1:btreceived1.index('\x26')+3]
   a12 = a11[1].encode('hex')+a11[0].encode('hex')
   a1 = int(a12,16)
   v11 = btreceived1[btreceived1.index('\x26')+3:btreceived1.index('\x26')+7]
   v12 = v11[3].encode('hex')+v11[2].encode('hex')+v11[1].encode('hex')+v11[0].encode('hex')
   print "..."
   print str(a1)+"mA"+" @ "+str(float(int(v12,16))/1000)+"V"+" - "+time.strftime("%Y-%m-%d %H:%M:%S")
   writettoofile = str(a1)+","+str(float(int(v12,16))/1000)+","+time.strftime("%Y%m%d%H%M%S")+"\n"
  #--- open/close file ---
   f1 = open(filename1,'a',0)
   f1.write(writettoofile)
   f1.close

#--- sleep ---
   time.sleep(1)

except:
 print "Something went wrong"
#--- sleep ---
 time.sleep(2)
#----- turn off -------
 a0 = 'AA';a1 = '00';a2 = '21';a3 = '00';a4 = '00';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")


#--------------Disable remote -------


 a0 = 'AA';a1 = '00';a2 = '20';a3 = '00';a4 = '00';a5 = '00';a6 = '00'
 a7 = '00';a8 = '00';a9 = '00';a10 = '00';a11 = '00';a12 = '00';a13 = '00'
 a14 = '00';a15 = '00';a16 = '00';a17 = '00';a18 = '00';a19 = '00';a20 = '00'
 a21 = '00';a22 = '00';a23 = '00';a24 = '00';a25 = '00'

 btsend1 = [int(a0,16),int(a1,16),int(a2,16),int(a3,16),int(a4,16),int(a5,16),int(a6,16),int(a7,16),int(a8,16),int(a9,16),int(a10,16),int(a11,16),int(a12,16),int(a13,16),int(a14,16),int(a15,16),int(a16,16),int(a17,16),int(a18,16),int(a19,16),int(a20,16),int(a21,16),int(a22,16),int(a23,16),int(a24,16),int('00',16)]

 cksum1 = hex(sum(btsend1))[len(hex(sum(btsend1)))-2:len(hex(sum(btsend1)))]

 btsend1[25] = int(cksum1,16)

 s = bytearray(btsend1)

 ser.write(s)
 ser.write("\r\n")





No comments: