I am communicating with equipment on a USB COM port using USB-RS485 serial port adapter using Win32::SerialPort
A CMD message of six bytes is sent to Equipment, which responds with ACK message.
However, I have trouble reading the ACK messsage returned from Equipment from the COM port. I have used $PortObj->input; $PortObj->lookfor(); $PortObj->read() to retrieve the string from COM port, but it does not show up.
Port characteristics are: Datarate = 115200 bps, 1 start bit, 8 databits, 1 stop bit, odd parity. 1 Byte = 11 bits. Each 11-bit byte has duration 11/115200 = 95.49 us. Timing is approx: 0.5msec: CMD message six bytes duration, 2 msec later ACK message, 2msec ACK message duration. 20 msec between successive CMD messages.
Nearly any combinations of values on $PortObj->read_interval(), $PortObj->read_char_time(), $PortObj->read_const_time() times, but it does not read the the COM port. I tried these methods and they do not read COM port, and gives in Error messages,
: CMD 01 61 00 00 01 61
Second Read attempted before First is done at serial_io.pl line 20.
Use of uninitialized value $got in numeric ne (!=) at C:/appl/strawber
+ry64/perl/site/lib/Win32/SerialPort.pm line 1560.
: ACK
Also, is the command 20 millisec between port writes
sleep( 0.020 ) a problem?
Any advice how to read COM port?
- Mastertone "Longtime Reader, First time poster"
#!C:\appl\strawberry64\perl\bin
use strict;
use warnings 'all';
use Win32::SerialPort;
use Time::HiRes qw (time sleep);
use Data::Dumper;
my $PortObj_r = &com_port_initialize() ;
my @byte = (0x01, 0x61, 0x00, 0x00, 0x01, 0x61 );
my $str = ": CMD ";
foreach ( @byte ) { $str .= sprintf(" %02X", $_)};
my $string_in; my $count_in;
while ()
{
my $PortObj_r = &get_PortObj; # Call Port Obj #
my $PortObj = $$PortObj_r;
print "$str \n";
foreach my $num ( @byte ) { $PortObj->transmit_char($num) }
$string_in = $PortObj->input; # Error: Line 20 ** #
# $string_in = $PortObj->lookfor(); # Error: Line 21 ** #
# ($count_in, $string_in) = $PortObj->read(1); # Error: Line 22 #
print ": ACK "; print $string_in; print "\n";
sleep( 0.020 ); # 20 millisec between writes
undef $PortObj;
} # -----------------------------------------
sub get_PortObj { $PortObj_r; }
# --------------------------------------------------------
# PortObject Definiton
# --------------------------------------------------------
sub com_port_initialize
{
my $quiet = 1;
$| = 1;
my $PortName = "COM10";
my $PortObj = new Win32::SerialPort ($PortName, $quiet)
|| die "Error: Can't open $PortName: $^E\n";
print "> PortName = ", $PortObj->alias, "\n";
my $dataRate = 115200;
my $read_bufferSize = 4096;
my $write_bufferSize = 4096;
my $char11_t = (11/$dataRate) ; # 95.49 us = 0.095 msec
my $readChar_t = 1 ; # in millisec; char11_t
my $readConst_t = 2; # read_const_time
my $readInterval_t = 10;
my $writeCharTime = 1;
my $writeConstTime = 0;
$PortObj->handshake("none");
$PortObj->user_msg("ON");
$PortObj->baudrate($dataRate);
$PortObj->databits(8);
$PortObj->parity("odd");
$PortObj->stopbits(1);
$PortObj->binary('T');
$PortObj->parity_enable('T');
$PortObj->debug(0);
$PortObj->buffers($read_bufferSize, $write_bufferSize);
$PortObj->read_interval($readInterval_t );
$PortObj->read_char_time($readChar_t );
$PortObj->read_const_time($readConst_t);
$PortObj->write_char_time($writeCharTime);
$PortObj->write_const_time($writeConstTime);
$PortObj->write_settings || undef $PortObj;
my $ModemStatus = $PortObj->modemlines;
if ($ModemStatus & $PortObj->MS_RLSD_ON) { print "> Carrier Detect
+ed"; }
open OUTFILE, ">portObj.txt" or
die "Error: Cannot open portObj.txt $!\n";
print OUTFILE "> PortObj = ", Dumper \\$PortObj;
close OUTFILE;
\$PortObj; # return reference to $PortObj
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.