必须通过Modbus RTU通过Modbus TCP编写轮询RS485的客户端吗?

问题描述 投票:-1回答:1

我想通过以太网发送转换器请求。我写了一小段php。代码:

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;
}

但是我想用C#编写一个。请在C#中告诉我一个库或一个示例

c# modbus rs485 modbus-tcp
1个回答
0
投票

[有很多可用的库,包括EasyModbusNModbus4,它们都有很好的文档和样本(在最近的stackoverflow问题中提到)。

© www.soinside.com 2019 - 2024. All rights reserved.