Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Perl conversion to python...

Reply
Thread Tools

Perl conversion to python...

 
 
Benjamin Schollnick
Guest
Posts: n/a
 
      11-23-2009
Folks,

I'm having some issues here with pyserial & trying to translate a perl
script to python... It's probably my inexperience with PySerial &
perl that is troubling me...

Can anyone assist?

I'm concerned, since I can't seem to receive the data in any reliable
manner.. I've tested multiple times, and only once received data...
So I suspect that my Transmit & receive code is faulty...

def xmit ( data, serialport ):
for x in data:
xmit_byte (x, serialport)
# serialport.write ( binascii.unhexlify ( data ) )
# for x in data:
# print str(x).encode ('hex')
# serialport.write ( x.encode('hex'))

def receive ( serialport ):
received = serialport.read (20)
print received, "!"

----- Perl Code ----
sub tx_command {
my $port = shift;
my $cmd = shift;

# warn "tx_command($cmd)\n";

my @cmd_bytes = split(/\s/, $cmd);

foreach my $byte (@cmd_bytes) {
$byte = pack('C', hex($byte));

$port -> write($byte);
select(undef, undef, undef, 0.01);
}
}

# returns the rtt, or 0 if no response
sub rx_response {
my ($port, $address) = @_;

$port->read_char_time(0); # don't wait for each character
$port->read_const_time(5000); # timeout if we don't get what we're
looking for

my $buf = '';

my $t_start = time;

### accumulate one byte at a time until we see the substring we're
looking for

while (1) {
my ($count_in, $string_in) = $port->read(1);

if ($count_in == 0) {
# warn "TIMEOUT\n";
return 0;
}

$buf .= $string_in;

my $bufstring = packed_to_text($buf);

#warn "bufstring: ".$bufstring;

if ($bufstring =~/02 50 $address (.. .. ..) (..) (.. ..)/) {

my $powerlinc_addr = $1;
my $flags = $2;
my $command = $3;

# warn "got response!\n";

my $rtt = time() - $t_start;

return $rtt;
}

}
}

 
Reply With Quote
 
 
 
 
J Kenneth King
Guest
Posts: n/a
 
      11-23-2009
Benjamin Schollnick <(E-Mail Removed)> writes:

> Folks,
>
> I'm having some issues here with pyserial & trying to translate a perl
> script to python... It's probably my inexperience with PySerial &
> perl that is troubling me...
>
> Can anyone assist?
>
> I'm concerned, since I can't seem to receive the data in any reliable
> manner.. I've tested multiple times, and only once received data...
> So I suspect that my Transmit & receive code is faulty...
>
> def xmit ( data, serialport ):
> for x in data:
> xmit_byte (x, serialport)
> # serialport.write ( binascii.unhexlify ( data ) )
> # for x in data:
> # print str(x).encode ('hex')
> # serialport.write ( x.encode('hex'))
>
> def receive ( serialport ):
> received = serialport.read (20)
> print received, "!"


Gah.. indentation is broken in your post... :S

>
> ----- Perl Code ----
> sub tx_command {
> my $port = shift;
> my $cmd = shift;
>
> # warn "tx_command($cmd)\n";
>
> my @cmd_bytes = split(/\s/, $cmd);
>
> foreach my $byte (@cmd_bytes) {
> $byte = pack('C', hex($byte));
>
> $port -> write($byte);
> select(undef, undef, undef, 0.01);
> }
> }


import struct

def tx_command(port, cmd):
cmd_bytes = cmd.split(' ')

for byte in cmd_bytes:
byte = struct.pack('C', hex(int(byte)))
port.write(byte)
# select() is a system call in Perl..
# insert Python equivalent here

>
> # returns the rtt, or 0 if no response
> sub rx_response {
> my ($port, $address) = @_;
>
> $port->read_char_time(0); # don't wait for each character
> $port->read_const_time(5000); # timeout if we don't get what we're
> looking for
>
> my $buf = '';
>
> my $t_start = time;
>
> ### accumulate one byte at a time until we see the substring we're
> looking for
>
> while (1) {
> my ($count_in, $string_in) = $port->read(1);
>
> if ($count_in == 0) {
> # warn "TIMEOUT\n";
> return 0;
> }
>
> $buf .= $string_in;
>
> my $bufstring = packed_to_text($buf);
>
> #warn "bufstring: ".$bufstring;
>
> if ($bufstring =~/02 50 $address (.. .. ..) (..) (.. ..)/) {
>
> my $powerlinc_addr = $1;
> my $flags = $2;
> my $command = $3;
>
> # warn "got response!\n";
>
> my $rtt = time() - $t_start;
>
> return $rtt;
> }
>
> }
> }


This isn't all that more complicated. I dunno, maybe it's just me but
I find most Perl pretty easy to read (barring obfuscation which just
takes longer to read but is no more difficult). For the most part you
can generally substitute the Python equivalent statements for each
line of Perl and get good results. Optimize for better Pythonicity
afterwards.
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      11-24-2009
J Kenneth King wrote:
> Benjamin Schollnick <(E-Mail Removed)> writes:


>> select(undef, undef, undef, 0.01);


> # select() is a system call in Perl..
> # insert Python equivalent here


That would be:

time.sleep(0.01)

Perl has a sleep() built-in, but it truncates its argument to an int.
If you want to sleep less than a second in Perl, you have to use select
as shown.

Not so in Python. Python's time.sleep() takes a float argument and
calls some platform-dependent function that provides sleeping with
sub-second accuracy. On some platforms, it ends up calling the
C level select() function.

Keep in mind that in both languages, your program may end up sleeping
longer than it should due to scheduling or other activity on the system.


Hope this helps,

-- HansM

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments