Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

PHPmodbus

ue9 modbus php

  • Please log in to reply
5 replies to this topic

#1 X_Project

X_Project
  • Members
  • 4 posts

Posted 01 April 2014 - 06:24 AM

Hi,

 

I have found this solution (PHP Modbus: https://code.google.com/p/phpmodbus/) to communicate with ue9 (via Modbus TCP) using php language.

 

The ModbusMaster.php file uses readMultipleRegisters function to read multiple register (Function 3). This function required the followings parameters:

 

- unitId: usually ID of Modbus device
- reference: Reference in the device memory to read data
 - quantity: Amounth of the data to be read from device.
 
I would like to read the first analog input (AIN0). Could you please help me to insert the corrected parameters?
 
I have tried to use this function:
$recData = $modbus->readMultipleRegisters(1, 0, 1);
 
But it doesn't works. 
 
Thank you so much for your help.
 
Project X
 


#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 01 April 2014 - 12:13 PM

I haven't used this library, but it looks like the call should be:

 

$recData = $modbus->readMultipleRegisters(0, 0, 2);

 

unitId isn't used on the UE9 and is echoed back in the response, the reference is the starting register address which is 0 for AIN0 and the quantity is the number of registers/words (register/word = 16-bits) to read. AIN0 is a 32-bit value, so quantity should be 2.

 

Also, you'll want to do error handling with the call (try/catch) to see what the error is.



#3 X_Project

X_Project
  • Members
  • 4 posts

Posted 03 April 2014 - 03:36 AM

Thank you so much for your reply.

 

I have performed some test on the code. I have tried to implement inside the code the possibility to ping the labjack, and all works well.

 

It seems that the code stops during the opening of the socket. Following you can find the function used to open the socket:

 

 private function connect(){
    // Create a protocol specific socket 
    if ($this->socket_protocol == "TCP"){ 
        // TCP socket
 
        $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
 
    } elseif ($this->socket_protocol == "UDP"){
        // UDP socket
        $this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    } else {
        throw new Exception("Unknown socket protocol, should be 'TCP' or 'UDP'");
    }
    // Bind the client socket to a specific local port
    if (strlen($this->client)>0){
echo "</br>Associo il socket per una porta specifica";
        $result = socket_bind($this->sock, $this->client, $this->client_port);
        if ($result === false) {
            throw new Exception("socket_bind() failed.</br>Reason: ($result)".
                socket_strerror(socket_last_error($this->sock)));
        } else {
            $this->status .= "Bound\n";
        }
    }
    // Socket settings
    socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));
    // Connect the socket
    $result = @socket_connect($this->sock, $this->host, $this->port);
    if ($result === false) {
        throw new Exception("socket_connect() failed.</br>Reason: ($result)".
            socket_strerror(socket_last_error($this->sock)));
    } else {
        $this->status .= "Connected\n";
        return true;        
    }    
  }


#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 03 April 2014 - 12:36 PM

You'll want to make sure the connection is TCP and the port is 502 for Modbus. If that doesn't help, what is the error and what call causes it?

 

With the PHP Modbus library you are using, it looks like the ModbusMaster/ModbusMasterTCP constructor should handle the connection side.



#5 X_Project

X_Project
  • Members
  • 4 posts

Posted 03 April 2014 - 11:12 PM

Yes, I have verified the connection to the labjack and the port (502) too. I don't receive any error, but following step by step the code I have notice that the code stops at this row:

 

$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 

 

Do you have any idea why I'm not able to create the socket TCP?

 

Thank you,

 

Project X



#6 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 04 April 2014 - 11:38 AM

Considering the first socket related call is stopping your code, make sure your PHP environment has sockets installed and enabled.





Also tagged with one or more of these keywords: ue9, modbus, php

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users