libmodbus
功能: a fast and portable Modbus library

库下载地址
https://libmodbus.org

使用指南
1)包含头文件
#include <modbus.h>
2) 编译
cc `pkg-config --cflags --libs libmodbus` files

示例:
#include <stdio.h>
#include <modbus.h>

int main(void) {
  modbus_t *mb;
  uint16_t tab_reg[32];

mb = modbus_new_tcp("127.0.0.1", 1502);
  modbus_connect(mb);

/* Read 5 registers from the address 0 */
  modbus_read_registers(mb, 0, 5, tab_reg);

modbus_close(mb);
  modbus_free(mb);
}

描述:
libmodbus是一个遵循Modbus协议的库,可以使一个设备用来发送接收数据。可以支持多种网络通信:串口通信RTU或者网口通信TCP/IPv6等。

相关概念
Contexts
The modbus_t context is an opaque structure containing all necessary information to establish a connection with other Modbus devices according to the selected variant.
常用的contexts有如下几种:
1)RTU Context:
A Modbus RTU message must be transmitted continuously without inter-character hesitations .The Modbus RTU framing calls a slave, a device/service
which handle Modbus requests, and a master, a client which send requests. The communication is always initiated by the master.
2)TCP(IPv4) Context:
The TCP backend implements a Modbus variant used for communications over TCP/IPv4 networks. It does not require a checksum calculation as lower                 layer takes care of the same.
3)TCP PI (IPv4 and IPv6) Context:
The TCP PI (Protocol Independent) backend implements a Modbus variant used for communications over TCP IPv4 and IPv6 networks. It does not require a checksum calculation as lower layer takes care of the same.Contrary to the TCP IPv4 only backend, the TCP PI backend offers hostname resolution but it consumes about
1Kb of additional memory.

常用API
1)释放context
modbus_free();
2)设置slave ID
modbus_set_slave();
3)启动调试模式
modbus_set_debut();
4)超时设置
modbus_get_byte_timeout();
modbus_set_byte_timeout();
modbus_get_response_timeout();
modbus_set_response_timeout();
modbus_get_indication_timeout();
modbus_set_indication_timeout();
5)错误恢复模式
modbus_set_error_recovery();
6)设置/获取内部socket
modbus_set_socket();
modbus_get_socket();
7)头部信息
modbus_get_header_length();
8)数据操作宏
MODBUS_GET_HIGH_BYTE(data)  // 获取数据的高字节
MODBUS_GET_LOW_BYTE(data)  // 获取数据的低字节
MODBUS_GET_INT64_FROM_INT16(tab_int16, index) // builds an int64 from the four first int16 starting at tab_int16[index]
MODBUS_GET_INT32_FROM_INT16(tab_int16, index) // builds an int32 from the two first int16 starting at tab_int16[index]
MODBUS_GET_INT16_FROM_INT8(tab_int8, index)   // builds an int16 from the two first int8 starting at tab_int8[index]
MODBUS_SET_INT16_TO_INT8(tab_int8, index, value)   // set an int16 value into the two first bytes starting at tab_int8[index]
MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) // set an int32 value into the two first int16 starting at tab_int16[index]
MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) // set an int64 value into the four first int16 starting at tab_int16[index]
9)处理bits和bytes
modbus_set_bits_from_byte();
modbus_set_bits_from_bytes();
modbus_get_byte_from_bits();
10)设置或获取浮点数
modbus_get_float_abcd();
modbus_set_float_abcd();
modbus_get_float_badc();
modbus_set_float_badc();
modbus_get_float_cdab();
modbus_set_float_cdab();
modbus_get_float_dcba();
modbus_set_float_dcba();

11) Connection连接
建立连接
mobus_connect();
关闭连接
modbu_close();
刷新
modbus_flush();

