Giter Club home page Giter Club logo

modbus's Introduction

Simple MODBUS implementation for STM32 HAL & FreeRTOS

Why I write my own implementation?

  • It does not need any timers.
  • It need only one thread on normal (or the same with interface) priority.
  • It can be easy adopted for USB/usart/whatever interfaces.
  • It is for fun of course!

How to USE

  • Grab modbus.c and modbus.h to your project
  • Fix values in modbus.h for you.
  • Call init functions and set slave address
ModBus_Init();
ModBus_SetAddress(1);
  • Handle incoming bytes to ModBusInHandle queue (example from USB-CDC)
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
...
for(int i=0;i<(*Len);i++)
  {
  osMessagePut(ModBusInHandle,Buf[i],0);
  }
...
return (USBD_OK);
  • Handle outgoing bytes the same way, but from ModBusOutHandle queue
uint8_t buf[256]; // buffer, where we collect output data
uint8_t c = 0; // counter for buffer fill
  
for(;;)
{
osEvent evt = osMessageGet(ModBusOutHandle,200); // wait here 200 tick
if (evt.status == osEventMessage)
  {
    buf[c++]=(uint8_t) evt.value.v;
  }
if (evt.status == osEventTimeout)
  {
    if( (c > 0) && (c < 254) ) // ok, something in buffer exist, lets send it
    {
      CDC_Transmit_FS(&buf[0], c); // by USB-CDC         
    }  
  c=0;
  }
  • In other part code, call ModBus_GetRegister & ModBus_SetRegister for reading & setting register
count = ModBus_GetRegister(0);
ModBus_SetRegister(0,count+1); 

How to check?

I prefer to use ModPoll (http://www.modbusdriver.com/modpoll.html)

So in my case all look like this. Here register 0 & 1 overwritten inside programm for test, and modbus slave on COM5

C:\Users\multi\Downloads\modpoll.3.4\win32>modpoll.exe -a 1 -r 1 -c 5  COM5 100 200 310 400 500
modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2013 proconX Pty Ltd
Visit http://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: Modbus RTU
Slave configuration...: address = 1, start reference = 1, count = 5
Communication.........: COM5, 19200, 8, 1, even, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, output (holding) register table

Written 5 references.

C:\Users\multi\Downloads\modpoll.3.4\win32>modpoll.exe -a 1 -r 1 -c 6 COM5
modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2013 proconX Pty Ltd
Visit http://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: Modbus RTU
Slave configuration...: address = 1, start reference = 1, count = 6
Communication.........: COM5, 19200, 8, 1, even, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, output (holding) register table

-- Polling slave... (Ctrl-C to stop)
[1]: 118
[2]: 0
[3]: 310
[4]: 400
[5]: 500
[6]: 0
-- Polling slave... (Ctrl-C to stop)
[1]: 140
[2]: 0
[3]: 310
[4]: 400
[5]: 500
[6]: 0
-- Polling slave... (Ctrl-C to stop)

You are too lazy?

In modbustest directory you find full working example.

I use:

  • STM32F3-Discovery board
  • Keil uVision 5.22
  • STM32CubeMX

#Have fun!#

modbus's People

Contributors

kiltum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

modbus's Issues

Reference different from 1 does not work

I am using your code for a USART based project. It was very simple to implement, because of the well-documented code. However, I am facing this problem:

This works fine:
modpoll.exe -a 1 -r 1 -c 6 COM5

But any -r different from 1 fails:

modpoll.exe -a 1 -r 2 -c 6 COM5
modpoll.exe -a 1 -r 3 -c 6 COM5

Data seems to arrive well in STM32F4, also mb_buf_out seems ok, I don't really know where to go from here?

Thanks

Code licence

Could you please choose a license for this project so people can know if they can modify it and use it in their own work?

implementing modbus on UART

Hi.
Your code is well described and good. I want to use it on STM32F103VET6 discovery board, and use UART1 port for modbus (not USB). I don't know how to do that and what codes I should use instead of those you used for USB.
Thank you very much...

Code Error in modbus.c

osThreadDef(ModBusTask, ModBusTask, osPriorityNormal, 0, 128);

throws a too many argumentes Error

i fixed it to :
osThreadDef(ModBusTask, osPriorityNormal, 0, 128);

was the function updated by freeRTOS?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.