Attachment 'hp1630.py'

Download

   1 #! /usr/bin/env python
   2 """
   3 # hp1631.py - Test for GPIB_USB converter
   4 # 2011-06-28 RR
   5 # 2011-07-10 RR, added option filename, printall
   6 # Use with R8C/13 USB-GPIB interface from Elektor 04-2011
   7 """
   8 
   9 import array
  10 import glob
  11 import optparse
  12 import os
  13 import serial
  14 import sys
  15 import time
  16 
  17 # on which port should the tests be performed
  18 # http://sourceforge.net/projects/osx-pl2303/
  19 
  20 LINEFEED = '\x0A'   # CTRL+J
  21 TAB = '\x1B'
  22 
  23 def scan():
  24     """scan for available ports. return a list of device names."""
  25     return glob.glob('/dev/cu.*')
  26 
  27 def ser_answer():
  28     """Read the answer after sending a command to the USB-GPIB converter."""
  29     answer = ser.read(30)
  30     if options.verbose:
  31         if (len(answer) == 1):
  32             print "Answer: {0:#04X}".format(ord(answer))
  33         elif (len(answer) > 1):
  34             print "Answer: " + answer
  35     if (len(answer) > 1):
  36         if "Error 1" in answer:
  37             print "GBIP Device (Address) not found"
  38         elif "Error 2" in answer:
  39             print "GPIB Timeout Error"
  40         else:
  41             print answer
  42         ser.close()
  43         sys.exit(6)
  44 
  45 if __name__=='__main__':
  46 
  47     port = "/dev/cu.PL2303-0000103D"
  48     # for debug only, command line argument
  49     #sys.argv = ['hp1630_cmd.py', '-p']
  50     
  51     parser = optparse.OptionParser(
  52         usage = "%prog [options] [port [baudrate]] version 1.0", 
  53         description = "hp1630 - A remote control program with R8C/13 GPIB_USB converter."
  54     )
  55     parser.add_option("-a", "--printall", dest="printall", action="store_true",
  56                       help="print all")
  57     parser.add_option("-b", "--beep", dest="beep", action="store_true",
  58                       help="actuate GPIB device beep")
  59     parser.add_option("-d", "--date", dest="date", action="store_true",
  60                       help="set date to hp1630")
  61     parser.add_option("-f", "--file", dest="filename",
  62                       help="Enter filename")
  63     parser.add_option("-g", "--gport", dest="gport",
  64                       help="serial port, e.g. /dev/cu.xxx")
  65     parser.add_option("-i", "--id", action="store_true", dest="id",
  66                       help="check for GPIB device ID")
  67     parser.add_option("-l", "--local", action="store_true", dest="local",
  68                       help="switch GPIB device to local mode")
  69     parser.add_option("-p", "--print", dest="printWS", action="store_true",
  70                       help="print waveform screen")
  71     #parser.add_option("-q", "--quiet",
  72     #                  action="store_false", dest="verbose", default=True,
  73     #                  help="don't print status messages to stdout")
  74     parser.add_option("-r", "--restore", action="store_true", dest="restore",
  75                       help="restore Learn String File, enter Filename with -f")
  76     parser.add_option("-t", "--timeout", dest="timeout",
  77                       help="Enter timeout in sec.")
  78     parser.add_option("-u", "--usb", action="store_true", dest="usb",
  79                       help="check for USB-GPIB interface")
  80     parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
  81                       help="tell more internal values for debugging")
  82 
  83     (options, args) = parser.parse_args()
  84 
  85     if (len(sys.argv) < 2):  # no arguments given
  86         parser.print_help()
  87         print "Default Serial Port: " + port
  88 
  89 # open USB-serial port
  90     if options.gport:
  91         port = options.gport
  92     otimeout = 1              # 1 sec
  93     if options.timeout:
  94         otimeout = int(options.timeout)
  95     try:
  96         ser = serial.Serial(port, baudrate=38400, timeout=otimeout)
  97         if options.verbose:
  98             print ser.name
  99         ser.flushInput()
 100     except serial.SerialException, e:
 101         sys.stderr.write("could not open port %r: %s\n" % (port, e))
 102         print "Found ports:"
 103         for name in scan():
 104             print "  "+name
 105         sys.exit(1)
 106 
 107     if options.printall:
 108         fname = "printall.txt"
 109         if options.filename:
 110             fname = options.filename
 111         # read pint screen from List Menu
 112         ser.write("E4,LM;CD;PA\n")
 113         hello = ser.read(1) # skip LineFeed, if there
 114         screen = ser.read(100000)
 115         #if screen[0] == "*":  # LineFeed was not there
 116         #    screen = TAB + screen
 117         if (len(screen) < 1000):
 118             print "len too short: " + str(len(screen))
 119             ser.write("B4,1\n") # Go To Local
 120             ser_answer()
 121             ser.close
 122             sys.exit(2)
 123         if options.verbose:
 124             print len(screen)
 125         # write text file
 126         try:
 127             f = file(fname, 'w')
 128             f.write(screen)
 129             f.close()
 130         except IOError, e:
 131             sys.stderr.write('ERROR opening file %s: %s \n' % (fname_tmp, e))
 132             ser.close()
 133             sys.exit(3)
 134         # Go To Local
 135         ser.write("B4,1\n")
 136         ser_answer() 
 137         
 138     if options.beep:
 139         if options.verbose:
 140             print "Beep"
 141         ser.write("W4,BP\n")
 142         ser_answer()
 143         # Go To Local
 144         ser.write("B4,1\n")
 145         ser_answer()
 146         
 147     if options.date:
 148         # Set date and time
 149         now = time.strftime('%m%d%Y%H%M%S')
 150         if options.verbose:
 151             print now
 152         month = int(now[0:2])
 153         datestr = now[2:]
 154         ser.write("W4,SM;NX;CD;CE\n")
 155         ser_answer()
 156         if month > 1:
 157                 ser.write("W4,NX"+str(month-1)+"\n")
 158                 ser_answer()  
 159         ser.write('W4,CR;"'+datestr+'"\n')
 160         ser_answer()
 161         # Go To Local
 162         ser.write("B4,1\n")
 163         ser_answer()
 164 
 165     if options.id:
 166         if options.verbose:
 167             print "ID"
 168         ser.write("E4,ID\n")
 169         hello = ser.read(30)
 170         print hello
 171         # Go To Local
 172         ser.write("B4,1\n")
 173         ser_answer()
 174 
 175     if options.local:
 176         if options.verbose:
 177             print "Go To Local"
 178         ser.write("B4,1\n")
 179         ser_answer()
 180 
 181     if options.printWS:
 182         fname = raw_input("filename (.pbm): ")
 183         fname_tmp = "screenWM.hpg"
 184         if options.verbose:
 185             print "Temporary file: " + fname_tmp
 186         
 187         # read pint screen from Waveform Menu
 188         ser.write("E4,WM;CD;PR\n")
 189         hello = ser.read(1) # skip LineFeed, if there
 190         screen = ser.read(50000)
 191         if screen[0] == "*":  # LineFeed was not there
 192             screen = TAB + screen
 193         if (len(screen) < 1000):
 194             print "len too short: " + str(len(screen))
 195             ser.close
 196             sys.exit(2)
 197         if options.verbose:
 198             print len(screen)
 199         # write temporary file
 200         try:
 201             f = file(fname_tmp, 'wb')
 202             f.write(screen)
 203             f.close()
 204         except IOError, e:
 205             sys.stderr.write('ERROR opening file %s: %s \n' % (fname_tmp, e))
 206             ser.close()
 207             sys.exit(3)
 208         # Go To Local
 209         ser.write("B4,1\n")
 210         ser_answer()      
 211         
 212         cur_row = 0
 213         width = -1
 214         gr_data = array.array('c')
 215 
 216         try:
 217             f = file(fname_tmp, 'rb')  # read binary
 218             statinfo = os.stat(fname_tmp)
 219             fdate = time.strftime('%Y-%m-%d %H%:%M:%S', time.localtime(statinfo.st_mtime))
 220             if options.verbose:
 221                 print fdate
 222         
 223             while (1):
 224                 buf = f.read(6)  # read header ??? ValueError: I/O operation on closed file
 225                 if (buf[1:3] != "*b"):
 226                     break
 227                 w = int(buf[3:5])  # line byte width
 228                 if (width == -1):
 229                     width = w
 230                 gr_data.fromfile(f, width)
 231                 cur_row += 1
 232                 #if (cur_row%10 == 0): print cur_row
 233 
 234             if options.verbose:
 235                 print "Bitmap geometry x, y: " + str(width*8) + " " + str(cur_row)
 236             f.close()
 237         except IOError, e:
 238             sys.stderr.write('ERROR opening file %s: %s \n' % (fname_tmp, e))
 239             ser.close()
 240             sys.exit(4)
 241         # write PBM file
 242         # Def: http://en.wikipedia.org/wiki/Netpbm_format
 243         #f = file(fname[0:-3] + 'pbm', 'wb')  # write binary
 244         try:
 245             f = file(fname + '.pbm', 'wb')  # write binary
 246             f.write('P4\n')                 # write Header
 247             f.write("# from HP1631D, " + fdate + "\n")
 248             f.write(str(width*8) + " " + str(cur_row) + "\n")
 249             gr_data.tofile(f)
 250             f.close()
 251         except IOError, e:
 252             sys.stderr.write('ERROR opening file %s: %s \n' % (fname, e))
 253             ser.close()
 254             sys.exit(4)
 255 
 256     if options.restore:
 257         # Restore reverse assembler
 258         #revasm = 'IHPIBi2.bin'
 259         revasm = options.filename
 260         if options.verbose:
 261             print "File Reverse Assembler: " + revasm
 262         try:
 263             f = file(revasm, 'r')
 264             resbuf = f.read(10000)
 265             f.close()
 266             if options.verbose:
 267                 print len(resbuf)
 268                 print resbuf[0:2]
 269         except IOError, e:
 270             sys.stderr.write('ERROR opening file %s: %s \n' % (revasm, e))               
 271 
 272         # setup Listerner
 273         ser.write("L4,\n")
 274         ser_answer()
 275 
 276         # Output data
 277         for i in range(0, len(resbuf)):
 278             ser.write("D"+"\n"+resbuf[i])
 279             hello = ser.read(1)
 280             if len(hello) == 0:
 281                 print "i=" + str(i)
 282                 break
 283             if (resbuf[i] != hello):
 284                 print "Error compare buf 0x%X ret 0x%X  i=%d" % (ord(resbuf[i]), ord(hello), i)
 285             #print hello
 286             
 287         # End transmittion with LF+EOI+UNL+UNT
 288         ser.write("U\n")
 289         ser_answer()
 290         
 291     if options.usb:
 292         ser.write("I\n")
 293         if options.verbose:
 294             print "Output I, expect answer: GPIB/USB converter V1.1"
 295         hello = ser.read(30)
 296         if (len(hello) < 10):
 297             print "R8C/13 USB-GPIB interface is not available"
 298             ser.close()
 299             sys.exit()
 300         else:       
 301             print hello
 302 
 303 ser.close()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-05-08 04:53:00, 10.4 KB) [[attachment:GPIB_USB.c]]
  • [get | view] (2013-05-08 04:53:00, 50.3 KB) [[attachment:GPIB_USB.pdf]]
  • [get | view] (2013-05-08 04:53:00, 212.3 KB) [[attachment:GPIB_USB.png]]
  • [get | view] (2013-05-08 04:53:00, 332.9 KB) [[attachment:GPIB_USB.sch]]
  • [get | view] (2013-05-08 04:53:00, 52.1 KB) [[attachment:GPIB_USB_Kabel1_DSC01698.jpg]]
  • [get | view] (2013-05-08 04:53:00, 53.6 KB) [[attachment:GPIB_USB_Kabel2_DSC01699.jpg]]
  • [get | view] (2013-05-08 04:53:00, 102.5 KB) [[attachment:GPIB_USB_Patine_US_DSC01703.jpg]]
  • [get | view] (2013-05-08 04:53:00, 96.9 KB) [[attachment:GPIB_USB_Platine_OB_DSC01704.jpg]]
  • [get | view] (2013-05-08 04:53:00, 69.8 KB) [[attachment:GPIB_USB_Stecker5V_DSC01701.jpg]]
  • [get | view] (2013-05-08 04:53:00, 5.7 KB) [[attachment:GPIB_functions.c]]
  • [get | view] (2013-05-08 04:53:00, 78.6 KB) [[attachment:Release.zip]]
  • [get | view] (2013-05-08 04:53:00, 9.8 KB) [[attachment:hp1630.py]]
  • [get | view] (2007-04-13 09:25:00, 1.0 KB) [[attachment:hp1630_HC_GPIBf.txt]]
  • [get | view] (2013-05-08 04:53:00, 10.6 KB) [[attachment:txo10MHz_BNC.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.