简体   繁体   English

如何使用 C/C++ 映射 Modbus 寄存器地址

[英]How to Map Modbus Register address using C / C++

I am trying to implement a ModBus RTU Protocol over a UART Peripheral using STM32F4-Discovery Board.我正在尝试使用 STM32F4-Discovery Board 在 UART 外设上实现 ModBus RTU 协议。

I am planning to implement Master and Slave functionality using Modbus RTU.我计划使用 Modbus RTU 实现主站和从站功能。

Here are my Questions这是我的问题

How could i move my received data in UART to MODBUS and Transmit data from MODBUS layer to UART, using C/C++ , Please provide some code snippet that can help me understand your explanation.我如何使用 C/C++ 将我在 UART 中接收到的数据移动到 MODBUS 并将数据从 MODBUS 层传输到 UART,请提供一些可以帮助我理解您的解释的代码片段。 (As of now i am thinking of coming up with some API's please suggest some patterns) (截至目前我正在考虑提出一些 API,请提出一些模式)

secondly how could i implement Modbus Registers and coils.其次,我如何实现 Modbus 寄存器和线圈。 I meant how could i define there address as specified in the ModBus Application Manual.我的意思是如何定义 ModBus 应用手册中指定的地址。 Please explain me with a code snippet.请用代码片段向我解释。 Below something i tried to implement.下面是我试图实现的东西。

/*Global Array, Structure and Variable Declarations*/
/* So to access Discrete_Output_Coils[2345] 
   the valid address to include in the protocol would be
   so we have to access address 0x929 to read the 
   data.*/
const unsigned short int Discrete_Output_Coils[4999];
const unsigned short int Discrete_Input_Contacts[4999];
const unsigned short int Analog_Input_Register[4999];
const unsigned short int Anlog_output_Holding_Register[4999];

I agree with user694733.我同意用户 694733。 You should go step by step.你应该一步一步来。 For example, you should define your hardware specifications (ie, what signal and of which type are they? How many? The answer to these questions will tell you how many registers and of which type will be meaningful to your "device". After that, map each signal to a register and then try to do a single register read/write operation with a CRC checking incorporated (programmed) for coils or analog. Adjust the protocol until it get what you expected, and them cover the rest of a single register type (coil, analog). Then, expand the implementation to multiple registers read/write operations. By the way, putting array declarations do not show an effort in solving the problem. You ask the community to do all the hard work. If you want some more code to start with google a little bit more. Here is a link to a library that you can study to see if it match your request: http://libmodbus.org/download/例如,您应该定义您的硬件规格(即,它们是什么信号和类型?有多少?这些问题的答案将告诉您有多少寄存器和哪种类型对您的“设备”有意义。之后, 将每个信号映射到一个寄存器,然后尝试执行单个寄存器读/写操作,其中包含针对线圈或模拟的 CRC 检查(已编程)。调整协议直到达到您的预期,并且它们涵盖了单个寄存器的其余部分寄存器类型(线圈,模拟)。然后,将实现扩展到多个寄存器读/写操作。顺便说一句,放置数组声明并不能显示解决问题的努力。你要求社区做所有的努力。如果你想要更多的代码从谷歌开始。这是一个图书馆的链接,你可以研究它是否符合你的要求: http : //libmodbus.org/download/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM