in reply to Re: Win32::SerialPort on Win10 issue
in thread Win32::SerialPort on Win10 issue

Looks like you are well on the way. Yes, Win32:SerialPort should work fine for this application.

I would print the configuration to make sure you know what it is. 38400 8N1 is common for these types of devices - looks plausible. You can just put explicit <CR> <LF> characters into the write string to prevent any ambiguities with "what does \n mean?". A single <CR> character should be enough for the modem to recognize end of command. But, you could try some permutations. Power on/off box between trials (make sure its brain is starting from zero). I would wait 1 second for ATI command to be sent before trying to read the response - the answer should be waiting when you get there.

It could be that your IDE is affecting things somehow. Try running your test program from the command line.

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"; $port->write("ati\x0D"); #keep simple for now single <CR> #also try \x0D\x0A <CR><LF> #try ATI\x0D (AT spec says upper case) sleep 1; #should allow for whole string to be sent #reply should be waiting #not clear how much buffering #goal is get a character, any character at first while(1) { my $line = $port->lookfor(); #can be multi-character response if( $line ) { print "[$line]"; # Some data processing takes place } else { sleep(1); # Allocate time for other processes to run print "."; } }
Update: perhaps try the streamline method, but the above should work.

update2: I think I understand what you have now. A USB gizmo that has 16 pin connector to the car on the other side. You don't actually have a serial cable so, serial cable breakout box won't help. However, Wireshark freeware for Windows can capture USB traffic CaptureSetup/USB. I would setup with your terminal emulator and capture USB traffic and then do the same with the Perl program.

Replies are listed 'Best First'.
Re^3: Win32::SerialPort on Win10 issue
by jmClifford (Beadle) on Jul 05, 2024 at 04:22 UTC

    Hi. Thanks to all for the help.

    I did try Serial Port Monitor (2 weeks trail). Wireshark looks too involved (and I previously have used on TCP/IP only). The Serial Port Monitor gives the response below, when I used the Realterm connection;

    [05/07/2024 13:43:32] - Open port COM5 (C:\Program Files (x86)\BEL\Rea +lterm\realterm.exe) # this occurs on connection with SW [05/07/2024 13:04:58] Written data (COM5) 61 74 69 0d ati. + [05/07/2024 13:04:58] Read data (COM5) 45 4c 4d 33 32 37 20 76 31 2e 35 0d 0d 3e ELM327 v1.5..> + [05/07/2024 13:05:05] - Close port COM5 # this occurs on Realterm shutdown

    While when I use the PERL connection;

    [05/07/2024 13:05:12] - Open port COM5 (C:\Strawberry\perl\bin\perl.ex +e) # occurs with the creation of the object [05/07/2024 13:05:28] - Close port COM5 # occurs with Eclipse shutting down the running Perl

    My only conclusion (at this stage) is that I am failing to connect to COM5 while there is nothing suspicious about the USB unit (yes it is a On Board Diagnostic unit used with automobiles with ~16 pin connector and no access to RS232).

    Regards JC....

      Hi. I did eventually find out the main issue with my previous program not working. The Win32::SerialPort as presented does not work with 64 bit operation (Perl). However there is a fix provided at (as linked to by Discipulus):

      https://rt.cpan.org/Public/Bug/Display.html?id=113337#txn-1691722

      This fix basically increases the sizes of some parameters in referencing a module (the memory mapping involved with this process).

      My current working program is:

      #! C:\perl\bin\perl.exe use strict; use warnings; use Win32::SerialPort; use Time::HiRes qw(usleep); my $port_name = 'COM5'; #my $config_file = 'setup.cfg'; # use this to configure from the fi +le my $port = new Win32::SerialPort( $port_name ) || die "Unable to open +: $^E\n"; # $^E EXTENDED_OS_ERROR #my $port = new Win32::SerialPort($port_name, $config_file) || die "Un +able 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); # 7 8 $port->stopbits(1); # 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 \n"; $port->write_settings || undef $port; # A report out to this 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"; # 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"."\r"); #$port->write("ati\x0D\x0A"); # carriage return and line feed: n +o different #$port->write("ate0"."\r"); usleep 0; my $debug = 1; my $cmd = " "; while ($cmd ne "quit") { print "Input command: "; $cmd = <STDIN>; chomp $cmd; if ($cmd eq "quit") {next;} $port->write($cmd."\r"); my $loop = 1; while( $loop ) { usleep(200000); # 0.2 of a second my $response = $port->input; chomp $response; 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; next; } print "."; } }

      Regards JC.....

      Ok. This is progress. we learn that lower case AT commands are ok and that the line ending is \x0D. Use that instead of \n.

      Did you follow my suggestion of reading the port settings (handshake, etc)? Make sure that the port is configured and "happy" - if not it won't send data. I would not introduce the complication of a config file at this early stage.

      Again, trying running outside of Eclipse. That could make a difference.

      please show the exact code that you are using for this testing.