Test and Measurement Forum

Python software for ADC 16

Forum for discussing PicoScope (version 5)

Python software for ADC 16

Postby croc » Wed Sep 10, 2008 6:25 pm

Hi,

I have had the ADC16 datalogger for quite some time, just recently I wrote a python program that pulls the data from the ADC, currently I'm only using a single channel, but adding other channels is very easy. if anyone is interested I can post the code. let me know.


Croc
croc
User
User
 
Posts: 4
Joined: Wed Sep 10, 2008 6:15 pm

Postby Guest » Thu Jul 09, 2009 9:27 am

I am interested! Please post......

Mark.
Guest
 

python code

Postby Guest » Thu Jul 09, 2009 3:58 pm

Code: Select all
# Software for controlling Pico data logger

# Python imports used
#########################################
import urllib
import time
import string
import sys
import math
import socket
import telnetlib
import os
import re
import commands
import serial
import binascii

# Com port used for ADC
COMPORT = 6 #(actual com port - 1)


#######################
def connect_to_adc():
#######################
    # read from the serial port
    # Open port 1 at "9600,8,N,1", no timeout
    global ser
   
    ser = serial.Serial(COMPORT,baudrate=9600, rtscts=0) # open serial port #7

    # Power up the ADC device
    ser.setRTS(1)             #set RTS on  (must be positive)
    ser.setDTR(0)             #set DTR off (must be negative)

    # check which port was realy used
    print "Serial port ",ser.portstr," Opened"

    # allow the ADC to power up
    time.sleep(2)             
    print "ADC powered up and ready"


####################
def open_log_file():
####################
    # Create the file name with the board serial number
    global logfile
   
    logfile = "voltage_log.txt"

    # open the log file for writing
    log_file = open(logfile, 'w')
   
    log_file.write('\n')
    log_file.write('\n')
    log_file.write('Channel 1          Channel 3\n')
    log_file.write('********************************\n')
    log_file.write('\n')

    # Close the log file so that the systeminfo can be dumped into it
    # without this it would fail because the file was locked by the python process
    log_file.close()



#######################
def single_channel_adc(channel_config, channel, samples):
#######################
   
    # configure channel 1
    # 16 bit mode
    # differential
    while (samples > 0):

        # send data to ADC to configure channel 1
        #ser.write("\x1F")
        ser.write(channel_config)
       
        time.sleep(.1)

        # read back three characters from the serial port
        # readings are returned as 3 bytes
        # byte 1 = sign (0x2B = positive, 0x2d = negative)
        # byte 2 = MSB
        # byte 3 = LSB
        serial_data = ser.read(size=3)

        # ignore the leading '0x'
        volt_msb = ord(serial_data[1:2])
        volt_lsb = ord(serial_data[2:3])
       
        # shift the MSB up and then add in the LSB value
        total_volts = ((volt_msb << 8 ) + volt_lsb)
       
        # Convert the integer value to an acutal voltage
        # and round that to 4 decimal places after the decimal point.
        voltage = round(((total_volts  * 2.5) / 65535), 4)
       
        # Here is the computed voltage.
        print "Channel",channel,":",voltage,"V"

        samples = samples - 1
   
    #close port
    ser.close()
   

#######################
def dual_channel_adc(channel_a_config, channel_b_config, channel_a, channel_b, samples):
#######################
   
    # configure channel 1 and 3
    # 16 bit mode
    # single ended

    # Reopen the log file
    log_file = open(logfile, 'a')

    # Used to skip the first reading from the ADC
    reading = 1
   
    while (samples > 0):
       
        # Get the first channels data
        ser.write(channel_a_config)
        time.sleep(.1)
        channel_a_serial_data = ser.read(size=3)
        channel_a_volt_msb = ord(channel_a_serial_data[1:2])
        channel_a_volt_lsb = ord(channel_a_serial_data[2:3])
        channel_a_total_volts = ((channel_a_volt_msb << 8 ) + channel_a_volt_lsb)
        channel_a_voltage = round(((channel_a_total_volts  * 2.5) / 65535), 4)
       
        # Get the second channels data
        ser.write(channel_b_config)
        time.sleep(.1)
        channel_b_serial_data = ser.read(size=3)
        channel_b_volt_msb = ord(channel_b_serial_data[1:2])
        channel_b_volt_lsb = ord(channel_b_serial_data[2:3])
        channel_b_total_volts = ((channel_b_volt_msb << 8 ) + channel_b_volt_lsb)
        channel_b_voltage = round(((channel_b_total_volts  * 2.5) / 65535), 4)

        if (reading > 1):
            # There seems to be some settling time needed, so I toss the first reading
            # Here is the computed voltages.
            print "Channel",channel_a,":",channel_a_voltage,"V"
            print "Channel",channel_b,":",channel_b_voltage,"V"

            # Dumping the readings to the log file.
            log_data = str(channel_a_voltage)+"     ,      "+str(channel_b_voltage)+"\n"
            log_file.write(log_data)
        reading = reading + 1

        samples = samples - 1

     #close port
    ser.close()
   
###########   
def main():
###########


    connect_to_adc()

    samples = 0
    choice = -1
   
    while(choice == -1) and (samples == 0):
        print " ADC measurement SW"
        print "-------------------------------------"
        print " Select channel to measure"
        print ""
        print "1 - Channel 1"
        print "2 - Channel 2"
        print "3 - Channel 3"
        print "4 - Channel 4"
        print "5 - Channel 5"
        print "6 - Channel 6"
        print "7 - Channel 7"
        print "8 - Channel 8"
        print "9 - Channel 1 and 3"
        print "enter channel to measure "
       
        choice = raw_input()
        choice = string.atoi(choice)

        print "Enter the number of samples to take: (1 - 1000)"
        samples = raw_input()
        samples = string.atoi(samples)
       
        # These are for single ended measurements, just for demo
        if (choice == 1):
            channel_config = "\x1E"
            channel = 1
           
        if (choice == 2):
            channel_config = "\x3F"
            channel = 2
           
        if (choice == 3):
            channel_config = "\x5F"
            channel = 3
               
        if (choice == 4):
            channel_config = "\x7F"
            channel = 4
               
        if (choice == 5):
            channel_config = "\x9F"
            channel = 5
               
        if (choice == 6):
            channel_config = "\xBF"
            channel = 6
               
        if (choice == 7):
            channel_config = "\xDF"
            channel = 7
               
        if (choice == 8):
            channel_config = "\xFF"
            channel = 8   
               
        if (choice == 9):
            channel_a_config = "\x1F"
            channel_a = 1
            channel_b_config = "\x5F"
            channel_b = 3

    # Open the log file to capture the readings
    open_log_file()
   
    if (choice <= 8):
        # single channel mesurement
        single_channel_adc(channel_config, channel, samples)
       
    elif (choice == 9):
        # dual channel mesurement
        dual_channel_adc(channel_a_config, channel_b_config, channel_a, channel_b, samples)
   
     #log_file.close() 
       
if __name__ == '__main__':
    main()
Guest
 


Return to PicoScope 5

Who is online

Users browsing this forum: No registered users and 0 guests