自写,支持主机,从机,下载地址:https://github.com/lotoohe-space/XTinyModbus 加入qq群讨论:419833232

也可到私人博客下载:http://www.iotscitech.cn/post/12

XTinyModbus
===

A small embedded Modbus protocol stack, written in C language, support master and slave, support discrete mapping, support non-blocking and blocking read and write mode, and provide a complete example, can be transplanted to different processors, simple to use, portable.<br>

0.Log
----

2020/4/13
Slave adds the send cache, which is optional and enabled by default through the macro configuration in md_rtu_fun. h.<br>

2020/4/12
Added transceiver conversion control.<br>

2020/4/10
Code to add comments, specification of comments, optimize the structure of the file storage.<br>

2020/4/4
Slave fixes a bug with a minimal probability that the timer overflow can cause packet loss.<br>

2020/4/3
Salve changes the discrete mapping, and the input is mapped separately <br>

2020/3/31
Salve adds host write callback function listen.<br>
Salve adds partial exception and error code support.<br>
Salve fixed an error sending unspaced frames.<br>
Optimize CRC16 efficiency.<br>

1.Features
----

ModBus RTU salve feature: <br>
1) realize Modbus RTU salve<br>
2) join the double-queue mechanism.<br>
3) implement function code 1,2,3,4,5,6,15,16.<br>
4) support discrete mapping between modbus address and local address.<br>
5) provide user to read and write modbus address interface.<br>
6) support multiple slaves.<br>

ModBus RTU master feature: <br>
1) realize Modbus RTU master<br>
2) support non-blocking type read and write <br>
3) support blocking type read and write <br>
4) support regular retransmission <br>
5) support timeout retransmission <br>
6) support read data discrete mapping <br>

2.How to transplant
----
The example gives an example of a port in the STM32F1 series of chips that need to be ported to other chips, need to modify md_rtu_serial.c, implement port port port function migration, and need to call serial port and timer functions in the interrupt function. < br >
Serial port transplantation function:
```c
/*Send function*/
void MDSSerialSendBytes(uint8 *bytes,uint16 num){
    /*The BSP sending function is called below*/
    uart_send_bytes(bytes,num);
}
```
Call the following function in the timer interrupt function, the timer interval is 100US:
```c
/*This function is called in the timer*/
void MDSTimeHandler100US(uint32 times){
    if(_pModbus_RTU==NULL){return;}
    _pModbus_RTU->mdRTUTimeHandlerFunction(_pModbus_RTU ,times);
}
```
The following function is called in the serial port receive interrupt
```c
/*The BSP layer interrupts the receiving call to this function*/
void MDSSerialRecvByte(uint8 byte){
    if(_pModbus_RTU==NULL){return;}
    _pModbus_RTU->mdsRTURecByteFunction(_pModbus_RTU , byte);
}
```
3.How to use
----
You can see an example of using the md_rtu_app.c file.
The following code adds two address mapping items, and the modbus address corresponds to the local address:
```c
uint16 regCoilData0[32]={1,2,3,4,5,6,7,8,9,10,11,12};
RegCoilItem regCoilItem0={
. ModbusAddr =0x0000, /* address in MODBUS */
. ModbusData =regCoilData0, /* mapped memory unit */
. ModbusDataSize =32, /* the size of the map */
.addrtype =REG_TYPE /* the type of the map */
};
uint16 regCoilData1[4]={0};
RegCoilItem regCoilItem1={
. ModbusAddr =0x0000, /* address in MODBUS */
. ModbusData =regCoilData1, /* mapped memory unit */
. ModbusDataSize =64, /* the size of the map */
.addrtype =BIT_TYPE /* type of mapping */
};
    /*Add an address map*/
    if(RegCoilListAdd(&modbusS_RTU, &regCoilItem0)==FALSE){
        return FALSE;
    }
    if(RegCoilListAdd(&modbusS_RTU, &regCoilItem1)==FALSE){
        return FALSE;
    }
```
The following code can be used to read and write modbus registers and coil values:
```c
Uint16 temp = 0 XFF;
Uint16 temp1 [] = {1, 2, 3, 4, 5};
MDS_RTU_WriteBits (& modbusS_RTU, 1, 5, & temp);
MDS_RTU_WriteReg (& modbusS_RTU, 11, temp);
MDS_RTU_WriteRegs (& modbusS_RTU, 5, 5, temp1, 0).
```
4.Future features
----

Modbus RTU host, Modbus ASCII host and slave.

XTinyModbus
===

