File: ModbusTcp.php

Recommend this page to a friend!
  Classes of fuchs yves  >  ModbusTcp  >  ModbusTcp.php  >  Download  
File: ModbusTcp.php
Role: Application script
Content type: text/plain
Description: Complet script to read in a PLC
Class: ModbusTcp
Author: By
Last change: updates
Date: 16 years ago
Size: 6,181 bytes
 

Contents

Class file image Download
<?

error_reporting
(E_ALL);
extract($_REQUEST);

ob_start(); // Bufferisation de tous les 'echo, print....'

echo "<HTML><HEAD>";
echo
"<TITLE>LECTURE MODBUS/TCP</TITLE></HEAD>";


echo
"<BODY bgcolor='#7189AB'>";

$RegistreMax = 125;

!isset(
$regDebut) ? $regDebut = 0 : true;
!isset(
$nbreRegistre) ? $nbreRegistre = 125 : true;
!isset(
$typeRegistre) ? $typeRegistre = "400001" : true;

if (
$regDebut < 1 ) $regDebut = 0;
if (
$nbreRegistre < 1 ) $nbreRegistre = 125;

if ( isset (
$ChangePage) ) {
    if (
$nbreRegistre > $RegistreMax ) $nbreRegistre = $RegistreMax;
    if (
$ChangePage == ">>" ) $regDebut += $nbreRegistre;
    if (
$ChangePage == "<<" ) $regDebut -= $nbreRegistre;
}


// -----------------------------------------------------------------------------------
// PROGRAMME PRINCIPAL
// -----------------------------------------------------------------------------------

$tabTypeReg = array ( "400001", "300001", "100001", "000001" );

echo
"<p align='center'><font color='#FFFFFF' size='5' face='Verdana'><b>LECTURE MODBUS/TCP</font>";
echo
"<p align='center'><font color='#FFFFFF' size='5' face='Verdana'><a href='http://195.6.140.177' target='_blank'><b>dans PLC SCHNEIDER a l'adresse 195.6.140.177</a></font>";

echo
"<form action='".$_SERVER['PHP_SELF']."' method='POST'>";

echo
"<p align='center'><font color='#FFFFFF' size='2' face='Verdana'>";
echo
"TYPE : <select name='typeRegistre' size='1'>\n";
    foreach(
$tabTypeReg as $value ) {
        if (
$typeRegistre == $value ) {
            echo
"<option selected>$value</option>\n";
        } else {
            echo
"<option>$value</option>\n";
        }
    }
echo
"</select>\n<br><br>";

echo
"DEBUT : <input type='text' name='regDebut' value = '" .$regDebut. "' size='5'><br>\n";
echo
"NBRE : <input type='text' name='nbreRegistre' value = '" .$nbreRegistre. "' size='5'><br><br>\n";
echo
"<input type='submit' value='<<' name='ChangePage'><input type='submit' value='SUBMIT'><input type='submit' value='>>' name='ChangePage'>\n";

echo
"</FORM>";

echo
"<hr>";
echo
"<p align='left'><b>Le " . date( "d/m/Y H:i:s") ."</b></br>";

echo
"<p align='center'>\n";

echo
"<table border='0' cellpadding='1' cellspacing='0' bgcolor='#CCCC99'>\n";

//********* EN-TETE du TABLEAU ************
echo "<tr>\n";
    echo
"<td align='center' bgcolor='#CC6633'><font color='#FFFFFF' size='1' face='Verdana'>ADRESSE</font></td>\n";
    echo
"<td></td>\n";
    echo
"<td align='center' bgcolor='#CC6633'><font color='#FFFFFF' size='1' face='Verdana'>DECI.</font></td>\n";

  if (
$typeRegistre == "400001" or $typeRegistre == "300001" ) {
         echo
"<td></td>\n";
        echo
"<td align='center' bgcolor='#CC6633'><font color='#FFFFFF' size='1' face='Verdana'>BINAIRE</font></td>\n";
      echo
"<td></td>\n";
        echo
"<td align='center' bgcolor='#CC6633'><font color='#FFFFFF' size='1' face='Verdana'>HEXA</font></td>\n";
      echo
"<td></td>\n";
        echo
"<td align='center' bgcolor='#CC6633'><font color='#FFFFFF' size='1' face='Verdana'>ASCII</font></td>\n";
    }
