Jobin
Jobin

Reputation: 8282

Reading Serial Port in Perl

I'm bit tired of reading serial port, using Perl first of all Perl is not my primary development tool, I'm from PHP background. So please guide me in a right way. my intention is to read the data from a weight machine to a web page, I already did a lot of research on this topic.

The browser app cannot read serial port data directly so I choose the user end machine can be installed with a server (Apache) and do the task with PHP and web application can read the result using an Ajax call to localhost url the idea is working great on Linux machines but on windows PC it stuck.

I understand PHP can't read data from serial port in windows like Linux so I choose Perl to do the stuff. I did some search an got few codes and last 8 hours I'm fighting with that :(

#!E:\xampp\perl\bin\perl.exe
# The above line is perl execution path in xampp
# The below line tells the browser, that this script will send html content.
# If you miss this line then it will show "malformed header from script" error.

  print "Content-type: text/plain\n\n";
  # use strict; use warnings;
  use Win32;
  require 5.003;
  use Win32::SerialPort qw( :STAT 0.19 );
  print "here";
  $PortObj = new Win32::SerialPort ("COM3") || die "Can't open COM3: $^E\n";
  print "here";
  $PortObj->user_msg(ON);
  $PortObj->databits(8);
  $PortObj->baudrate(9600);
  $PortObj->parity("none");
  $PortObj->stopbits(1);
  $PortObj->handshake("rts");
  $PortObj->buffers(4096, 4096);

  $PortObj->error_msg(1);  # prints major messages like "Framing Error"
  $PortObj->user_msg(1);  

  $PortObj->write_settings || undef $PortObj;


  print "here";
  # $PortObj->save($Configuration_File_Name);
  $PortObj->read_interval(100);    # max time between read char (milliseconds)
  $PortObj->read_char_time(5);     # avg time between read char
  $PortObj->read_const_time(100);

   print "here";
   $string_in = $PortObj->read("200");

   print $string_in
  # $PortObj->baudrate(300);
  # $PortObj->restart($Configuration_File_Name);  # back to 9600 baud

  $PortObj->close || die "failed to close";
  undef $PortObj;  

The code is not working but on forum and many other places it says working like here , here, this and this.

Im unable to find what is this means "use Win32::SerialPort qw( :STAT 0.19 );" is that something like importing that library with version number. ?

I'm already installed Xampp with Perl and activePerl Win32::serialport 0.22 and its API but when I run the code it return an error like

Error message:
End of script output before headers: serial.pl 

500 Error

Any idea ??

Solved

For reading section I use the code below.

# warn "here";
  my ($ser) = @_;
  my ($numChars, $c);
  my $line = '';
  do {
    do {
      ($numChars, $c) = $PortObj->read(1);
      #print "$numChars read=$c\n";
    } while ($numChars < 1);
    # print $c;
    $line .= $c;
  } while ($c ne "\n");
   print $line;

The code works fine from CLI so I just put a PHP file to read the perl script and return the data.

Upvotes: 0

Views: 3695

Answers (1)

Borodin
Borodin

Reputation: 126732

use Win32::SerialPort qw( :STAT 0.19 );

This is the same as

use Win32::SerialPort ':STAT', '0.19';

The :STAT imports a number of useful status constants, and is explained under Exports in the documentation for the module

The 0.19 is part of Perl, and is insisting that the version of Win32::SerialPort must be 0.19 or later. The current version is 0.22. If you change 0.19 to 0.23 then you will see the effect it has

Upvotes: 1

Related Questions