Attachment 'dso150-110-plot.py'

Download

   1 #! /usr/bin/env python3
   2 """
   3 # DSO150-110-plot.py - capture DSO150 data and plot
   4 # 2017-11-27 Rudolf Reuter, version 1.0, Python 2/3
   5 # 2017-12-05 Rudolf Reuter, version 1.1
   6 # 2018-07-20 Rudolf Reuter, version 1.2, Py2:sampleIntvl = tNum / float(tFactor)
   7 # waveform data are just time resolution (s) and 12 bit data (1024)
   8 # firmware 110 and newer
   9 #
  10 # This software is public domain.
  11 """
  12 from __future__ import print_function # make "print" Python 2/3 compatible
  13 import glob
  14 import locale
  15 import optparse
  16 import os
  17 import platform
  18 import serial
  19 from subprocess import call
  20 import sys
  21 import time
  22 
  23 # make the program universal for Python 2 and 3
  24 from sys import version_info
  25 python2 = False
  26 if version_info.major == 2:
  27     python2 = True
  28 
  29 # on which port should the tests be performed
  30 # http://sourceforge.net/projects/osx-pl2303/
  31 
  32 # for USB port use, adopt to your hardware
  33 port = ""
  34 os_name = platform.system()
  35 if os_name == "Windows":
  36     port = "COM3:"
  37 if os_name == "Linux":
  38     port = "/dev/ttyUSB0"
  39 if os_name == "Darwin": # Mac OS X
  40     #port = "/dev/cu.usbserial-A4009RFD"
  41     #port = "/dev/cu.PL2303-0000103D"
  42     #port = "/dev/cu.wchusbserial14a120"  # CH340
  43     port = "/dev/cu.SLAB_USBtoUART"  # CP2102
  44 # for debug only, command line argument
  45 #sys.argv = ['dso150_p23.py', '-p']
  46 
  47 
  48 parser = optparse.OptionParser(
  49     usage = "%prog [options] [port [baudrate]] version 1.2", 
  50     description = "DSO150-110-plot.py - capture DSO150 data."
  51 )
  52 parser.add_option("-d", "--data", dest="data", action="store_true",
  53                   help="data plot")
  54 parser.add_option("-f", "--file", dest="filename",
  55                   help="Enter filename")
  56 parser.add_option("-g", "--gport", dest="gport",
  57                   help="serial port, e.g. /dev/cu.xxx")
  58 parser.add_option("-p", "--print", dest="printWS", action="store_true",
  59                   help="print waveform screen")
  60 #parser.add_option("-q", "--quiet",
  61 #                  action="store_false", dest="verbose", default=True,
  62 #                  help="don't print status messages to stdout")
  63 parser.add_option("-t", "--timeout", dest="timeout",
  64                     help="Enter timeout in sec.")
  65 parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
  66                   help="tell more internal values for debugging")
  67 
  68 (options, args) = parser.parse_args()
  69 
  70 if (len(sys.argv) < 2):  # no arguments given
  71     parser.print_help()
  72     print("Default Serial Port: " + port)
  73 
  74 if options.printWS:
  75     # open USB-serial port, if waveform transfer is needed
  76     if options.gport:
  77         port = options.gport
  78     otimeout = 1              # 1 sec
  79     if options.timeout:
  80         otimeout = int(options.timeout)
  81     try:
  82         ser = serial.Serial(port, baudrate=115200, timeout=otimeout)
  83         if options.verbose:
  84             print(ser.name)
  85         ser.flushInput()
  86         #time.sleep(2)  # skip Arduino DTR toogle RESET
  87         print("For DSO150 firmware 111 and newer ONLY")
  88     except:
  89         sys.stderr.write("could not open port %r\n" % (port))
  90         if os_name == "Darwin": # Mac OS X
  91             print ("Found ports:")
  92             for name in glob.glob('/dev/cu.*'): # show available ports
  93                 print("  "+name)
  94         if os_name == "Linux":
  95             for name in glob.glob('/dev/ttyUSB*'): # show available ports
  96                 print("  "+name)
  97         sys.exit(1)
  98         
  99     
 100 if options.printWS:  # Python 2/3 OK
 101     # find out decimal point character
 102     locale.setlocale(locale.LC_ALL, '')
 103     conv = locale.localeconv()
 104     dp =  conv["decimal_point"]
 105     #print(dp)
 106     print ("Push DSO150 ADJ + V/DIV.")
 107     
 108     # wait for data
 109     nwait = 60 # 60 seconds
 110     ncount = 1
 111     dsoHeader =[]  # 19 lines
 112     dsoData = []   # 1024 lines
 113     
 114     while (nwait > 0):
 115         sdat = ser.readline()  # includes CR + LF
 116         if len(sdat) < 1:
 117             nwait -= 1
 118             print (nwait)
 119         else:
 120             if ncount < 20:
 121                 # convert byte array to string
 122                 dsoHeader.append(sdat.decode("utf-8"))
 123                 ncount += 1
 124             else:
 125                 ncount += 1
 126                 dsoData.append(sdat.decode("utf-8"))
 127                 for i in range(0,1024,1):
 128                     sdat = ser.readline()
 129                     dsoData.append(sdat.decode("utf-8"))
 130                 break
 131             #sys.exit(0)
 132     if nwait < 1:
 133         sys.exit(0)
 134         
 135     # produce csv file
 136     fname = "dso150-110-data.csv"
 137     if options.filename:
 138         fname = options.filename
 139     if options.verbose:
 140         print("DSO150 csv file: " + fname)
 141     f = open(fname, "w")
 142     now = time.strftime('%Y-%m-%d %H:%M:%S')
 143     if options.verbose:
 144         print(now)
 145         
 146     # write Header to file
 147     title = "File: " + fname + " " + now + "\n"
 148     f.write(title)
 149     for i in range(0,19,1):
 150         datLine = dsoHeader[i]
 151         f.write(datLine)
 152         
 153     # calculate Sample Interval
 154     timeFactor = {"s":1, "ms":1000, "us":1000000, "ns":1000000000}
 155     sdat = dsoHeader[18]
 156     cpos = sdat.find(",")
 157     tNum = int(sdat[cpos+1:cpos+6])
 158     tUnit = sdat[cpos+6:]
 159     tUnit = tUnit.rstrip('\r\n')
 160     if tUnit in timeFactor:
 161         tFactor = timeFactor[tUnit]
 162         sampleIntvl = tNum / float(tFactor) # float() for Python 2
 163     else:
 164         print("Error time unit " + tUnit)
 165         sys.exit(1)
 166     
 167     for i in range(0,1024,1):
 168         datLine1 = str(dsoData[i])
 169         # process Sample Interval
 170         datLine2 = "{:.8f}".format(i * sampleIntvl) + "," + datLine1[17:]
 171         f.write(datLine2)
 172     f.close()
 173     
 174 
 175 def getMeasure():  # read measurements
 176     datax = []
 177 
 178     
 179 if options.data:  # Python 2/3 OK
 180     # write gnuplot parameter file, and call gnuplot
 181     # Set date and time
 182     if options.filename:
 183         fname = options.filename
 184     if options.verbose:
 185         print("DSO150 csv file: " + fname)
 186     now = time.strftime('%Y-%m-%d %H:%M:%S')
 187     if options.verbose:
 188         print(now)
 189     # create parameter file for gnuplot
 190     fnamePar = "dso150_gnuplot.par"
 191     f = open(fnamePar, "w")
 192     # default x-size = 672 pixel, use better 1200 x 675 ratio: 16:9
 193     if os_name == "Darwin":
 194         f.write('set terminal qt size 1200,675 font "Helvetica,14"'  + "\n")
 195     if os_name == "Linux":
 196         f.write('set terminal qt size 1200,675 font "Helvetica,12"'  + "\n") 
 197     if os_name == "Windows":
 198         f.write('set terminal qt size 1200,675'  + "\n") # ratio: 16:9 
 199     f.write('set datafile separator ","' + "\n")
 200     # get data title
 201     fd = open(fname)
 202     datTitle = fd.readline().rstrip('\r\n')
 203     f.write('set title ' + '"' + datTitle + '"' + '\n')
 204     f.write('set xlabel "Time (s)"' + "\n")
 205     f.write('set ylabel "Volt"' + "\n")
 206     f.write('set grid'  + "\n")
 207     
 208     # Label for measurements
 209     mLabel = []
 210     for i in range(1,19,1):
 211         mLabel.append(fd.readline().replace(",",":").rstrip('\r\n'))
 212     f.write('set label 1 at graph 0, 1.015 "' + mLabel[14] + '"\n')
 213     f.write('set label 2 at graph 0.1, 1.015 "' + mLabel[15] + '"\n')
 214     f.write('set label 3 at graph 0.19, 1.015 "' + mLabel[16] + '"\n')
 215     f.write('set label 4 at graph 0.28, 1.015 "' + mLabel[17] + '"\n')
 216     f.write('set label 5 at graph 0.63, 1.015 "' + mLabel[9] + '"\n')
 217     f.write('set label 6 at graph 0.71, 1.015 "' + mLabel[10] + '"\n')
 218     f.write('set label 7 at graph 0.79, 1.015 "' + mLabel[11] + '"\n')
 219     f.write('set label 8 at graph 0.87, 1.015 "' + mLabel[12] + '"\n')
 220     f.write('set label 9 at graph 0.94, 1.015 "' + mLabel[13] + '"\n')
 221     f.write('set label 10 at graph 0.0, -0.06 "' + mLabel[0] + '"\n')
 222     f.write('set label 11 at graph 0.1, -0.055 "' + mLabel[1] + '"\n')
 223     f.write('set label 12 at graph 0.2, -0.055 "' + mLabel[2] + '"\n')
 224     f.write('set label 13 at graph 0.3, -0.055 "' + mLabel[3] + '"\n')
 225     f.write('set label 14 at graph 0.6, -0.055 "' + mLabel[5] + '"\n')
 226     f.write('set label 15 at graph 0.73, -0.055 "' + mLabel[6] + '"\n')
 227     f.write('set label 16 at graph 0.88, -0.055 "' + mLabel[7] + '"\n')
 228     fd.close()
 229         
 230     # every ::21 skips first 20 lines
 231     f.write('plot "' + fname + '" every ::21  with lines' + "\n")
 232     f.write('pause -1 "Select terminal window, hit RETURN to continue "'  + "\n")
 233     f.close()
 234     print("Call GNUPlot")
 235     status = call(['gnuplot', 'dso150_gnuplot.par'])
 236 
 237 if options.printWS:
 238     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] (2017-10-08 08:43:00, 56.8 KB) [[attachment:DSO150_DSC07450.jpg]]
  • [get | view] (2017-10-09 15:38:00, 80.7 KB) [[attachment:DSO150_USB-UART_DSC07467.jpg]]
  • [get | view] (2017-10-17 12:42:00, 63.0 KB) [[attachment:DSO150_USB-UART_cutout_DSC07478.jpg]]
  • [get | view] (2017-10-09 16:03:00, 91.0 KB) [[attachment:DSO150_gnuplot_qt.png]]
  • [get | view] (2017-10-08 09:33:00, 128.7 KB) [[attachment:DSO150_jumper_DSC07463.jpg]]
  • [get | view] (2018-01-28 16:35:00, 123.6 KB) [[attachment:dso150-110-plot.png]]
  • [get | view] (2018-07-22 14:20:00, 8.2 KB) [[attachment:dso150-110-plot.py]]
  • [get | view] (2017-10-18 15:14:00, 5.7 KB) [[attachment:dso150_p23.py]]
 All files | Selected Files: delete move to page copy to page

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