API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
modbus_master_rtu.c

This is a basic example of a ModBUS Master RTU

/* @file modbus_master_rtu.c
* @author TST
* @version 0.1
* @date 03/07/2012
* @brief This is an example about how to manage the stack Modbus in master mode.
*/
/* Platform headers */
#include "tsmart.h"
/* Modbus headers */
#include "modbus/port/mbport.h"
#include "modbus/mbmaster/include/mbm.h"
#include "modbus/common/include/mbframe.h"
#include "modbus/common/include/mbportlayer.h"
#include "modbus/common/include/mbutils.h"
/* ----------------------- Static variables ---------------------------------*/
static xMBMHandle MBMHandle;
/* @brief ModBUS RTU handler task
*
* This task initializes the ModBUS serial stack and polls the status of coils
* and registers in remote node 0x01.
*
* @param pvParameters Not used
*/
static void MBRTUTask(void *pvParameters) {
static uint8_t i, data[32];
static USHORT data2[32];
eMBErrorCode eStatus;
/* Main task loop */
for (;;) {
for (i=0 ; i<3 ; i++) {
eStatus = eMBMWriteSingleCoil(MBMHandle, 0x01, i, TRUE);
if (eStatus != MB_ENOERR) {
for (;;);
}
eStatus = eMBMReadCoils(MBMHandle, 0x01, i, 1, data);
if (eStatus != MB_ENOERR) {
for (;;);
}
vTaskDelay(1000/portTICK_RATE_MS);
eStatus = eMBMWriteSingleCoil(MBMHandle, 0x01, i, FALSE);
if (eStatus != MB_ENOERR) {
for (;;);
}
eStatus = eMBMReadCoils(MBMHandle, 0x01, i, 1, data);
if (eStatus != MB_ENOERR) {
for (;;);
}
vTaskDelay(1000/portTICK_RATE_MS);
eStatus = eMBMReadInputRegisters( MBMHandle, 0x01, 0, 1, data2);
if (eStatus != MB_ENOERR) {
for (;;);
}
vTaskDelay(1000/portTICK_RATE_MS);
eStatus = eMBMReadInputRegisters( MBMHandle, 0x01, 1, 1, data2);
if (eStatus != MB_ENOERR) {
for (;;);
}
vTaskDelay(1000/portTICK_RATE_MS);
eStatus = eMBMReadInputRegisters( MBMHandle, 0x01, 2, 15, data2); /* Read 15 --> 15 uint16 = 30 uint8 or 30 char */
if (eStatus != MB_ENOERR) {
for (;;);
}
vTaskDelay(1000/portTICK_RATE_MS);
}
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need these:
*
* - MBRTUTask task.
* - Initialize Modbus stack
* - Initialize communication interface: RS-485
*
*/
/* Initialize ModBus porting */
if (vMBPInit() != 0) {
return TSMART_FAIL;
}
/* Initialize the Master RTU */
if (eMBMSerialInit(&MBMHandle, MB_RTU, 0, 9600, MB_PAR_NONE) != MB_ENOERR) {
return TSMART_FAIL;
}
/* Create the ModBus TCP server task */
if (xTaskCreate(MBRTUTask, "MODBUS", 512, NULL, 2, NULL) != pdPASS) {
return TSMART_FAIL;
}
return TSMART_PASS;
}