user27972404
user27972404

Reputation: 1

How to read power data on teltonika trb145 gateway - PHP + Socket?

Good morning,

I am facing difficulty obtaining energy data from meters that are connected to teltonika trb145 gateways via RS485.

I'm using the OverIP feature, available on the equipment in client mode:

enter image description here

Feature documentation: https://wiki.teltonika-networks.com/view/Serial_OverIP_communication_between_Server_and_Client

I created my server application, it receives connection from the gateway, but does not send data, or maybe

I have to request the records, but I was not successful.

Part of my code below:

<?php

if (php_sapi_name() !== 'cli') {
    echo 'Should be used only in command line interface';
    return;
}

require 'vendor/

autoload.php';
require './logger.php';

ob_implicit_flush();


use ModbusTcpClient\Packet\ErrorResponse;
use ModbusTcpClient\Packet\ModbusApplicationHeader;
use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersRequest;
use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\RequestFactory;
use ModbusTcpClient\Utils\Packet;
use ModbusTcpClient\Utils\Types;
use React\Socket\SocketServer;
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersRequest;
use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersResponse;
use ModbusTcpClient\Packet\ResponseFactory;


// Install: 'composer require react/socket:^1.6'
// OPTIONAL: install PHP extension ('ev', 'event' or 'uv') for better support of event loop within PHP.

$address = getenv('MODBUS_SERVER_BIND_ADDRESS') ?: '0.0.0.0';
$port = getenv('MODBUS_SERVER_PORT') ?: '4000';

$logger = new EchoLogger();

$loop = React\EventLoop\Loop::get();
$socket = new SocketServer("{$address}:{$port}", [], $loop);

$socket->on('connection', function (React\Socket\ConnectionInterface $conn) use ($logger, $loop) {
    $logger->debug($conn->getRemoteAddress() . ": connected: ");
    $address_remote = explodeAddress($conn->getRemoteAddress());
 
    // buffer for received bytes for that connection
    $conn->write("Server ready:{}\n");
    $receivedData = b'';

    $conn->on('data', function ($data) use ($conn, $logger, &$receivedData) {
        //$logger->debug($data);
        $logger->debug($conn->getRemoteAddress() . ": received: " . unpack('H*', $data)[1]);

I ran the code shared above, I receive a connection from the gateway, but the data is not sent or I am unable to connect to the gateway IP and receive the data from the modbus registers that I need.

Upvotes: 0

Views: 74

Answers (0)

Related Questions