-1

I want to send a converter request via ethernet. I write a small piece of php. code:

public function actionModbus()
{

    $errors = array();
    $warnings = array();

    $connection = BinaryStreamConnection::getBuilder()
        ->setPort(80)
        ->setHost('192.168.56.205')
        ->setReadTimeoutSec(3) // increase read timeout to 3 seconds
        ->build();

    $startAddress = 2165;
    $quantity = 1;
    $slaveId = 1; // RTU packet slave id equivalent is Modbus TCP unitId

    $tcpPacket = new ReadHoldingRegistersRequest($startAddress, $quantity, $slaveId);
    $rtuPacket = RtuConverter::toRtu($tcpPacket);
    try {
        $binaryData = $connection->connect()->sendAndReceive($rtuPacket);
        $warnings[] = 'RTU Binary received (in hex):   ' . unpack('H*', $binaryData)[1] . PHP_EOL;

        $response = RtuConverter::fromRtu($binaryData);
        $warnings[] = 'Parsed packet (in hex):     ' . $response->toHex() . PHP_EOL;
        $warnings[] ='Data parsed from packet (bytes):' . PHP_EOL;
        print_r($response->getData());
    }catch (\Exception $exception)
    {
        $errors[] = $exception->getMessage();
        $errors[] = $exception->getLine();
    }finally {
        $connection->close();
    }
    $result = array('errors'=>$errors,'warnings'=>$warnings);

    Yii::$app->response->format = Response::FORMAT_JSON;
    Yii::$app->response->data = $result;
}

But I want to write one in C#. Please tell me a library or an example in C#

sticky bit
  • 36,626
  • 12
  • 31
  • 42
rasul
  • 9
  • 1

1 Answers1

0

There are quite a few libraries available including EasyModbus and NModbus4 both of which have good documentation and samples (and are mentioned in recent stackoverflow questions).

Brits
  • 14,829
  • 2
  • 18
  • 31