Coder Social home page Coder Social logo

modbus_lib's Introduction

Description

Easy to use/port Modbus RTU slave library, written microcontrollers in mind.

Usage

  1. Include the header and source file paths within your toolchain (Makefile or your IDE):
C_SOURCES =  \
    ... \
    modbus_lib/modbus_lib.c \
    modbus_lib/modbus_crc.c

C_INCLUDES =  \
    ... \
    -Imodbus_lib
  1. Include the header file within your main.c:
#include "modbus_lib.h"
  1. Implement the following functions in your application:
  • Read handler:
uint16_t modbus_lib_read_handler(uint16_t la){ // la: logical_address
    switch(la){
        case 40001:
            return something; 
        case 40002:
            return something_else;
        case 40003: 
            if (some_error){
                return modbus_lib_send_error(MBUS_RESPONSE_ILLEGAL_DATA_VALUE);
            } else {
                return some_other_value;
            }
        default:
            return modbus_lib_send_error(MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS); 
    }
}
  • Write handler:
uint16_t modbus_lib_write_handler(uint16_t la, uint16_t value){
{
    if ( la > 40000 && la <= 40013 ){
        my_buffer_reg_4xxxx[la-40001] = value;
    }
    if (la == 40018 ){
         output = (value >> 8) | (value << 8);
    }
        
    if (some_error){
        return modbus_lib_send_error(MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS); 
    }
    return 0; // data is successfully written
}
  • Place the data receive function inside your incoming stream:
void USART2_dataHandler(void)
{
  uint8_t buff[1]; 
  HAL_UART_Receive (&huart2, buff, 1, 400);  
  modbus_lib_append_data(buff[0]); // append byte-by-byte
}
  • Trigger the end of telegram after a timeout:
void USART2_idleHandler(void)
{
    modbus_lib_end_of_telegram();
}
  • Implement write function:
int modbus_lib_transport_write(uint8_t* buffer, uint16_t length){
    HAL_UART_Transmit(&huart2, buffer, length, 1000);
    return 0; 
}
  1. Make Modbus slave configuration and initialize in your main() function:
ModbusConfig_t modbus_cfg = {
    .address = 1
}; 

modbus_lib_init(&modbus_cfg);
  1. Debug:
  • Verify that uint8_t g_modbus_lib_received_telegram[] is filled with received telegram.
  • Verify that uint8_t g_modbus_lib_received_length has correct value.
  • Examine uint8_t outgoing_telegram[] contents for outgoing telegram (use uint16_t oindex for length) inside modbus_lib.c.

Example

STM32F407 Discovery board example is available here.

modbus_lib's People

Contributors

ceremcem avatar

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.