libmodbus -Linux、Mac OS、FreeBSD、Windows用のModbusライブラリ

無料無料技术资料

这篇内容可直接阅读,适合用于基础学习和検索引流。

libmodbus -Linux、Mac OS、FreeBSD、Windows用のModbusライブラリ

概要:概要

libmodbusは、従来のRS-232、RS-422、RS-485、およびイーサネットデバイスをサポートする高速でポータブルなModbusライブラリです。

インストールする。

Linux、MacOS、Free BSD、Win 32などはパッケージマネージャから直接インストールできます。libmodbus

libmodbus公式ダウンロード:https://libmodbus.org/releases/libmodbus-3.1.7.tar.gz
Githubのダウンロード:https://github.com/stephane/libmodbus.git

//解凍ディレクトリへ
cd libmodbus-3.1.7
//コンパイルオプションを設定します(ARMの場合、--buildはシステムアーキテクチャ、--hostはシステムツールチェーンをコンパイル、--prefixは出力ディレクトリ、オプションです)。
./ configure--build = i 686--host = arm-linux--enable-static-prefix = install path//
//インストールのコンパイル
make make installを

例:例

RTU master

#include stdio.h>
#include stdlib.h>
#include memory.h>
#include "modbus.h"

int main void
{ 
    int i = 0;
    int regs = 0;
    mod_t *ctx = NULL;
    uint16_t tab_reg64] = {0};

    //Open Port:ポート、ボーレート、チェック·ビット、データ·ビット、停止ビット
    ctx = modbus_new_rtu(“/dev/ttyUSB 0”,96 00,'N',8,1);
    //スレーブアドレスを設定
    mod_set_slave ctx 1;
    //シリアルモードの設定(オプション)
    mod_r_set_serial_mode ctx、MODBUS_RTU_485;
    //RTSの設定(オプション)
    mod_r_set_rts ctx、MODBUS___UP;
    //コネクションの確立
    if modbus_connect ctx == -1 {
        ff stderr "Connexion ed %s\n" mod_strerror no;
        mod_free ctx;
        リターン-1;
    }
    //応答遅延の設定(オプション)
    mod_set__out ctx 0 100000;

    while 1
    {
        memset tab_0 of tab_;
        //読み取りレジスタ設定:レジスタアドレス、数量、データバッファ
        regs = mod_read_registers ctx 0 20 tab_;
        if rc == -1 {
           ff stderr "%s\n" mod_strerror no;
           継続的に;
        }
        f
        for i = 0; i 20; ++i
        {
            printf " %#x" tab_reg i];
        }
        printf "\n";
        sleep(1);
    }
}
  • RTU-Masterプロセスの概要:
  1. modbusポインタの初期化
  2. ステーションIDの設定
  3. 接続の確立
  4. 読み出しホールドレジスタ/入力レジスタ/入力/コイル入力
  5. 単一レジスタ/複数レジスタ/複数ビットデータの書き込み
  6. 接続を閉じる

RTU slave

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <error.h>
#include <memory.h>
#include "modbus.“H”

int main void
{ 
    int i = 0;
    rc = 0;
    mod_t *ctx = NULL;
    mod_m_t *mb_m = NULL;
    uint8_t query MODBUS_TCP_MAX_ADU_LENGTH];

    //Open Port:ポート、ボーレート、チェック·ビット、データ·ビット、停止ビット
    ctx = modbus_new_rtu(“/dev/ttyUSB 0”,96 00,'N',8,1);
    //スレーブアドレスを設定
    mod_set_slave ctx 1;
    //シリアルモードの設定
    //mod_r_set_serial_mode ctx MODBUS_RTU_485;
    //RTSの設定
    //mod_r_set_rts ctx MODBUS___UP;
    //コネクションの確立
    if modbus_connect ctx == -1 {
        ff stderr "Connexion ed %s\n" mod_strerror no;
        mod_free ctx;
        リターン-1;
    }
    mb_m = mod_m_new MODBUS_MAX__0
                                    MODBUS_MAX__ISTERS 0;
    if mb_m == NULL {
        fprintf stderr "Failed to allocate the mapping %s\n"
                mod_strerror no;
        mod_free ctx;
        リターン-1;
    }

   複数のレジスタを初期化する。
   mb_m-tab_registers 0] = 1;
   mb_m-tab_registers 1] = 2;
   mb_m-tab_registers 2] = 3;
   mb_m-tab_registers 3] = 4;
   mb_m-tab_registers 4] = 5;
   mb_m-tab_registers 5] = 6;

    while 1
    {
        memset query 0 sizeof query;
        rc = modbus_receive ctx query;
        if rc 0 {
            mod_rectx rc mb_m;
        } else if rc == -1 {
            //クライアントまたはエラーによって接続が閉じられた
            休憩を取る。
        }
    }

    f

    mod_m_free mb_m;
    if s != -1) {
        close(s);
    }
    modbus_close(ctx);
    modbus_free(ctx);
}

