使用者工具

網站工具


tech:modbus

Modbus TCP 測試

Master / Slave 模擬測試

  1. Slave 模擬設備環境 Exp. 192.168.11.247 : CentOS8 (PVE6 CT template)
    yum install wget tar
    wget https://www.modbusdriver.com/downloads/diagslave.tgz
    tar -xvf diagslave.tgz
    cd diagslave/linux_x86-64
    ./diagslave -m tcp

    看執行畫面

  2. Master 模擬控制環境 : CentOS8 (PVE6 CT template)
    yum install wget tar
    wget https://www.modbusdriver.com/downloads/modpoll.tgz
    tar -xvf modpoll.tgz 
    cd modpoll/linux_x86-64
    ./modpoll -m tcp 192.168.11.247

    看執行畫面

  • 設定開機可以直接啟動
    crontab -e

    編輯內容如下:

    @reboot /root/diagslave/linux_x86-64/diagslave -m tcp > /tmp/t.log
  • 重開機後可以看到 /tmp/t.log 出現執行的紀錄訊息

使用 Perl 開發 Client(Master) 程式

  1. 安裝環境 CentOS8 (PVE6 CT Template)
  2. 安裝相關模組套件
    yum install perl gcc
    perl -MCPAN -e shell
    upgrade
    force install Net::Server::MultiType
    install Device::Modbus::TCP
    exit
  3. 撰寫簡單讀取測試程式 t.pl
    #! /usr/bin/perl
     
    use Device::Modbus::TCP::Client;
    use Data::Dumper;
    use strict;
    use warnings;
    use v5.10;
     
    my $client = Device::Modbus::TCP::Client->new(
        host => '192.168.1.34',
    );
     
    my $req = $client->read_holding_registers(
        unit     => 3,
        address  => 2,
        quantity => 1
    );
     
    say Dumper $req;
    $client->send_request($req) || die "Send error: $!";
    my $response = $client->receive_response;
    say Dumper $response;
     
    $client->disconnect;
    perl t.pl

    看執行結果

  4. 撰寫簡單寫入測試程式 t1.pl
    #! /usr/bin/perl
     
    use Device::Modbus::TCP::Client;
    use Data::Dumper;
    use strict;
    use warnings;
    use v5.10;
     
    my $client = Device::Modbus::TCP::Client->new(
        host => '192.168.11.247',
    );
     
    # Write reg0 -> 12345
    my $req = $client->write_single_register(
        unit     => 1,
        address  => 1,
        value    => 15
    );
    
    say Dumper $req;
    $client->send_request($req) || die "Send error: $!";
    my $response = $client->receive_response;
    say Dumper $response;
    
     
    $client->disconnect;
  • 讀寫入的方式有以下幾種 : 參考網址 - https://metacpan.org/pod/Device::Modbus::Client
    • $req = $client→read_coils(unit,address,quantity);
    • $req = $client→read_discrete_inputs(unit,address,quantity);
    • $req = $client→read_holding_registers(unit,address,quantity);
    • $req = $client→read_input_registers(unit,address,quantity);
    • $req = $client→write_single_coil(unit,address,value);
    • $req = $client→write_single_register(unit,address,value);
    • $req = $client→write_multiple_coils(unit,address,value); Example
    • $req = $client→write_multiple_registers(unit,address,value); Example
    • $req = $client→read_write_registers(unit,read_address,read_quantity,write_address,values); Example

使用 PHP 開發 Client(Master) 程式

  1. 安裝環境 CentOS8 (PVE6 CT Template)
  2. 安裝相關模組套件
    yum install php php-json
    mkdir -p /root/php-modbus
    cd /root/php-modbus
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('sha384', 'composer-setup.php') ===  '572cb359b56ad9ae52f9c23d29d4b19a040af10d6635642e646a7caa7b96de717ce683bd797a92ce99e5929cc51e7d5f') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"
    ./composer.phar require aldas/modbus-tcp-client
    ./composer.phar require react/socket:^0.8.11
  3. 撰寫簡單讀取測試程式 t.php
    mkdir -p /root/php-modbus/example
    cd /root/php-modbus/example
    vi t.php
    
    

參考網址

tech/modbus.txt · 上一次變更: 2020/08/29 08:42 由 jonathan_tsai