概要:概要
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:ポート、ボーレート、チェック·ビット、データ·ビット、Stopビット
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_;
//読み取りレジスタ設定:レジスタアドレス、Quantity、データバッファ
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プロセスの概要:
- modbusポインタの初期化
- ステーションIDの設定
- 接続の確立
- 読み出しホールドレジスタ/入力レジスタ/入力/コイル入力
- 単一レジスタ/複数レジスタ/複数ビットデータの書き込み
- 接続を閉じる
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:ポート、ボーレート、チェック·ビット、データ·ビット、Stopビット
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プロセスのまとめ
- modbusポインタの初期化;
- ステーションIDの設定;
- modbus_mapping_new初期化レジスタはmodbus_mapping_tポインタを返します。
- 接続の確立;
- 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;
}
第一次接觸,善用資源學習
不知道怎么用的,来学习一下。
试试看,能否正常使用。
值得学习
值得学习参考.
值得学习参考