jmClifford has asked for the wisdom of the Perl Monks concerning the following question:
Hi. As the title suggests in an using a supplied module Win32::SerialPort in my own module. I have a few minor issues (I think). The next 3 sections give my module's code, my test routine and console output
---------------------------------------------------- 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 in +to 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 th +e 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 "U +nable 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 charact +er 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 Seria +l 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 globa +l destruction. ----------------------------------------------------------
My first question is wrt the (in cleanup) warning experienced at the end of the execution. Can I get rid of this?
In test_Serialpm.pl I have "use lib '.';" to denote where the library is ? Is there a better way ?
Also, in the same test routine, I have to use long identifiers to access the embedded subroutines. I expected that this was un-necessary ?
I am using Perl 5.32 on a Win10 64 bit system.
Regards JC......
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using Win32::SerialPort in a module
by Corion (Patriarch) on Sep 05, 2024 at 07:50 UTC | |
|
Re: Using Win32::SerialPort in a module
by NERDVANA (Priest) on Sep 06, 2024 at 01:02 UTC | |
by jmClifford (Beadle) on Sep 06, 2024 at 09:39 UTC | |
by Corion (Patriarch) on Sep 06, 2024 at 09:46 UTC | |
by hippo (Archbishop) on Sep 06, 2024 at 09:46 UTC | |
by NERDVANA (Priest) on Sep 06, 2024 at 10:36 UTC | |
by Danny (Chaplain) on Sep 06, 2024 at 14:32 UTC | |
|
Re: Using Win32::SerialPort in a module
by dasgar (Priest) on Sep 06, 2024 at 18:46 UTC | |
by jmClifford (Beadle) on Sep 07, 2024 at 03:01 UTC |