----------------------------------------------------
Serial.pm
#! C:\perl\bin\perl.exe
use strict;
use warnings;
package Serial;
# The following 2 lines I suspect are the lines that make a package into a module
# Also, they seem to make it a member of a class Exporter.
require Exporter;
my @ISA = qw( Exporter ); # This module "is a" Exporter class member
#Now declare what we permit to be visible within the module.
my @EXPORT = qw( &Serial_Init &Serial_TrxRcv );
use Win32::SerialPort;
use Time::HiRes qw(usleep);
my $port;
# ############ subroutine Serial_Init ###################################################
sub Serial_Init {
my $port_name = 'COM5';
#my $config_file = 'setup.cfg'; # use this to configure from the file
$port = new Win32::SerialPort( $port_name )
|| die "Unable to open: $^E\n"; # $^E EXTENDED_OS_ERROR
#$port = new Win32::SerialPort($port_name, $config_file) || die "Unable to open: $^E\n";
$port->handshake('none'); # none dtr rts xoff
$port->baudrate(38400); # 19200 57600 1200 9600 115200 4800 600 2400 300 38400
$port->parity('none'); # space none odd even mark
#$port->parity_enable(1); # for any parity except "none"
$port->databits(8); # options 7 8
$port->stopbits(1); # options 2 1
$port->buffers(256, 256);
$port->read_interval(0); #RI
$port->read_const_time(20); #RC
$port->write_char_time(1); #WM
$port->write_const_time(100); #WC
print "Write settings; ";
$port->write_settings || undef $port;
# A report out to the console
my $baud = $port->baudrate;
my $parity = $port->parity;
my $data = $port->databits;
my $stop = $port->stopbits;
my $hshake = $port->handshake;
print "B = $baud, D = $data, S = $stop, P = $parity, H = $hshake\n\n";
# use the below to save the current configuration
# if ( $port ) { $port->save('setup.cfg') ; print "Serial Port OK \n" };
# pack: used for assembling binary stuff
# my $status = pack('H2' * 6, 'ca', '00', '01', '00', '00', 'fe');
#$port->write("ati\x0D\x0A"); # carriage return and line feed: no different
#$port->write("ate0"."\r");
#print "test 01\n";
#usleep 0;
#print "test 02\n";
}
# ############## subroutine Serial_TrxRcv ###############################################
sub Serial_TrxRcv {
my ($cmd) = @_;
my $response = "";
#print "cmd; $cmd\n";
$port->write($cmd."\r");
my $loop = 1;
while( $loop ) {
usleep(200000); # 0.2 of a second
my $partial_resp;
$partial_resp = $port->input;
chomp $partial_resp;
$response = $response.$partial_resp;
# print $response;
#my $responseHex = unpack ('H*', $response);
#print $responseHex."\n";
my $last = substr ( $response, -1 ); # get the last character
if ($last eq ">") { $loop = 0;
$response = substr( $response, 0, -1); # -1 removes the ">";
chomp $response;
next;
}
print ".";
}
return $response;
}
1;
---------------------------------------------------------------
####
---------------------------------------------------------------
# ########################################################################
test_Serialpm.pl
#!usr/bin/perl
use strict;
use warnings;
use lib '.'; # The Serial.pm is in the current folder
use Serial; # use a "1 off type of macro expansion" of the Serial module
Serial::Serial_Init(); # Initialise the interface
my $response = Serial::Serial_TrxRcv("at dp"); # Transmit "AT Diplay Protocol" and get a response
print "Response is; $response\n";
print "Response num char; ".length($response)."\n";
------------------------------------------------------------------
####
------------------------------------------------------------------
Console output for a simple test;
Write settings; B = 38400, D = 8, S = 1, P = none, H = none
Response is; AUTO, SAE J1850 PWM
Response num char; 21
(in cleanup) Can't call method "Call" on an undefined value at C:/Strawberry/perl/vendor/lib/Win32API/CommPort.pm line 193 during global destruction.
----------------------------------------------------------