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

Discussion in 'Python' started by Jean Dupont, Feb 3, 2012.

  1. Jean Dupont

    Jean Dupont Guest

    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?

    #!/usr/bin/python
    #version 1-2-2012, script to read data from voltcraft vc940-meter
    import serial, time, os
    voltport='/dev/ttyUSB2'
    print "Be sure the Voltcraft is connected to ttyUSB2"
    print "Enter a filename:",
    filename = raw_input()
    voltdata = open(filename,'w')
    ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1,
    timeout=15)
    print "rs-232 parameters of Voltcraft: ", ser2
    print "Opening " + ser2.portstr
    received=ser2.readline()
    print received
    print "Goodbye, data logged in file:"
    print filename
    ser2.close()
    # Close file
    voltdata.close()




    On 2 feb, 21:57, Jean Dupont <> wrote:
    > I'd like to read in the output of a voltcraft vc960 voltmeter
    > connected to a usb-port.
    > I found the perl-script below but I'd like to accomplish the same with
    > python:
    > I guess I have to use the module serial but I don't know how I should
    > set the serial parameters so they are the same as in the perl-script.
    > Could someone supply me the command for setting the serial-parameters
    > correctly
    > in Python?
    >
    > thanks
    > Jean
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use Device::SerialPort;
    >
    > die("Usage: $0 /dev/ttyS0\n") unless $#ARGV == 0;
    >
    > my ($devicepath) = @ARGV;
    >
    > my $port = new Device::SerialPort($devicepath);
    > die "Couldn't open serial port" if ! defined $port;
    >
    > $port->baudrate(2400);
    > $port->databits(8);
    > $port->parity("none");
    > $port->stopbits(1);
    > $port->handshake("none");
    > $port->rts_active(0);
    > $port->dtr_active(1);
    >
    > #$port->read_char_time(5);     # wait 5ms per character
    > $port->read_const_time(200);   # 0.2 second per unfulfilled "read"
    > call
    > $| = 1; # autoflush STDOUT
    > while(1) {
    >         my ($nin, $in) = $port->read(255);
    >         print $in;
    >
    > }
    >
    > $port->close;
     
    Jean Dupont, Feb 3, 2012
    #1
    1. Advertising

  2. On Fri, 3 Feb 2012 05:11:54 -0800 (PST), Jean Dupont
    <> wrote:

    >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


    Well, since readline() pretty much by definition wants a line-ending
    character before returning, it obviously won't work. (Side comment:
    readline() isn't even shown as part of the basic Serial class -- it is
    in a class FileLike: http://pyserial.sourceforge.net/pyserial_api.html
    -- oh wait.. fine print says that is a base class for Serial on non-io
    module systems).

    What is the boundary marker for your 11-byte chunks? You may need to
    do a synchronization loop using

    while ser2.read() != marker: continue

    then go into main processing loop with

    data = ser2.read(11)

    to capture a "chunk".

    Oh, BTW...

    > $port->rts_active(0);
    > $port->dtr_active(1);

    ....
    > #$port->read_char_time(5);     # wait 5ms per character
    > $port->read_const_time(200);   # 0.2 second per unfulfilled "read"


    vs your

    > ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE, 1, timeout=15)


    Your timeout is FIFTEEN SECONDS!

    Suspect you want something like

    ser2 = serial.Serial(voltport, 2400, 8, serial.PARITY_NONE,
    serial.STOPBITS-ONE, timeout=0.200,
    dsrdtr = True)
    and maybe also

    ser2.setDTR(True)
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Feb 7, 2012
    #2
    1. Advertising

  3. Jean Dupont

    Rick Johnson Guest

    On Feb 7, 11:44 am, Dennis Lee Bieber <> wrote:
    >
    > [...]
    >
    >         Well, since readline() pretty much by definition wants a line-ending
    > character before returning, it obviously won't work. (Side comment:
    > readline() isn't even shown as part of the basic Serial class -- it is
    > in a class FileLike:http://pyserial.sourceforge.net/pyserial_api.html
    > -- oh wait.. fine print says that is a base class for Serial on non-io
    > module systems).
    >
    >         What is the boundary marker for your 11-byte chunks? You may need to
    > do a synchronization loop using
    >
    > [...]
    >
    >         Your timeout is FIFTEEN SECONDS!
    >
    >         Suspect you want something like


    Why do you have this unnatural penchant for superfluous eight space
    indention?

    I always thought that indenting the first sentence of a paragraph was
    quite ridiculous anyhow, however, i cannot even fathom any need to
    indent a single sentence! Are you purposely injecting this noise or is
    this some automated behavior of your broken mail client? Either way, i
    find it annoying and unreadable. Could you please rectify this issue
    and bring signal to noise ratio back to reasonable levels?
     
    Rick Johnson, Feb 7, 2012
    #3
  4. 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()
     
    Dietmar Schwertberger, Feb 8, 2012
    #4
  5. Jean Dupont

    Jean Dupont Guest

    On 8 feb, 01:26, Dietmar Schwertberger <> wrote:
    > 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()


    Wow, this is great, it works like a charm. Thanks a lot!

    Jean
     
    Jean Dupont, Feb 8, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,529
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,809
  3. pj
    Replies:
    3
    Views:
    291
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    141
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    4
    Views:
    142
    Ted Zlatanov
    Feb 10, 2009
Loading...

Share This Page