一个应用于嵌入式方面的小型Modbus协议栈,采用C语言编写,支持master与slave,支持离散映射,支持非阻塞与阻塞读写模式,并提供完整示例,可以移植到不同的处理器,使用简单,移植方便。<br>

0.日志
----

2020/4/13
Slave增加发送缓存,该项为可选项,可以通过MD_RTU_Fun.h内的宏配置,默认开启。<br>

2020/4/12
增加收发转换控制。<br>

2020/4/10
代码添加注释,规范注释方式,优化文件存放结构。<br>

2020/4/4
Slave修复定时器溢出可能导致丢包的极小概率bug。<br>

2020/4/3
Slave更改离散映射方式,输入分开映射<br>

2020/3/31
Slave添加主机写回调函数监听。<br>
Slave添加部分异常码与错误码支持。<br>
Slave修复发送未进行帧间隔的错误。<br>
优化CRC16效率。<br>

1.特性
----
ModBus RTU Slave 特性:<br>
1)实现Modbus RTU Slave<br>
2)加入双队列机制。<br>
3)实现功能码1,2,3,4,5,6,15,16。<br>
4)支持modbus地址与本机地址离散映射。<br>
5)提供用户读写modbus地址接口。<br>
6)支持多个从机。<br>

ModBus RTU master 特性:<br>
1)实现Modbus RTU master<br>
2)支持非阻塞式读写<br>
3)支持阻塞式读写<br>
4)支持定时重发<br>
5)支持超时重传<br>
6)支持读数据离散映射<br>

2.如何移植
----
示例中给出了在STM32F1系列芯片的一个移植示例,去过需要移植到其它芯片,需要对MD_RTU_Serial.c进行修改,实现串口函数的移植,以及需要在中断函数中调用串口与定时器的 函数。<br>
串口移植函数:
```c
/*发送函数*/
void MDSSerialSendBytes(uint8 *bytes,uint16 num){
    /*在下面调用bsp的发送函数*/
    uart_send_bytes(bytes,num);
}
```
在定时器中断函数中调用下面这个函数,定时器间隔100US:
```c
/*定时器中调用该函数*/
void MDSTimeHandler100US(uint32 times){
    if(_pModbus_RTU==NULL){return;}
    _pModbus_RTU->mdRTUTimeHandlerFunction(_pModbus_RTU ,times);
}
```
在串口接收中断中调用下面这个函数
```c
/*bsp层中断接收调用这个函数*/
void MDSSerialRecvByte(uint8 byte){
    if(_pModbus_RTU==NULL){return;}
    _pModbus_RTU->mdsRTURecByteFunction(_pModbus_RTU , byte);
}
```
3.如何使用
----
使用方式可以查看MD_RTU_APP.c文件的使用示例。
下面代码添加两个地址映射项,吧modbus地址与本地地址对应起来:
```c
uint16 regCoilData0[32]={1,2,3,4,5,6,7,8,9,10,11,12};
RegCoilItem regCoilItem0={
    .modbusAddr=0x0000,                /*MODBUS中的地址*/
    .modbusData=regCoilData0,    /*映射的内存单元*/
    .modbusDataSize=32,                /*映射的大小*/
    .addrType=REG_TYPE                /*映射的类型*/
};
uint16 regCoilData1[4]={0};
RegCoilItem regCoilItem1={
    .modbusAddr=0x0000,                /*MODBUS中的地址*/
    .modbusData=regCoilData1,    /*映射的内存单元*/
    .modbusDataSize=64,                /*映射的大小*/
    .addrType=BIT_TYPE                /*映射的类型*/
};
    /*添加一个地址映射*/
    if(RegCoilListAdd(&modbusS_RTU, &regCoilItem0)==FALSE){
        return FALSE;
    }
    if(RegCoilListAdd(&modbusS_RTU, &regCoilItem1)==FALSE){
        return FALSE;
    }
```
下面的代码可以实现用户端读写modbus 寄存器与线圈值:
```c
    uint16 temp=0xff;
    uint16 temp1[]={1,2,3,4,5};
    MDS_RTU_WriteBits(&modbusS_RTU,1,5,&temp);
    MDS_RTU_WriteReg(&modbusS_RTU,11, temp);
    MDS_RTU_WriteRegs(&modbusS_RTU,5,5, temp1,0);
```
4.未来功能
Modbus ASCII主机与从机。

5.谢鸣
紫§尘、