RTU-Slaveプロセスのまとめ

  1. modbusポインタの初期化;
  2. ステーションIDの設定;
  3. modbus_mapping_new初期化レジスタはmodbus_mapping_tポインタを返します。
  4. 接続の確立;
  5. modbus_receive関数を呼び出して、シリアルポートの受信データを判断し、受信処理と応答を担当します。

TCP master

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <error.h>
#include <memory.h>
#include <sys/socket.h>
#include "modbus.h"

void main(void)
{
    int i;
    int regs = 0;
    int num = 0;
    modbus_t *ctx = NULL;
    uint16_t tab_reg[64] = {0};
   
    ctx = modbus_new_tcp("127.0.0.1",8000);
    modbus_set_slave(ctx, 1);
    modbus_connect(ctx);
    modbus_set_response_timeout(ctx, 0, 1000000);
   
    while (1)
    {
        memset(tab_reg, 0, sizeof(tab_reg));
        regs = modbus_read_registers(ctx, 0, 20, tab_reg);
        printf("[%4d][read num = %d]", num++, regs);
        for (i = 0; i < 20; i++)
        {
            printf("<%#x>", tab_reg[i]);
        }
        printf("\n");
        sleep(1);
    }

    modbus_close(ctx);
    modbus_free(ctx);
}

TCP slave

#include stdio.h>
#include unistd.h>
#include stdlib.h>
#include error.h>
#include memory.h>
#include sys/socket.h>
#include "modbus.h"

int main void
{ 
    int i = 0;
    rc = 0;
    mod_t *ctx = NULL;
    mod_m_t *mb_m = NULL;
    uint8_t query MODBUS_TCP_MAX_ADU_LENGTH];

    //ポートを开くip、port
    ctx = mod_new_t "127.0.0.1"8000;
    //スレーブアドレスを設定
    mod_set_slave ctx 1;
    //コネクションの確立
    if modbus_connect ctx == -1 {
        ff stderr "Connexion ed %s\n" mod_strerror no;
        mod_free ctx;
        リターン-1;
    }
    mb_m = mod_m_new MODBUS_MAX__0
                                    MODBUS_MAX__ISTERS 0;
    if mb_m == NULL {
        fprintf stderr "Failed to allocate the mapping %s\n"
                mod_strerror no;
        mod_free ctx;
        リターン-1;
    }

   複数のレジスタを初期化する。
   mb_m-tab_registers 0] = 1;
   mb_m-tab_registers 1] = 2;
   mb_m-tab_registers 2] = 3;
   mb_m-tab_registers 3] = 4;
   mb_m-tab_registers 4] = 5;
   mb_m-tab_registers 5] = 6;

    while 1
    {
        memset query 0 sizeof query;
        rc = modbus_receive ctx query;
        if rc 0 {
            mod_rectx rc mb_m;
        } else if rc == -1 {
            //クライアントまたはエラーによって接続が閉じられた
            休憩を取る。
        }
    }

    f

    mod_m_free mb_m;
    if s != -1){
        close s;
    }
    mod_clctx;
    mod_free ctx;
}

この資料を実際のプロジェクトに活用しませんか?

ツールセンターで電文解析、CRCチェック、デバイスデバッグを行うか、要件を送信して選定と接続のアドバイスを受けましょう。

エンジニア会員

この記事を実行可能なデバッグ資料に

登録すると、高度な電文解析、資料パックのダウンロード、コード例、エンジニアリングケース、優先技術サポートが利用可能に。実際のプロジェクト納品に最適です。

高度なツール無制限
資料パックとコードパック
完全なエンジニアリングケースライブラリ
優先技術サポート窓口

《 “libmodbus -Linux、Mac OS、FreeBSD、Windows用のModbusライブラリ” 》 有 6 条コメント

发表回复

あなたのメールアドレスアドレス不会被公开。 必須項目已用 * 标注