12) Client客户端
The Modbus protocol defines different data types and functions to read and write them from/to remote devices. The following functions are used by the clients to send Modbus requests:
读数据
modbus_read_bits();
modbus_read_input_bits();
modbus_read_registers();
modbus_read_input_registers();
modbus_report_slave_id();
写数据
modbus_write_bit();
modbus_write_registers();
modbus_write_bits();
modbus_write_registers();
读写数据
modbus_write_and_read_registers();
Raw请求
modbus_send_raw_request();
modbus_receive_confirmation();
响应异常
modbus_reply_exception();

13)Sever
The server is waiting for request from clients and must answer when it is concerned by the request.
accept/listen
modbus_tcp_listen();
modbus_tcp_accept();
modbus_tcp_pi_listen();
modbus_tcp_pi_accept();
接收
modbus_receive();
发送响应
modbus_reply();
modbus_reply_exception();

14)错误处理
modbus_strerror();

15)RTU context
创建一个RTU context
modbus_new_rtu();
设置串口模式
modbus_rtu_get_serial_mode();
modbus_rtu_set_serial_mode();
modbus_rtu_get_rts();
modbus_rtu_set_rts();
modbus_rtu_set_custom_rts();
modbus_rtu_get_rts_delay();
modbus_rtu_set_rts_delay();

16) TCP(IPv4) Context
创建
modbus_new_tcp();

17) TCP PI(IPv4 and IPv6)
创建
modbus_new_tcp_pi();

libmodbus学习笔记的更多相关文章

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  3. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  4. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  5. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  6. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  7. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  8. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

随机推荐

  1. mysql-mysqldump

    备份(导出)所有数据库的数据和结构(注意:这种方式备份,还原时,无需先创建数据库,可直接导入) mysqldump -u root -p 'password' --all-databases > ...

  2. git 学习笔记 —— 获取远端分支并修改后提交至远端仓库

    笔者最近进行开发过程中,所有参与者的代码需要通过 git 上传到远端仓库中,不同的模块对应不同的 git 分支,不同模块的数据需要从远端仓库中获取.这里记录下笔者从远端仓库中获取分支数据,进行修改,最 ...

  3. 记录一次群答问:jmeter正则提取器轻松提取一个及多个值

    图截得比较完整,电脑端浏览器放大倍数看吧^_^,手机端可以点击图片然后放大看. 一个正则提取问题 前几天,在Q群和微信群里被同时@,咨询这样一个问题:服务器返回:name="tom" ...

  4. (尚029)Vue_案例_交互footer组件功能

    需要实现界面截图: 难点分析:sAllCheck必须定义为计算属性 1.想到问题: 一旦写一个组件,需要接收哪些属性?? 因为只有属性确定了,标签才好写 todos属性可以确定三个方面的显示 2.做交 ...

  5. Reactive Extensions (Rx) 入门(3) —— Rx的事件编程

    译文:https://blog.csdn.net/fangxing80/article/details/7628322 原文:http://www.atmarkit.co.jp/fdotnet/int ...

  6. 洛谷 P1097 【统计数字】 题解

    题目背景 警告:数据可能存在加强 题目描述 某次科研调查时得到了nn个自然数,每个数均不超过1500000000(^)( ).已知不相同的数不超过1000010000个,现在需要统计这些自然数各自出现 ...

  7. C语言中常见的图形打印总结

    直角三角形(靠右直立) 示例实现代码如下: int main(){ int n; int i,j; cin >> n; if(n<= 0){ cout << " ...

  8. NodeJS代码组织与部署

    使用NodeJS编写程序前,为了有个良好的开端,首先需要准备好代码的目录结构和部署方式,就如同修房子要先搭脚手架.本章将介绍与之相关的各种知识. 一.模块路径解析规则 我们已经知道,require函数 ...

  9. OpenVSwitch

    参考: https://opengers.github.io/openstack/openstack-base-use-openvswitch/ 这篇原理部分就不贴出来了,请自行参考上文,并根据自行实 ...

  10. linux中的目录

    Linux文件系统数如下: 在 Linux 系统中,文件系统通过目录"包含"子目录及文件的方式,来组织成一个树状结构.那么目录到底是如何"包含"其他目录及文件的 ...