Attachment 'dso150_p23.py'

Download

   1 #! /usr/bin/env python3
   2 """
   3 # DSO150_p23.py - capture DSO150 data
   4 # 2017-10-06 Rudolf Reuter, version 1.0, Python 2/3
   5 # waveform data are just time resolution (s) and 12 bit data (1024)
   6 # firmware must have toshi extension 60B
   7 #
   8 # This software is public domain.
   9 """
  10 from __future__ import print_function # make "print" Python 2/3 compatible
  11 import glob
  12 import locale
  13 import optparse
  14 import os
  15 import platform
  16 import serial
  17 from subprocess import call
  18 import sys
  19 import time
  20 
  21 # make the program universal for Python 2 and 3
  22 from sys import version_info
  23 python2 = False
  24 if version_info.major == 2:
  25     python2 = True
  26 
  27 # on which port should the tests be performed
  28 # http://sourceforge.net/projects/osx-pl2303/
  29 
  30 # for USB port use, adopt to your hardware
  31 port = ""
  32 os_name = platform.system()
  33 if os_name == "Windows":
  34     port = "COM3:"
  35 if os_name == "Linux":
  36     port = "/dev/ttyUSB0"
  37 if os_name == "Darwin": # Mac OS X
  38     #port = "/dev/cu.usbserial-A4009RFD"
  39     #port = "/dev/cu.PL2303-0000103D"
  40     #port = "/dev/cu.wchusbserial14a120"  # CH340
  41     port = "/dev/cu.SLAB_USBtoUART"  # CP2102
  42 # for debug only, command line argument
  43 #sys.argv = ['dso150_p23.py', '-p']
  44 
  45 parser = optparse.OptionParser(
  46     usage = "%prog [options] [port [baudrate]] version 1.0", 
  47     description = "DSO150_p23.py - capture DSO150 data."
  48 )
  49 parser.add_option("-d", "--date", dest="date", action="store_true",
  50                   help="set date and plot")
  51 parser.add_option("-f", "--file", dest="filename",
  52                   help="Enter filename")
  53 parser.add_option("-g", "--gport", dest="gport",
  54                   help="serial port, e.g. /dev/cu.xxx")
  55 parser.add_option("-p", "--print", dest="printWS", action="store_true",
  56                   help="print waveform screen")
  57 #parser.add_option("-q", "--quiet",
  58 #                  action="store_false", dest="verbose", default=True,
  59 #                  help="don't print status messages to stdout")
  60 parser.add_option("-t", "--timeout", dest="timeout",
  61                     help="Enter timeout in sec.")
  62 parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
  63                   help="tell more internal values for debugging")
  64 
  65 (options, args) = parser.parse_args()
  66 
  67 if (len(sys.argv) < 2):  # no arguments given
  68     parser.print_help()
  69     print("Default Serial Port: " + port)
  70 
  71 # open USB-serial port
  72 if options.gport:
  73     port = options.gport
  74 otimeout = 1              # 1 sec
  75 if options.timeout:
  76     otimeout = int(options.timeout)
  77 try:
  78     ser = serial.Serial(port, baudrate=38400, timeout=otimeout)
  79     if options.verbose:
  80         print(ser.name)
  81     ser.flushInput()
  82     #time.sleep(2)  # skip Arduino DTR toogle RESET
  83 except:
  84     sys.stderr.write("could not open port %r\n" % (port))
  85     if os_name == "Darwin": # Mac OS X
  86         print ("Found ports:")
  87         for name in glob.glob('/dev/cu.*'): # show available ports
  88             print("  "+name)
  89     if os_name == "Linux":
  90         for name in glob.glob('/dev/ttyUSB*'): # show available ports
  91             print("  "+name)
  92     sys.exit(1)
  93 
  94 if options.printWS:  # Python 2/3 OK
  95     # find out decimal point character
  96     locale.setlocale(locale.LC_ALL, '')
  97     conv = locale.localeconv()
  98     dp =  conv["decimal_point"]
  99     #print(dp)
 100     print ("Push the DSO150 encoder button.")
 101     
 102     fname = "dso150-data.csv"
 103     if options.filename:
 104         fname = options.filename
 105     if options.verbose:
 106         print("DSO150 csv file: " + fname)
 107     
 108     # wait for data
 109     nwait = 60 # 60 seconds
 110     ncount = 1
 111     dsoData = []
 112     while (nwait > 0):
 113         sdat = ser.readline()
 114         if len(sdat) < 1:
 115             nwait -= 1
 116             print (nwait)
 117         else:
 118             if ncount == 1:
 119                 timeRes = float(sdat)
 120                 ncount += 1
 121                 #print(timeRes)
 122             else:
 123                 ncount += 1
 124                 for i in range(1,1024,1):
 125                     sdat = ser.readline()
 126                     dsoData.append(float(sdat))
 127                 break
 128             #sys.exit(0)
 129     if nwait < 1:
 130         sys.exit(0)
 131         
 132     # produce csv file
 133     f = open(fname, "w")
 134     dsoTime = timeRes
 135     datLine = str(dsoData[0]) + ";" + "0.0" + "\n"
 136     datLine = datLine.replace(".", dp)
 137     f.write(datLine)
 138     for i in range(1,1023,1):
 139         datLine = str(dsoData[i]) + ";" + "{:.8f}".format(dsoTime) + "\n"
 140         datLine = datLine.replace(".", dp)
 141         f.write(datLine)
 142         dsoTime += timeRes
 143     f.close()
 144     
 145 if options.date:  # Python 2/3 OK
 146     # write gnuplot parameter file, and call gnuplot
 147     # Set date and time
 148     now = time.strftime('%Y-%m-%d %H:%M:%S')
 149     if options.verbose:
 150         print(now)
 151     # create parameter file for gnuplot
 152     fname = "dso150_gnuplot.par"
 153     f = open(fname, "w")
 154     # default x-size = 672 pixel, use better 1200 x 675 ratio: 16:9
 155     if os_name == "Darwin":
 156         f.write('set terminal qt size 1200,675 font "Helvetica,14"'  + "\n")
 157     if os_name == "Linux":
 158         f.write('set terminal qt size 1200,675 font "Helvetica,12"'  + "\n") 
 159     else:
 160         f.write('set terminal qt size 1200,675'  + "\n") # ratio: 16:9 
 161     f.write('set datafile separator ";"' + "\n")
 162     if os_name == "Windows":
 163         f.write('set decimalsign locale "German_Germany.1252"' + "\n")
 164     else:
 165         f.write('set decimalsign locale "de_DE.UTF-8"' + "\n")
 166     f.write('set title "File: dso150-data.csv ' + now + '"' + "\n")
 167     f.write('set xlabel "Time (s)"' + "\n")
 168     f.write('set ylabel "Volt"' + "\n")
 169     f.write('set grid'  + "\n")
 170     # "using 2:1" swaps the columns
 171     f.write('plot "dso150-data.csv" using 2:1 with lines' + "\n")
 172     f.write('pause -1 "Select terminal window, hit RETURN to continue "'  + "\n")
 173     f.close()
 174     status = call(['gnuplot', 'dso150_gnuplot.par'])
 175 
 176 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.