Last discussion on this issue I mentioned a python program I found.  I
have since put the program in Spyder using Python3.  I had to make a number
of changes to accommodate the change to 3.  The latest is that I see that
the readings are available to the  program although parsing the data is
running into difficulty associated, I believe, with  Python3's fussiness
with bytes, strings, etc.  (The data I see is via some print statements
stuck in the code.)

line 72: originally it had

mult=self.units.strip()+'  '[0]
TypeError: can't concat bytes to int

I tried:
mult=self.units.strip()+'  '.encode()[0]

and
mult=self.units.strip()+b'  '[0]

to no avail.

Note:
data =  b'DC -0.000   V\r'
type data =  <class 'bytes'>
01234567890123456789
type units =  <class 'bytes'>

But the good news is that when I changed the reading on the meter the value
in data changed accordingly.  So I see the issue now as one of knowing how
to fix the Python so that it parses the data correctly, rather than an
issue of how  to communicate with the dmm.

BTW, I  see no way to change the manufacture's sw running under Windows to
look at the usb, which is what I need to test the usb interface under
Windows.  I will explore the usb using Linux, which is  the ultimate goal.

Full program as it stands (sorry I do not have line #'s)

#!/usr/bin/python

import serial, time, os

modes=['DC V','AC V','DC uA','DC mA','DC A',
       'AC uA','AC mA','AC A','OHM','CAP',
       'Hz','Net Hz','Amp Hz','Duty','Net Duty',
       'Amp Duty','Width','Net Width','Amp Width','Diode',
       'Continuity','hFE','Logic','dBm','EF','Temperature']

segs={ 0x00: ' ',
       0x20: '-',
       0xd7: '0',
       0x50: '1',
       0xb5: '2',
       0xf1: '3',
       0x72: '4',
       0xe3: '5',
       0xe7: '6',
       0x51: '7',
       0xf7: '8',
       0xf3: '9',
       0x87: 'C',
       0xa7: 'E',
       0x27: 'F',
       0x86: 'L',
       0x66: 'h',
       0x64: 'n',
       0x37: 'P',
       0x24: 'r',
       0xa6: 't'}

byte1=['Hz','Ohms','K','M','F','A','V','m']
byte2=['u','n','dBm','S','%','hFE','REL','MIN']
byte7=['Beep','Diode','Bat','Hold','-','~','RS232','Auto']

decimal=0x08

class ChecksumError(Exception): pass
class LengthError(Exception): pass
class TimeoutError(Exception): pass

import sys

infinity=9e99999999 # and beyond!

class Packet:
    def __init__(self):
        self.string=''
        self.unit=''
        self.value=None
        self.mode=None
        self.packet_len=14

    def load_data(self,data):
        debug=True
        # DC -0.000   V
        # 01234567890123
        if debug:
            print("data = ",data)
            print("type data = ",type(data))
            print("0123456789"*2)

        if len(data) != 14:
            raise LengthError

        self.mode =data[0:2]
        self.sign =data[3]
        self.value=data[4:9]
        self.units=data[9:14].strip()
        print("type units = ",type(self.units))
        mult=self.units.strip()+' '[0]
        print("mult = ",mult)
        if not mult in "MKmunp":  mult=' '

        if debug:
            print("mode",self.mode)
            print("sign",self.sign)
            print("value",self.value)
            print("units",self.units)
            print("mult",mult)

        if data[2]!=' ' or data[13] != '\r':
            raise ChecksumError


        if 'O' in self.value and 'L' in self.value:
            self.value=infinity
        else:
            self.value=float(self.value) * {'M':1e6,
                                            'K':1e3,
                                            ' ':1,
                                            'm':1e-3,
                                            'u':1e-6,
                                            'n':1e-9,
                                            'p':1e-12}[mult]


        if self.sign=='-':
            self.value=-self.value

        self.string=data

    def __repr__(self):
        return "%s %s"%(self.string,self.unit) #self.value)

    def __nonzero__(self):
        return None!=self.mode

class MasMeter:
    def __init__(self, port='/dev/ttyPROLIFIC'):

        if not os.path.lexists(port):
            port = '/dev/ttyS0'

        self.s=serial.Serial(port=port, timeout=3.0,
                             baudrate=600, stopbits=2,
                             bytesize=serial.SEVENBITS)
        try:
            self.s.setDTR()
        except:
            print("Could not set DTR")

        try:
            self.s.setRTS(0)
        except:
            print("Could not clear RTS")

#       self.s.setTimeout(1)
        self.packet_len=14
        self.packet=None

    def flush(self):
        self.s.flushInput()

    def try_for_packet(self):
        """ May return a None packet"""
        self.s.write(b'\0'*14)

        d=self.s.read(self.packet_len)
        print("type d = ",type(d))

        if len(d)==0: raise TimeoutError("is meter turned on?")

        if len(d)<14: return False

        #if len(d): print("read %d bytes"%len(d))

        p=Packet()

        while 1:
            if len(d)>2*self.packet_len: return False
            try:
                p.load_data(d[-self.packet_len:])
                return p
            except ChecksumError:
                d=d+self.s.read(1)
            except LengthError:
                return False

        else:
            return False

    def get_packet(self, tries=-1):
        while tries!=0:
            p=self.try_for_packet()
            if p: return p
            tries-=1

    def get_dc_voltage(self):
        return self.get_specific_measurement('DC','V')

    def get_specific_measurement(self, mode, units):
        connected_printed=False
        units_printed=False
        while 1:
            p=self.get_packet(10)
            if p==None:
                if not connected_printed:
                    print("Meter not connected?")
                    connected_printed=True
            elif p.mode!=mode or not units in p.units:
                if not units_printed:
                    print("Please set meter to %s %s"%(mode,units))
                    units_printed=True
            else:
                return p;

import time
if __name__=="__main__":
    meter=MasMeter()
    while 1:
        p=meter.get_dc_voltage()
        if p and None != p.value:
            print(p.value)
        else:
            print("x")
        sys.stdout.flush()
_______________________________________________
PLUG mailing list
PLUG@pdxlinux.org
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to