echo
"</tr>\n";

$cell_color="#FFFFFF";
$item_txt_color="#000000";

require_once
"class_ModbusTcp.php";

$Plc = new ModbusArray;
$Plc->SetAdIpPLC ("195.6.140.177");
//$Plc->SetDebug();
//$Plc->SetSimulation();


if ( !isset($toggle) ) $toggle = 0;

if (
$toggle == 1 ){
    if (
$ToggleValue == "0" ) {
       
$Plc->WriteCoil( $Adresse, true );
    } else {
       
$Plc->WriteCoil( $Adresse, false );
    }
}

switch (
$typeRegistre ) {
    case
"400001":
       
$temp = $Plc->ReadModbus( (string)("400001" + $regDebut), $nbreRegistre );
    break;
    case
"300001":
       
$temp = $Plc->ReadModbus( (string)("300001" + $regDebut), $nbreRegistre );
    break;
    case
"100001":
       
$temp = $Plc->ReadModbus( (string)("100001" + $regDebut), $nbreRegistre );
    break;
    case
"000001":
       
$temp = $Plc->ReadModbus( (string)("000001" + $regDebut), $nbreRegistre );
    break;
    default:
       
$temp = $Plc->ReadModbus( (string)("400001" + $regDebut), $nbreRegistre );
    break;
}

$bgcolor = '#FFFFFF' ;
$cptrRegistre = 0;

foreach(
$temp as $key => $value) {

    list(
$value_1, $value_2 ) = $Plc->WordToBytes( (int)$value ); //Pour l'affichage en ASCII

   
$cptrRegistre++;

   
$cell_color == '#FFFFFF' ? $cell_color = '#ECECEC' : $cell_color = '#FFFFFF';

  if (
$typeRegistre == "000001" or $typeRegistre == "100001" ) {
        if (
$value ) {
           
$cell_color = '#66FF00' ;
        }
    }

    echo
"<tr>\n";
        echo
"<td align='left' bgcolor='".$cell_color."' nowrap><font color='#000000' size='2' face='Verdana'>[".$key."] </font></td>\n";
        echo
"<td></td>\n";

    if (
$typeRegistre == "000001" ) {
            echo
"<td align='right' bgcolor='".$cell_color."' nowrap><font color='#0000FF' size='2' face='Verdana'><a title='Toggle bit' href=".basename( $_SERVER['PHP_SELF'] )."?toggle=1&Adresse=".$key."&ToggleValue=".$value."&typeRegistre=".$typeRegistre."&regDebut=".($regDebut)."&nbreRegistre=".$nbreRegistre."><b>".$value."</b></a></font></td>\n";
        } else {
            echo
"<td align='right' bgcolor='".$cell_color."' nowrap><font color='#0000FF' size='2' face='Verdana'><b>".$value."</b></font></td>\n";
        }

        if (
$typeRegistre == "400001" or $typeRegistre == "300001" ) {
           echo
"<td></td>\n";
            echo
"<td align='right' bgcolor='".$cell_color."' nowrap><font color='#0000FF' size='2' face='Verdana'><b>".decbin($value)."</b></font></td>\n";
           echo
"<td></td>\n";
           echo
"<td align='right' bgcolor='".$cell_color."' nowrap><font color='#0000FF' size='2' face='Verdana'><b>".strtoupper(dechex($value))."</b></font></td>\n";
        echo
"<td></td>\n";
            echo
"<td align='right' bgcolor='".$cell_color."' nowrap><font color='#0000FF' size='2' face='Verdana'><b>&nbsp;".$value_1.$value_2."</b></font></td>\n";
        }

    echo
"</tr>\n";
}

echo
"</table>\n";

$Plc->ModClose();
unset (
$Plc);

echo
"<div bgcolor='#FFFFFF'><pre>";

echo
"<br><a href=".basename($_SERVER['PHP_SELF'])."?show=1>Show Source</a><br>";

if ( !isset(
$show) ) $show = 0;
if (
$show == 1 ){
    echo
"<pre align='left' bgcolor='#FFFFFF'>";
   
show_source( basename( $_SERVER['PHP_SELF'] ) );
}

echo
"\n</BODY></HTML>";

?>


For more information send a message to info at phpclasses dot org.