PHP use RS232 serial communication to send file

PHP use RS232 serial communication to send file


<?php
// --------------------------------------------------------------------------
// File name   : RS232_Server.php
//
//Procedure Description: 
//RS232_Client the procedures and the demonstration of a complete system in PHP to display 
//the RS232 serial communications applications. 
//Process to achieve a foundation of integrity, however the RS232 communication protocol 
//(HQB232), communication protocol format is as follows: 
//Agreement: 
//C-> S 01 //request for communication 
//S-> C 02 //response communications 
//C-> S LEN DATA //LEN said data (DATA) that the actual data length of DATA 
//C-> S 03 //end communication 
//Note: S, said that under the PC C-bit machine 
//HQB232 said HonestQiao the basis of presentation (Base) RS232 communication protocol, 
//including the agreement 
//Request and response, data frame structure. 
//Demonstration of the process for request and response communications, document
//delivery (data frame), the end of communication 
//Data frame structure of the current data for the current length of the HEX data 
//string (2 complement)
// --------------------------------------------------------------------------
set_time_limit(0);
exec('mode COM1: baud=9600 data=8 stop=1 parity=n xon=on');

$fd = dio_open('COM1:', O_RDWR);
if(!$fd)
{
        die("Error when open COM1");
}

$ff = dio_stat($fd);print_r($ff);
echo "HQB232 SERVER is listenning on COM1\n";
/// read
$len = 2;
$t=0;while (($t++)<1000)
{
        $data = dio_read($fd, $len);
        if ($data) {
                if($data==chr(0).chr(1)){
                        echo "S_RECV:01\n";
                        echo "S_SEND:02\n";
                        dio_write($fd,chr(0).chr(2));
                        break;
                }
        }
}

$handle = fopen("E:\\RS232\\TEST\\S_WAPMMonitor.exe", 'w');
/// read
$len = 2;
$t=0;while ($handle && ($t++)<1000)
{
        $len = 2;
        $data = dio_read($fd, $len);
        if($data==chr(0).chr(3)){
                echo "S_RECV:03\n";
                break;
        }
        elseif ($data) {
                $len = hexdec($data);
                if($len>0)
                {
                $data = dio_read($fd, $len);
                if($data){
                        fwrite($handle, str2hex($data));
                        echo "S_RECV:($len) ".microtime()."\n";
                }
                }
        }
}
fclose($handle);
dio_close($fd);

function str2hex($string)
{
        $str = '';
        for($i=0; $i<strlen($string); $i+=2){
                $str .= chr(hexdec(substr($string,$i,2)));
        }
        return $str;
}
?>

<?php
// --------------------------------------------------------------------------
// File name   : RS232_Client.php
// Procedure Description: 
// RS232_Server the procedures and the demonstration of a complete system in PHP 
// to display the RS232 serial communications applications. 
// Process to achieve a foundation of integrity, however the RS232 communication 
// protocol (HQB232), communication protocol format is as follows: 
// Agreement: 
// C-> S 01 // request for communication 
// S-> C 02 // response communications 
// C-> S LEN DATA // LEN said data (DATA) that the actual data length of DATA 
// C-> S 03 // end communication
// --------------------------------------------------------------------------
set_time_limit(0);
exec('mode COM2: baud=9600 data=8 stop=1 parity=n xon=on');

$fd = dio_open('COM2:', O_RDWR);
if(!$fd)
{
        die("Error when open COM2");
}

$ff = dio_stat($fd);print_r($ff);
echo "HQB232 CLIENT is start on COM2\n";
dio_write($fd,chr(0).chr(1));echo "C_SEND:01\n";
$len = 2;
$t=0;while(($t++)<1000)
{
        $data = dio_read($fd, $len);
        if($data==chr(0).chr(2)){
                echo "C_RECV:02\n";
                break;
        }
}
/*
$len = 2;
$t=0;while(($t++)<10)
{
        $sdata = sprintf("%03d",$t) . "=" . microtime() . " (" . randomkeys(rand(0,35)) . ")";
        $slen = strlen($sdata);
        $stxlen = sprintf("%02d",$slen);
        dio_write($fd,"$stxlen");
        dio_write($fd,"$sdata");echo "C_SEND:($stxlen)$sdata\n";
        //sleep(1);
}
*/

$handle = fopen ("E:\\RS232\\TEST\\WAPMMonitor.exe", "rb");
$data = "";
while ($handle && !feof($handle)) {
  $sdata = fread($handle, 127);
  $sdata = hex2str($sdata);
  $slen = strlen($sdata);
  $stxlen = sprintf("%02X",$slen);
  dio_write($fd,"$stxlen");
  dio_write($fd,"$sdata");echo "C_SEND:($slen) ".microtime()."\n";
  usleep(300);
}
fclose($handle);

dio_write($fd,chr(0).chr(3));echo "C_SEND:03\n";
dio_close($fd);

function randomkeys($length)
  {
   $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
   for($i=0;$i<$length;$i++)
   {
     $key .= $pattern{rand(0,35)};
   }
   return $key;
}

function hex2str($hexstring)
{
        $str = '';
        for($i=0; $i<strlen($hexstring); $i++){
                $str .= sprintf("%02X",ord(substr($hexstring,$i,1)));
        }
        return $str;
}
?>

QUOTE:
1. Code among: Please modify to your own practical test file
$handle = fopen (“E:\\RS232\\TEST\\WAPMMonitor.exe”, “rb”);

$handle = fopen(“E:\\RS232\\TEST\\S_WAPMMonitor.exe”, ‘w’);

2. Into the test, you can send a text file, binary file.

3. HQB232 agreement to amend the original directive 01,02,03 revised
to chr (0). Chr (1), chr (0). Chr (2), chr (0). Chr (3) to form the

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “PHP use RS232 serial communication to send file”

Leave a Reply

Name:
Email:
Website:
Comment:
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>