convert perl-script for voltcraft voltmeter to python [newbie]

Dietmar Schwertberger news at schwertberger.de
Tue Feb 7 19:26:48 EST 2012


Am 03.02.2012 14:11, schrieb Jean Dupont:
> As my request might have been too much asked, I have started doing
> some coding myself.
> I'm in doubt about the readline statement -which doesn't show anything
> received- as the meter sends continuously streams of 11 bytes
> Is there a way to just monitor with python what is arriving at a
> serial port?
Some time ago I started working on reading data from a VC940.
I would assume that the protocol is the same.

Please find below the code that will return the raw values from
a VC940 (tested on a classical RS232 port, but probably
will work on USB-RS232 converters as well).


If you don't get anything, then you should check whether your
USB converter is supplying voltage on the DTR pin once you have called
self.serial.setDTR(1).


You have the description how to decode the values?
E.g. the string "0003:1401" translates to 0.3 Ohms.

I did not implement anything else, as I just wanted to be sure
that I could read the values, but I never needed to...


Regards,

Dietmar


import serial
import time


class VC940(object):
     def __init__(self, port="COM3"):
         self.port = port
         self.serial=serial.Serial(port,2400, bytesize=7, parity="N", 
stopbits=1, timeout=1.5, xonxoff=0, rtscts=0, dsrdtr=None)
         self.serial.setRTS(0)
         self.serial.setDTR(0)
     def _read_raw_value(self):
         timeout = True
         for n in range(5):
             self.serial.flushInput()
             self.serial.setDTR(1)
             data = self.serial.read(11)
             self.serial.setDTR(0)
             if data.endswith("\r\n") and len(data)==11:
                 return data
             if not data:
                 raise ValueError, "communication timeout"
         raise ValueError, "could not read data from port"


if __name__=="__main__":
     vc = VC940()
     while True:
         print vc._read_raw_value()



More information about the Python-list mailing list