Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19912 > unrolled thread

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

Started byJean Dupont <jeandupont115@gmail.com>
First post2012-02-02 12:57 -0800
Last post2012-02-07 17:57 -0500
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  convert perl-script for voltcraft voltmeter to python [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-02 12:57 -0800
    Re: convert perl-script for voltcraft voltmeter to python [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-03 05:11 -0800
      Re: convert perl-script for voltcraft voltmeter to python [newbie] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-02-07 12:44 -0500
        Re: convert perl-script for voltcraft voltmeter to python [newbie] Rick Johnson <rantingrickjohnson@gmail.com> - 2012-02-07 11:47 -0800
      Re: convert perl-script for voltcraft voltmeter to python [newbie] Dietmar Schwertberger <news@schwertberger.de> - 2012-02-08 01:26 +0100
        Re: convert perl-script for voltcraft voltmeter to python [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-08 04:24 -0800
    Re: convert perl-script for voltcraft voltmeter to python [newbie] Terry Reedy <tjreedy@udel.edu> - 2012-02-06 23:21 -0500
      Re: convert perl-script for voltcraft voltmeter to python [newbie] Jean Dupont <jeandupont115@gmail.com> - 2012-02-07 11:44 -0800
        Re: convert perl-script for voltcraft voltmeter to python [newbie] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-02-07 17:57 -0500

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

FromJean Dupont <jeandupont115@gmail.com>
Date2012-02-02 12:57 -0800
Subjectconvert perl-script for voltcraft voltmeter to python [newbie]
Message-ID<d953c169-0656-46c1-a3ba-5387c9c06df8@t30g2000vbx.googlegroups.com>
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;

[toc] | [next] | [standalone]


#19917

FromJean Dupont <jeandupont115@gmail.com>
Date2012-02-03 05:11 -0800
Message-ID<8a475c9d-24ed-45a4-af9f-2ca826f9301d@m2g2000vbc.googlegroups.com>
In reply to#19912
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 <jeandupont...@gmail.com> 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;

[toc] | [prev] | [next] | [standalone]


#19969

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-02-07 12:44 -0500
Message-ID<mailman.5506.1328637009.27778.python-list@python.org>
In reply to#19917
On Fri, 3 Feb 2012 05:11:54 -0800 (PST), Jean Dupont
<jeandupont115@gmail.com> 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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#19979

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2012-02-07 11:47 -0800
Message-ID<46cb8864-dd45-482a-b0b0-ccf20ce3638b@f14g2000yqe.googlegroups.com>
In reply to#19969
On Feb 7, 11:44 am, Dennis Lee Bieber <wlfr...@ix.netcom.com> 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?

[toc] | [prev] | [next] | [standalone]


#19995

FromDietmar Schwertberger <news@schwertberger.de>
Date2012-02-08 01:26 +0100
Message-ID<jgsfge$mgh$1@online.de>
In reply to#19917
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()

[toc] | [prev] | [next] | [standalone]


#20020

FromJean Dupont <jeandupont115@gmail.com>
Date2012-02-08 04:24 -0800
Message-ID<c7658357-1733-46b7-805c-edc6aea5b32e@o13g2000vbf.googlegroups.com>
In reply to#19995
On 8 feb, 01:26, Dietmar Schwertberger <n...@schwertberger.de> 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

[toc] | [prev] | [next] | [standalone]


#19924

FromTerry Reedy <tjreedy@udel.edu>
Date2012-02-06 23:21 -0500
Message-ID<mailman.5487.1328588509.27778.python-list@python.org>
In reply to#19912
On 2/2/2012 3:57 PM, 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:

The script below is for an old-fashioned, slow, multiple-pin serial 
port, not usb. I don't know anything about interfacing through usb. 
Recheck what the voltmeter actually connects to.

> 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?

Last I know, pyserial is also for old serial ports. Setting the 
properties should be pretty obvious from the manual or code.

There are also python usb modules.
http://sourceforge.net/projects/mysql-python/?source=directory

> #!/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;


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#19977

FromJean Dupont <jeandupont115@gmail.com>
Date2012-02-07 11:44 -0800
Message-ID<f6626e9a-ec89-4081-adea-07703e2ad6ee@t30g2000vbx.googlegroups.com>
In reply to#19924
On 7 feb, 05:21, Terry Reedy <tjre...@udel.edu> wrote:
> On 2/2/2012 3:57 PM, 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:
>
> The script below is for an old-fashioned, slow, multiple-pin serial
> port, not usb. I don't know anything about interfacing through usb.
> Recheck what the voltmeter actually connects to.
The voltmeter uses an optical rs232-connection, that is "good enough
technology" for this purpose. But as I don't have a computer with real
rs232 ports I use a rs232toUSB adapter which presents itself to the
linux-computer as /dev/ttyUSBx.
>
> > 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?
>
> Last I know, pyserial is also for old serial ports. Setting the
> properties should be pretty obvious from the manual or code.
>
It is not so obvious as you might think, one reason being the
handshake line(s?) are used in an unconvential way to supply power to
the rs232-optical interface
> There are also python usb modules.http://sourceforge.net/projects/mysql-python/?source=directory
I followed this link but all I found was something concerning
mysql...???
>
anyway, thanks for trying to help

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;
>
> --
> Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#19993

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-02-07 17:57 -0500
Message-ID<mailman.5524.1328655912.27778.python-list@python.org>
In reply to#19977
On Tue, 7 Feb 2012 11:44:40 -0800 (PST), Jean Dupont
<jeandupont115@gmail.com> wrote:


>It is not so obvious as you might think, one reason being the
>handshake line(s?) are used in an unconvential way to supply power to
>the rs232-optical interface

	That could be a problem... 1) USB<>Serial converters may not supply
emulation of ALL the handshake lines. 2) USB ports don't supply much
power -- what is it, each powered hub provides 500mA? (shared by all
ports on the hub?)
http://en.wikipedia.org/wiki/Universal_Serial_Bus#Power

	So even if the converter supplies both the RS-232 voltages and the
control lines, it may not have enough power to activate a device.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web