Main menu
WalkswithMePerl PHPRead serial port data using Perl and PHP under windows

Read serial port data using Perl and PHP under windows

Read serial port data using Perl is very easy compare to PHP in Windows OS. The PHP serial port reading has some complication in Windows Os. In Linux serial port reading is very simple using PHP , but on Windows it makes more difficult that is why I choose Perl.

Using Perl we can easily done this, with the help of Win32 Serial port library. make sure your windows machine is installed with Perl and Win32 serial port lib. the easiest option is just install the Active Perl and install with basic options. Now you have to install Win32 Serial Port with administrator privileged terminal.


ppm install Win32-SerialPort

Read serial port data using perl is just about few lines of code. you just need to make sure which directory the Perl get installed , and that you have to specify at the top of the line in the script. If you’re using Xampp it will come with Apache PHP Mysql and Perl so there is no need of additional Perl setup. so you can run the Perl script via browser. Or you can run it using CLI.

The following code first line you should to your Perl.exe file. also the Port is set to COM3 that you have to check once you connect the serial device.


#!C:\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 ':STAT', '0.19';
my $PortObj;
my $string_in;
my $port;
my $baudrate;
$port = "COM3"
$baudrate = "9600";
$PortObj = new Win32::SerialPort ($port) || die "Can't open $port: $^E\n";
# $PortObj->user_msg(ON);
$PortObj->databits(8);
$PortObj->baudrate($baudrate);
$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;
# $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);
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;
$PortObj->close || die "failed to close";
undef $PortObj;

save the above script as serial.pl file and run it using localhost or CLI.


perl serial.pl

Once the device is connected you can read the out put in CLI. In my case I connected weigh machine with USB port and my port number is COM3.

Yea read serial port data using Perl is done !. If you’re in Linux OS you can use my Read Serial Port with PHP script for reading serial port, my intention is to do with PHP, but on Windows PHP can’t read serial port data, So I choose Perl. Ok now I have a Perl solution for reading serial port data on Windows. How can I use it in PHP ?

Reading Serial port data using PHP under Windows ?

Yea with the help of Perl script we can implement serial port data reading within PHP. you have to call the Perl script from PHP using following methods.


header('Access-Control-Allow-Origin: *');
$port = $_REQUEST['port'];
$baudrate = $_REQUEST['baudrate'];
$perl_result = `perl serial.pl $port $baudrate`;
$data = str_replace("Content-type: text/plain","",$perl_result);
echo trim($data);

either you can pass port number and baudrate from PHP to Perl or you can set it in Perl script , but its better to passing values bcoz some device port may be different. So in that case small alteration in your Perl script like below.


$port = $ARGV[0];
$baudrate = $ARGV[1];

Yea we are retrieving those values from PHP to Perl. Passing variable from PHP to Perl is easy. In PHP you can use simple Perl serial.pl to execute a Perl script or you can use exec function.

I hope you guys found this article useful in many cases. like read serial port data using Perl or PHP under windows. Executing Perl script from PHP  and Passing variables to Perl from PHP script.

Conclusion: Using this idea you can read the serial port data in Perl/PHP under windows means you can get the result in client browser only thing is client PC should be installed with this Apache and Perl setup and Win32 Serial Port lib.

You can download the full working kit only thing you have to pass values as query string to the php page.

Download1487 downloads

3 thoughts on “Read serial port data using Perl and PHP under windows

  1. Thank for your article.
    I have installed XAMPP parked with Perl and downloaded the zipped package you attached to this article but the test file doesnt is only showing undefined index for “port” and “baudrate”.
    Do I still need to install Win32 Serial Port or XAMPP is sufficient. Please help with where I am missing it.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

FacebookTwitterGoogle+RSS