开源Modbus 协议库---XTinyModbus相关推荐

  1. 开源http协议库curl和wget的区别和使用

    curl和wget基础功能有诸多重叠,如下载等. 在高级用途上的curl由于可自定义各种请求参数所以长于模拟web请求,用于测试网页交互(浏览器):wget由于支持ftp和Recursive所以长于下 ...

  2. 【流媒體】jrtplib—VS2010下RTP开源协议库JRTPLIB3.9.1编译

    一.JRTPLIB简介 老外用C++编写的开源RTP协议库,用来进行实时数据传输,可以运行在 Windows.Linux. FreeBSD.Solaris.Unix和VxWorks 等多种操作系统上, ...

  3. 【流媒體】jrtplib—VS2010 下RTP开源协议库JRTPLIB3.9.1编译

    [流媒體]jrtplib-VS2010下RTP开源协议库JRTPLIB3.9.1编译 SkySeraph Apr 7th 2012 Email:skyseraph00@163.com 一.JRTPLI ...

  4. 今晚19:00,淘宝自研标准化协议库XQUIC开源直播!

    今晚19:00-20:00,淘系技术视频号. XQUIC项目负责人喵吉与你详聊开源背后的故事. 开源地址:https://github.com/alibaba/xquic XQUIC介绍 XQUIC  ...

  5. 配置开源安卓QQ协议库Mirai

    博客和更新地址:配置开源安卓QQ协议库Mirai 前言 因为经常配置Mirai,每次手动输入命令有些麻烦,所以记录一下配置过程. 配置JDK Debian系: apt install openjdk- ...

  6. Modbus协议概念最详细介绍

    ★ 一文认识Modbus协议: " 1 什么是Modbus? 2 主从模式 3 协议的分类 3.1 Modbus ASCII 3.2 Modbus RTU 3.3 Modbus TCP 3. ...

  7. C#实现Modbus协议与PLC通信

    项目需要用C#写一个上位机,用Modbus/TCP协议与PLC通信,控制伺服电机的启停.转速等.D:\Code\C#\ConsoleApp1 1. 获取PLC的IP地址 待续... 2. " ...

  8. 嵌入式相关开源项目、库、资料------持续更新中

    学习初期最难找的就是找学习资料了,本贴精心汇总了一些嵌入式相关资源,包括但不限于编程语言.单片机.开源项目.物联网.操作系统.Linux.计算机等资源,并且在不断地更新中,致力于打造全网最全的嵌入式资 ...

  9. wpf项目源代码_C# WPF开源控件库:MahApps.Metro

    C# WPF开源控件库:MahApps.Metro ❝ 其实站长很久之前就知道这个开源WPF控件库了,只是一直欣赏不了这种风格,但也star了该项目.每次浏览该仓库时,发现star越来越多,也看到很多 ...

最新文章

  1. 分布式一致性协议Raft原理与实例
  2. 高级指引——概念解释——图形 Shape 及其属性
  3. 过程(栈帧结构是干货)
  4. HBase shell 命令。
  5. php前台用户权限开通,vue实现网站前台的权限管理
  6. 默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
  7. 发现很多人的基础都不好
  8. C++编译报错:重复定义
  9. [Unity][FlowCanvas][NodeCanvas] ForEach 不适合连接 Wait,FSM 的 SubFlowScript 接受不到事件
  10. 【Spring】Spring 关于 Redis 的序列化器
  11. oppo手机android是多,OPPO手机卡屏?教你三招轻松解决卡顿问题,大部分安卓都适用...
  12. JSP中报错only a type can be imported XXX resolves to package
  13. 从web层运作流程认识Struts2
  14. 手把手教学暴力破解WIFI密码(仅供学习交流)
  15. 双系统Win10下装Ubuntu16.04
  16. 为什么大数据与云计算密不可分?
  17. 怎样能把在线视频(不提供下载)储存下来到电脑
  18. 防雷知识:什么是雷电浪涌
  19. 最棒的 7 个 Laravel admin 后台管理系统推荐 - 卡拉云
  20. 篮球图片html页面代码,教你用PS制作一个非常逼真的篮球图片

热门文章

  1. 数据分类分析--聚类
  2. Android自定义控件-仿淘宝ios客户端天猫商品详情界面
  3. 盘点Linux操作系统的十大版本
  4. 基于Verilog HDL与虚拟实验平台的【计算机组成】与CPU实验第三章:三态门和多路器
  5. blazeds与spring的结合使用
  6. 解决 libpng warning: iCCP: known incorrect sRGB profile
  7. C#中在鼠标经过Button控件时显示提示信息(弹出气泡提示框)
  8. normalize.css 类似,使用normalize.css遇到的问题?
  9. 【2022】超详细的JAVA JDK配置和IDEA安装教程(Windows 版)
  10. 安装SQL server出现“服务没有及时响应启动或控制请求”