Giter VIP home page Giter VIP logo

phpmodbus's Introduction

PayPayl donate button

Phpmodbus

Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP. This is the Google code project follower at https://code.google.com/p/phpmodbus/

Implemented features:

  • Modbus master
  • FC1 - Read coils
  • FC2 - Read input discretes
  • FC3 - Read holding registers
  • FC4 - Read holding input registers
  • FC5 - Write single coil
  • FC6 - Write single register
  • FC15 - Write multiple coils
  • FC16 - Write multiple registers
  • FC23 - Read/Write multiple registers

Example:

require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php'; 

// Modbus master UDP
$modbus = new ModbusMaster("192.168.1.1", "UDP"); 
// Read multiple registers
try {
    $recData = $modbus->readMultipleRegisters(0, 12288, 5); 
}
catch (Exception $e) {
    // Print error information if any
    echo $modbus;
    echo $e;
    exit;
}
// Print data in string format
echo PhpType::bytes2string($recData); 

For more see [http://code.google.com/p/phpmodbus/downloads/list documentation] or [http://code.google.com/p/phpmodbus/wiki/FAQ FAQ].

Note:

  • The PHP extension php_sockets.dll should be enabled (server php.ini file)

phpmodbus's People

Contributors

bitdeli-chef avatar krakorj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpmodbus's Issues

php 8.1 not working conection error

there are connection issues on php version 8.1 , (also php_socket enabled ) anyone has the same issue ?
after the update from php 7.2 to 8.1 my process stop working , the only way to get working is to switch to the previous version (7.2) .

Byte Missing

Hello,

I try to read the status Register (30201) of a Sunny Boy. Expected answer is 307

I do have one inverter, providing me the correct answer.

Another (older) Device brings up an error:
readMultipleRegisters: START
Connected
Packet: ac4e00000006030375f90002
Send
Wait data ...
Data received (12)
Packet: ac4e00000007030304000001
Modbus response error code: NOERROR
Disconnected
readMultipleRegisters: DONE

I have modified the library showing me the amount of recived bytes. If the problem orrurs, only 12 bytes are recived instead of 13.
The 12 Bytes will chrash readMultipleRegistersParser on line 506 $data[$i] = ord($packet[9+$i]); accessing the array out of range.
Notice: Uninitialized string offset: 12

Of course, then the result is garbage (256)

I have running pymodmon in parallel, giving me the correct value for all inverters.

I would like to support, but modbus is just not my area. But I can help on questions.

HowTo convert string to bytes

Im trying to convert a string to bytes with a script like
`$str = "String";

$arr = array();

for ( $pos=0; $pos < strlen($str); $pos ++ ) {

$byte = substr($str, $pos);

$arr[$pos] = ord($byte);

}

print_r($arr);
`

The output array looks like

Array ( [0] => 83 [1] => 116 [2] => 114 [3] => 105 [4] => 110 [5] => 103 )

If i receive the same string from my Wago 750 it looks like

Array ( [0] => 83 [1] => 1 [2] => 114 [3] => 116 [4] => 110 [5] => 105 [6] => 0 [7] => 103 )

How do i get the bytes in the right order?

combineBytes bug?

Hard to believe, because I'm seeing this in all the different forks of this project, but I think in combineBytes the byte order is wrong when $endianness is falsy:

if ($endianness == 0)
            $value = (($data[3] & 0xFF)<<16) |
                    (($data[2] & 0xFF)<<24) |
                    (($data[1] & 0xFF)) |
                    (($data[0] & 0xFF)<<8);

Errrr... no? That makes no sense. Shouldn't it just be a mirrored version of big endian:

if ($endianness == 0) {
	$value = (($data[3] & 0xFF)) |
		(($data[2] & 0xFF) << 8 ) |
		(($data[1] & 0xFF) << 16 ) |
		(($data[0] & 0xFF) << 24 );

What am I missing? Is no one ever using integers?

Real and Integer Values

I cant read real and integer value from the plc. This program can only read the data in bytes(0 to 255). Kindly guide me to read the real and integer values

Moxa ioLogik E1212

I have a bit situation with Moxa ioLogik E1212 module. Half of the digital outputs are configured pulse outputs and half regular DO like this: $data = array([M1 STEP], [M1 ON/OFF], [M2 STEP], [M2 ON/OFF], [M3 STEP], [M3 ON/OFF], [M4 STEP], [M4 ON/OFF]);

DO´s works fine with this:

$data = array(TRUE);
try {
// FC15
$modbus->writeMultipleCoils(0, 0001, $data);
$modbus->writeMultipleCoils(0, 0003, $data);
$modbus->writeMultipleCoils(0, 0005, $data);
$modbus->writeMultipleCoils(0, 0007, $data);
}

but pulse outputs are not:

$data = array(TRUE);
try {
// FC15
$modbus->writeMultipleCoils(0, 0016, $data);
}

Here you have Moxa register & address map:

image

FC3 read 2x8bit registers instead of 1x16bit registry

Hi all,

As mentioned in topic: I'm using FC3 and in return there's only 2x8bit registers (1 variable) instead of 1x16bit registry. Could anyone help me fix this issue? Thanks in advance.

PS. Modifying in script isn't an option.

Fatal Error: Uncaught Exception in readCoils

I have to run phpmodbus lib in my SocketServer. So after a few hours, the script canceld with the follow error Message

Fatal Error: Uncaught exception 'Exception' with Message 'Watchdog time expired [ 5 sec ]!!! Connection to [IP ADRESS] is not established.' in [xxx]/phpmodbus_0.0.r106/ModbusMaster.php:172
Stack trace:
#0: [xxx]/phpmodbus_0.0.r106/ModbusMaster.php(240):ModbusMaster->rec()
#1: [xxx]/class.WagoWebsocket.php(58): ModbusMaster->readcoils(0, '12288', 4)
#2: [xxx]/class.WagoWebsocket.php(58): ModbusMaster->checkbit()
#3: [xxx]/dienst.php(75): WagoWebsocket->run()
#4: {main} thrown in [xxx]phpmodbus_0.0.r106/ModbusMaster.php on line 172

I can use client port only one time in 60 seconds.

Hi, I have a problem with this code. Im triying to read modbus tcp data from modbus slave program in my computer. When I start the project it is working but when I refresh the page giving port error. And I am changing the client_port to +=1 its working. How can I fix it?

Troubleshooting Output

I am running the following:

$modbus = new ModbusMaster("192.168.1.18", "TCP");
$recData = $modbus->readMultipleRegisters(1, 2160, 2);
print_r($recData); 
echo $modbus->status;
echo 'VALUE: '.PhpType::bytes2float($recData, 1);

The value that I SHOULD be getting back is 2404.00. However I am getting this output:

Array
(
    [0] => 69
    [1] => 22
    [2] => 64
    [3] => 0
)
readMultipleRegisters: START
Connected
Packet: 967900000006010308700002
Send
Wait data ...
Data received
Packet: 96790000000701030445164000
Modbus response error code: NOERROR
Disconnected
readMultipleRegisters: DONE
VALUE: 2.00421667099

I am not sure what else to do to troubleshoot this. In using a 3rd party Modbus GUI for Mac (oModbusMaster) I am able to get the correct value using the following settings:

  • Starting Register = 2160
  • Number of Registers = 2
  • Data Type = Floating Point IEEE 754 Single Precision 32 bits inverse

Any idea how I can get the correct value?

Deprecated using php7.3

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; ModbusMaster has a deprecated constructor in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 38

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 477

Deprecated: Non-static method IecType::iecINT() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 479

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 51

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 52

Deprecated: Non-static method IecType::iecINT() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 480

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 51

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 52

Deprecated: Non-static method IecType::iecINT() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 484

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 51

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 52

Deprecated: Non-static method IecType::iecINT() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 485

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 51

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 52

Deprecated: Non-static method IecType::iecINT() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 486

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 51

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/IecType.php on line 52

Deprecated: Non-static method IecType::iecBYTE() should not be called statically in /var/www/html/phpmodbus/Phpmodbus/ModbusMaster.php on line 487

Memory issue for extended run time

Not sure how much of an issue this may be considered if at all. I use this to do a continues collection on a device. $this->status .= is constantly appended on eventually causing the process to hit its memory limit. I just clear that variable between collections now but I thought I would mention it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.