Add master support to modbus. From Darcy Gong

This commit is contained in:
Gregory Nutt 2015-04-11 09:37:31 -06:00
parent 108a6bf660
commit 8475fcc11a
14 changed files with 621 additions and 65 deletions

View File

@ -32,25 +32,6 @@
#ifndef __APPS_INCLUDE_MODBUS_MB_H #ifndef __APPS_INCLUDE_MODBUS_MB_H
#define __APPS_INCLUDE_MODBUS_MB_H #define __APPS_INCLUDE_MODBUS_MB_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <termios.h>
#ifdef __cplusplus
PR_BEGIN_EXTERN_C
#endif
#include "mbport.h"
#include "mbproto.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* This module defines the interface for the application. It contains /* This module defines the interface for the application. It contains
* the basic functions and types required to use the Modbus protocol stack. * the basic functions and types required to use the Modbus protocol stack.
* A typical application will want to call eMBInit() first. If the device * A typical application will want to call eMBInit() first. If the device
@ -75,6 +56,26 @@ PR_BEGIN_EXTERN_C
* } * }
*/ */
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <termios.h>
#ifdef __cplusplus
PR_BEGIN_EXTERN_C
#endif
#include "mbport.h"
#include "mbproto.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Use the default Modbus TCP port (502) */ /* Use the default Modbus TCP port (502) */
#define MB_TCP_PORT_USE_DEFAULT 0 #define MB_TCP_PORT_USE_DEFAULT 0
@ -319,7 +320,7 @@ eMBErrorCode eMBRegisterCB(uint8_t ucFunctionCode,
* ILLEGAL DATA ADDRESS exception frame is sent as a response. * ILLEGAL DATA ADDRESS exception frame is sent as a response.
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
* currently not available and the application dependent response * currently not available and the application dependent response
* timeout would be violated. In this case a SLAVE DEVICE BUSY * timeout would be violated. In this case a SLAVE DEVICE BUSY
* exception is sent as a response. * exception is sent as a response.
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
* a SLAVE DEVICE FAILURE exception is sent as a response. * a SLAVE DEVICE FAILURE exception is sent as a response.
@ -354,7 +355,7 @@ eMBErrorCode eMBRegInputCB(uint8_t * pucRegBuffer, uint16_t usAddress,
* ILLEGAL DATA ADDRESS exception frame is sent as a response. * ILLEGAL DATA ADDRESS exception frame is sent as a response.
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
* currently not available and the application dependent response * currently not available and the application dependent response
* timeout would be violated. In this case a SLAVE DEVICE BUSY * timeout would be violated. In this case a SLAVE DEVICE BUSY
* exception is sent as a response. * exception is sent as a response.
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
* a SLAVE DEVICE FAILURE exception is sent as a response. * a SLAVE DEVICE FAILURE exception is sent as a response.
@ -390,7 +391,7 @@ eMBErrorCode eMBRegHoldingCB(uint8_t * pucRegBuffer, uint16_t usAddress,
* ILLEGAL DATA ADDRESS is sent as a response. * ILLEGAL DATA ADDRESS is sent as a response.
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
* currently not available and the application dependent response * currently not available and the application dependent response
* timeout would be violated. In this case a SLAVE DEVICE BUSY * timeout would be violated. In this case a SLAVE DEVICE BUSY
* exception is sent as a response. * exception is sent as a response.
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
* a SLAVE DEVICE FAILURE exception is sent as a response. * a SLAVE DEVICE FAILURE exception is sent as a response.
@ -421,7 +422,7 @@ eMBErrorCode eMBRegCoilsCB(uint8_t *pucRegBuffer, uint16_t usAddress,
* as a response. * as a response.
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is * - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
* currently not available and the application dependent response * currently not available and the application dependent response
* timeout would be violated. In this case a SLAVE DEVICE BUSY * timeout would be violated. In this case a SLAVE DEVICE BUSY
* exception is sent as a response. * exception is sent as a response.
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case * - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
* a SLAVE DEVICE FAILURE exception is sent as a response. * a SLAVE DEVICE FAILURE exception is sent as a response.

465
include/modbus/mb_m.h Normal file
View File

@ -0,0 +1,465 @@
/****************************************************************************
* apps/include/modbus/mb_m.h
*
* FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (C) 2013 Armink <armink.ztl@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_MODBUS_MB_M_H
#define __APPS_INCLUDE_MODBUS_MB_M_H
/* This module defines the interface for the application. It contains
* the basic functions and types required to use the Modbus Master protocol stack.
* A typical application will want to call eMBMasterInit() first. If the device
* is ready to answer network requests it must then call eMBEnable() to activate
* the protocol stack. In the main loop the function eMBMasterPoll() must be called
* periodically. The time interval between pooling depends on the configured
* Modbus timeout. If an RTOS is available a separate task should be created
* and the task should always call the function eMBMasterPoll().
*
* // Initialize protocol stack in RTU mode for a Master
*
* eMBMasterInit(MB_RTU, 38400, MB_PAR_EVEN);
*
* // Enable the Modbus Protocol Stack.
*
* eMBMasterEnable();
* for(;;)
* {
* // Call the main polling loop of the Modbus Master protocol stack.
*
* eMBMasterPoll();
* ...
* }
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <termios.h>
#ifdef __cplusplus
PR_BEGIN_EXTERN_C
#endif
#include "mbport.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Use the default Modbus Master TCP port (502) */
#define MB_MASTER_TCP_PORT_USE_DEFAULT 0
/****************************************************************************
* Public Type Definitions
****************************************************************************/
/* Error codes used by all function in the Master request. */
typedef enum
{
MB_MRE_NO_ERR, /* no error. */
MB_MRE_NO_REG, /* illegal register address. */
MB_MRE_ILL_ARG, /* illegal argument. */
MB_MRE_REV_DATA, /* receive data error. */
MB_MRE_TIMEDOUT, /* timeout error occurred. */
MB_MRE_MASTER_BUSY, /* master is busy now. */
MB_MRE_EXE_FUN /* execute function error. */
} eMBMasterReqErrCode;
/* TimerMode is Master 3 kind of Timer modes. */
typedef enum
{
MB_TMODE_T35, /* Master receive frame T3.5 timeout. */
MB_TMODE_RESPOND_TIMEOUT, /* Master wait respond for slave. */
MB_TMODE_CONVERT_DELAY /* Master sent broadcast ,then delay sometime.*/
}eMBMasterTimerMode;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Description:
* Initialize the Modbus Master protocol stack.
*
* This functions initializes the ASCII or RTU module and calls the
* init functions of the porting layer to prepare the hardware. Please
* note that the receiver is still disabled and no Modbus frames are
* processed until eMBMasterEnable() has been called.
*
* Input Parameters:
* eMode If ASCII or RTU mode should be used.
* ucPort The port to use. E.g. 1 for COM1 on windows. This value
* is platform dependent and some ports simply choose to ignore it.
* ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
* on the porting layer.
* eParity Parity used for serial transmission.
*
* Returned Value:
* If no error occurs the function returns eMBErrorCode::MB_ENOERR.
* The protocol is then in the disabled state and ready for activation
* by calling eMBMasterEnable(). Otherwise one of the following error codes
* is returned:
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
*
****************************************************************************/
eMBErrorCode eMBMasterInit(eMBMode eMode, uint8_t ucPort, speed_t ulBaudRate,
eMBParity eParity);
/****************************************************************************
* Description:
* Initialize the Modbus Master protocol stack for Modbus TCP.
*
* This function initializes the Modbus TCP Module. Please note that
* frame processing is still disabled until eMBEnable() is called.
*
* Input Parameters:
* usTCPPort The TCP port to listen on.
*
* Returned Value:
* If the protocol stack has been initialized correctly the function
* returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
* codes is returned:
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
* slave addresses are in the range 1 - 247.
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
*
****************************************************************************/
eMBErrorCode eMBMasterTCPInit(uint16_t usTCPPort);
/****************************************************************************
* Description:
* Release resources used by the protocol stack.
*
* This function disables the Modbus Master protocol stack and release all
* hardware resources. It must only be called when the protocol stack
* is disabled.
*
* \note Note all ports implement this function. A port which wants to
* get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
*
* Returned Value:
* If the resources where released it return eMBErrorCode::MB_ENOERR.
* If the protocol stack is not in the disabled state it returns
* eMBErrorCode::MB_EILLSTATE.
*
****************************************************************************/
eMBErrorCode eMBMasterClose(void);
/****************************************************************************
* Description:
* Enable the Modbus Master protocol stack.
*
* This function enables processing of Modbus Master frames. Enabling the
* protocol stack is only possible if it is in the disabled state.
*
* Returned Value:
* If the protocol stack is now in the state enabled it returns
* eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
* return eMBErrorCode::MB_EILLSTATE.
*
****************************************************************************/
eMBErrorCode eMBMasterEnable(void);
/****************************************************************************
* Description:
* Disable the Modbus Master protocol stack.
*
* This function disables processing of Modbus frames.
*
* Returned Value:
* If the protocol stack has been disabled it returns
* eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
* eMBErrorCode::MB_EILLSTATE.
*
****************************************************************************/
eMBErrorCode eMBMasterDisable(void);
/****************************************************************************
* Description:
* The main pooling loop of the Modbus Master protocol stack.
*
* This function must be called periodically. The timer interval required
* is given by the application dependent Modbus slave timeout. Internally
* the function calls xMBMasterPortEventGet() and waits for an event from
* the receiver or transmitter state machines.
*
* Returned Value:
* If the protocol stack is not in the enabled state the function
* returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
* eMBErrorCode::MB_ENOERR.
*
****************************************************************************/
eMBErrorCode eMBMasterPoll(void);
/****************************************************************************
* Description:
* Registers a callback handler for a given function code.
*
* This function registers a new callback handler for a given function
* code. The callback handler supplied is responsible for interpreting
* the Modbus PDU and the creation of an appropriate response. In case
* of an error it should return one of the possible Modbus exceptions
* which results in a Modbus exception frame sent by the protocol stack.
*
* Input Parameters:
* ucFunctionCode The Modbus function code for which this handler should
* be registers. Valid function codes are in the range 1 to 127.
* pxHandler The function handler which should be called in case
* such a frame is received. If NULL a previously registered function
* handler for this function code is removed.
*
* Returned Value:
* eMBErrorCode::MB_ENOERR if the handler has been installed. If no
* more resources are available it returns eMBErrorCode::MB_ENORES. In
* this case the values in mbconfig.h should be adjusted. If the argument
* was not valid it returns eMBErrorCode::MB_EINVAL.
*
****************************************************************************/
eMBErrorCode eMBMasterRegisterCB(uint8_t ucFunctionCode,
pxMBFunctionHandler pxHandler);
/****************************************************************************
* Description:
* Callback function used if the value of a Input Register is required by
* the protocol stack. The starting register address is given by usAddress
* and the last register is given by usAddress + usNRegs - 1.
*
* The protocol stack does not internally allocate any memory for the
* registers. This makes the protocol stack very small and also usable on
* low end targets. In addition the values don't have to be in the memory
* and could for example be stored in a flash.
*
* Whenever the protocol stack requires a value it calls one of the callback
* function with the register address and the number of registers to read
* as an argument. The application should then read the actual register values
* (for example the ADC voltage) and should store the result in the supplied
* buffer.
*
* If the protocol stack wants to update a register value because a write
* register function was received a buffer with the new register values is
* passed to the callback function. The function should then use these values
* to update the application register values.
*
* Input Parameters:
* pucRegBuffer A buffer where the callback function should write
* the current value of the modbus registers to.
* usAddress The starting address of the register. Input registers
* are in the range 1 - 65535.
* usNRegs Number of registers the callback function must supply.
*
* Returned Value:
* The function must return one of the following error codes:
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
* Modbus response is sent.
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
* within the requested address range. In this case a
* ILLEGAL DATA ADDRESS is sent as a response.
*
****************************************************************************/
eMBErrorCode eMBMasterRegInputCB(uint8_t *pucRegBuffer, uint16_t usAddress,
uint16_t usNRegs);
/****************************************************************************
* Description:
* Callback function used if a Holding Register value is
* read or written by the protocol stack. The starting register address
* is given by usAddress and the last register is given by
* usAddress + usNRegs - 1.
*
* Input Parameters:
* pucRegBuffer If the application registers values should be updated the
* buffer points to the new registers values. If the protocol stack needs
* to now the current values the callback function should write them into
* this buffer.
* usAddress The starting address of the register.
* usNRegs Number of registers to read or write.
* eMode If eMBRegisterMode::MB_REG_WRITE the application register
* values should be updated from the values in the buffer. For example
* this would be the case when the Modbus master has issued an
* WRITE SINGLE REGISTER command.
* If the value eMBRegisterMode::MB_REG_READ the application should copy
* the current values into the buffer pucRegBuffer.
*
* Returned Value:
* The function must return one of the following error codes:
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
* Modbus response is sent.
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
* within the requested address range. In this case a
* ILLEGAL DATA ADDRESS is sent as a response.
*
****************************************************************************/
eMBErrorCode eMBMasterRegHoldingCB(uint8_t *pucRegBuffer, uint16_t usAddress,
uint16_t usNRegs, eMBRegisterMode eMode);
/****************************************************************************
* Description:
* Callback function used if a Coil Register value is
* read or written by the protocol stack. If you are going to use
* this function you might use the functions xMBUtilSetBits() and
* xMBUtilGetBits() for working with bitfields.
*
* Input Parameters:
* pucRegBuffer The bits are packed in bytes where the first coil
* starting at address usAddress is stored in the LSB of the
* first byte in the buffer <code>pucRegBuffer</code>.
* If the buffer should be written by the callback function unused
* coil values (I.e. if not a multiple of eight coils is used) should
* be set to zero.
* usAddress The first coil number.
* usNCoils Number of coil values requested.
* eMode If eMBRegisterMode::MB_REG_WRITE the application values should
* be updated from the values supplied in the buffer pucRegBuffer.
* If eMBRegisterMode::MB_REG_READ the application should store the
* current values in the buffer pucRegBuffer.
*
* Returned Value:
* The function must return one of the following error codes:
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
* Modbus response is sent.
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
* within the requested address range. In this case a
* ILLEGAL DATA ADDRESS is sent as a response.
*
****************************************************************************/
eMBErrorCode eMBMasterRegCoilsCB(uint8_t *pucRegBuffer, uint16_t usAddress,
uint16_t usNCoils, eMBRegisterMode eMode);
/****************************************************************************
* Description:
* Callback function used if a Input Discrete Register value is
* read by the protocol stack.
*
* If you are going to use his function you might use the functions
* xMBUtilSetBits() and xMBUtilGetBits() for working with bitfields.
*
* Input Parameters:
* pucRegBuffer The buffer should be updated with the current
* coil values. The first discrete input starting at usAddress must be
* stored at the LSB of the first byte in the buffer. If the requested number
* is not a multiple of eight the remaining bits should be set to zero.
* usAddress The starting address of the first discrete input.
* usNDiscrete Number of discrete input values.
*
* Returned Value:
* The function must return one of the following error codes:
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
* Modbus response is sent.
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
* within the requested address range. In this case a
* ILLEGAL DATA ADDRESS is sent as a response.
*
****************************************************************************/
eMBErrorCode eMBMasterRegDiscreteCB(uint8_t *pucRegBuffer, uint16_t usAddress,
uint16_t usNDiscrete);
/****************************************************************************
* Description:
* These Modbus functions are called for user when Modbus run in Master Mode.
*
****************************************************************************/
eMBMasterReqErrCode eMBMasterReqReadInputRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usNRegs, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usRegData, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqWriteMultipleHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usNRegs, uint16_t *pusDataBuffer,
uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqReadHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usNRegs, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqReadWriteMultipleHoldingRegister(uint8_t ucSndAddr,
uint16_t usReadRegAddr, uint16_t usNReadRegs, uint16_t *pusDataBuffer,
uint16_t usWriteRegAddr, uint16_t usNWriteRegs, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqReadCoils(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usNCoils, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqWriteCoil(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usCoilData, uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usNCoils, uint8_t *pucDataBuffer,
uint32_t lTimeOut);
eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs(uint8_t ucSndAddr,
uint16_t usDiscreteAddr, uint16_t usNDiscreteIn, uint32_t lTimeOut);
eMBException eMBMasterFuncReportSlaveID(uint8_t *pucFrame, uint16_t *usLen);
eMBException eMBMasterFuncReadInputRegister(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncReadHoldingRegister(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncWriteHoldingRegister(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncWriteMultipleHoldingRegister(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncReadCoils(uint8_t *pucFrame, uint16_t *usLen);
eMBException eMBMasterFuncWriteCoil(uint8_t *pucFrame, uint16_t *usLen);
eMBException eMBMasterFuncWriteMultipleCoils(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncReadDiscreteInputs(uint8_t *pucFrame,
uint16_t *usLen);
eMBException eMBMasterFuncReadWriteMultipleHoldingRegister(uint8_t *pucFrame,
uint16_t *usLen);
/* These functions are interface for Modbus Master */
void vMBMasterGetPDUSndBuf(uint8_t **pucFrame);
uint8_t ucMBMasterGetDestAddress(void);
void vMBMasterSetDestAddress(uint8_t Address);
bool xMBMasterGetCBRunInMasterMode(void);
void vMBMasterSetCBRunInMasterMode(bool IsMasterMode);
uint16_t usMBMasterGetPDUSndLength(void);
void vMBMasterSetPDUSndLength(uint16_t SendPDULength);
void vMBMasterSetCurTimerMode(eMBMasterTimerMode eMBTimerMode);
bool xMBMasterRequestIsBroadcast(void);
eMBMasterErrorEventType eMBMasterGetErrorType(void);
void vMBMasterSetErrorType(eMBMasterErrorEventType errorType);
eMBMasterReqErrCode eMBMasterWaitRequestFinish(void);
#ifdef __cplusplus
PR_END_EXTERN_C
#endif
#endif /* __APPS_INCLUDE_MODBUS_MB_M_H */

View File

@ -55,6 +55,26 @@ typedef enum
EV_FRAME_SENT /* Frame sent. */ EV_FRAME_SENT /* Frame sent. */
} eMBEventType; } eMBEventType;
typedef enum
{
EV_MASTER_READY = 1<<0, /* Startup finished. */
EV_MASTER_FRAME_RECEIVED = 1<<1, /* Frame received. */
EV_MASTER_EXECUTE = 1<<2, /* Execute function. */
EV_MASTER_FRAME_SENT = 1<<3, /* Frame sent. */
EV_MASTER_ERROR_PROCESS = 1<<4, /* Frame error process. */
EV_MASTER_PROCESS_SUCESS = 1<<5, /* Request process success. */
EV_MASTER_ERROR_RESPOND_TIMEOUT = 1<<6, /* Request respond timeout. */
EV_MASTER_ERROR_RECEIVE_DATA = 1<<7, /* Request receive data error. */
EV_MASTER_ERROR_EXECUTE_FUNCTION = 1<<8 /* Request execute function error. */
} eMBMasterEventType;
typedef enum
{
EV_ERROR_RESPOND_TIMEOUT, /* Slave respond timeout. */
EV_ERROR_RECEIVE_DATA, /* Receive frame data erroe. */
EV_ERROR_EXECUTE_FUNCTION /* Execute function error. */
} eMBMasterErrorEventType;
/* Parity used for characters in serial mode. /* Parity used for characters in serial mode.
* *
* The parity which should be applied to the characters sent over the serial * The parity which should be applied to the characters sent over the serial
@ -84,9 +104,13 @@ typedef enum
* currently blocked on the eventqueue. * currently blocked on the eventqueue.
*/ */
extern bool(*pxMBFrameCBByteReceived)(void); extern bool(*pxMBFrameCBByteReceived)(void);
extern bool(*pxMBFrameCBTransmitterEmpty)(void); extern bool(*pxMBFrameCBTransmitterEmpty)(void);
extern bool(*pxMBPortCBTimerExpired)(void); extern bool(*pxMBPortCBTimerExpired)(void);
extern bool(*pxMBMasterFrameCBByteReceived)(void);
extern bool(*pxMBMasterFrameCBTransmitterEmpty)(void);
extern bool(*pxMBMasterPortCBTimerExpired)(void);
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes
@ -96,7 +120,14 @@ extern bool(*pxMBPortCBTimerExpired)(void);
bool xMBPortEventInit(void); bool xMBPortEventInit(void);
bool xMBPortEventPost(eMBEventType eEvent); bool xMBPortEventPost(eMBEventType eEvent);
bool xMBPortEventGet(/*@out@ */ eMBEventType * eEvent); bool xMBPortEventGet(eMBEventType *eEvent);
bool xMBMasterPortEventInit(void);
bool xMBMasterPortEventPost(eMBMasterEventType eEvent);
bool xMBMasterPortEventGet(eMBMasterEventType * eEvent);
void vMBMasterOsResInit(void);
bool xMBMasterRunResTake(int32_t time);
void vMBMasterRunResRelease(void);
/* Serial port functions */ /* Serial port functions */
@ -108,6 +139,14 @@ void vMBPortSerialEnable(bool xRxEnable, bool xTxEnable);
bool xMBPortSerialGetByte(int8_t * pucByte); bool xMBPortSerialGetByte(int8_t * pucByte);
bool xMBPortSerialPutByte(int8_t ucByte); bool xMBPortSerialPutByte(int8_t ucByte);
bool xMBMasterPortSerialInit(uint8_t ucPort, speed_t ulBaudRate,
uint8_t ucDataBits, eMBParity eParity);
void vMBMasterPortClose(void);
void xMBMasterPortSerialClose(void);
void vMBMasterPortSerialEnable(bool xRxEnable, bool xTxEnable);
bool xMBMasterPortSerialGetByte(int8_t * pucByte);
bool xMBMasterPortSerialPutByte(int8_t ucByte);
/* Timers functions */ /* Timers functions */
bool xMBPortTimersInit(uint16_t usTimeOut50us); bool xMBPortTimersInit(uint16_t usTimeOut50us);
@ -116,6 +155,23 @@ void vMBPortTimersEnable(void);
void vMBPortTimersDisable(void); void vMBPortTimersDisable(void);
void vMBPortTimersDelay(uint16_t usTimeOutMS); void vMBPortTimersDelay(uint16_t usTimeOutMS);
bool xMBMasterPortTimersInit(uint16_t usTimeOut50us);
void xMBMasterPortTimersClose(void);
void vMBMasterPortTimersT35Enable(void);
void vMBMasterPortTimersConvertDelayEnable(void);
void vMBMasterPortTimersRespondTimeoutEnable(void);
void vMBMasterPortTimersDisable(void);
/* Callback for the master error process */
void vMBMasterErrorCBRespondTimeout(uint8_t ucDestAddress, const uint8_t *pucPDUData,
uint16_t ucPDULength);
void vMBMasterErrorCBReceiveData(uint8_t ucDestAddress, const uint8_t *pucPDUData,
uint16_t ucPDULength);
void vMBMasterErrorCBExecuteFunction(uint8_t ucDestAddress, const uint8_t *pucPDUData,
uint16_t ucPDULength);
void vMBMasterCBRequestScuuess(void);
#ifdef CONFIG_MB_TCP_ENABLED #ifdef CONFIG_MB_TCP_ENABLED
/* TCP port function */ /* TCP port function */

View File

@ -13,10 +13,20 @@ config MB_ASCII_ENABLED
bool "Modbus ASCII support" bool "Modbus ASCII support"
default y default y
config MB_ASCII_MASTER
bool "Modbus ASCII master"
default n
depends on MB_ASCII_ENABLED
config MB_RTU_ENABLED config MB_RTU_ENABLED
bool "Modbus RTU support" bool "Modbus RTU support"
default y default y
config MB_RTU_MASTER
bool "Modbus RTU master"
default n
depends on MB_RTU_ENABLED
config MB_TCP_ENABLED config MB_TCP_ENABLED
bool "Modbus TCP support" bool "Modbus TCP support"
default y default y
@ -41,6 +51,7 @@ config MB_ASCII_TIMEOUT_SEC
a configuration option. It should be set to the maximum expected delay a configuration option. It should be set to the maximum expected delay
time of the network. time of the network.
config MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS config MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
int "Timeout to wait before sending" int "Timeout to wait before sending"
depends on MB_ASCII_ENABLED depends on MB_ASCII_ENABLED
@ -137,4 +148,14 @@ config MB_FUNC_READWRITE_HOLDING_ENABLED
---help--- ---help---
If the Read/Write Multiple Registers function should be enabled. If the Read/Write Multiple Registers function should be enabled.
if MB_ASCII_MASTER || MB_RTU_MASTER
config MB_MASTER_TOTAL_SLAVE_NUM
int "Total number slaves"
default 16
---help---
The total slaves in Modbus Master system. Default 16.
NOTE: The slave ID must be continuous from 1.
endif # MB_ASCII_MASTER || MB_RTU_MASTER
endif # MODBUS endif # MODBUS

View File

@ -5,9 +5,9 @@ This directory contains a port of last open source version of FreeModBus
(BSD license). The code in this directory is a subset of FreeModBus version (BSD license). The code in this directory is a subset of FreeModBus version
1.5.0 (June 6, 2010) that can be downloaded in its entirety from http://developer.berlios.de/project/showfiles.php?group_id=6120. 1.5.0 (June 6, 2010) that can be downloaded in its entirety from http://developer.berlios.de/project/showfiles.php?group_id=6120.
NOTE: There are extensions to support RTU master mode available here: Includes extensions to support RTU master mode by Armink(383016632@qq.com):
https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32, author: https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32. Ported to
Armink(383016632@qq.com) NuttX by Darcy Gong.
Directory Structure/Relation to freemodbus-v1.5.0 Directory Structure/Relation to freemodbus-v1.5.0
------------------------------------------------- -------------------------------------------------
@ -62,7 +62,9 @@ The NuttX-named configuration options that are available include:
CONFIG_MODBUS - General ModBus support CONFIG_MODBUS - General ModBus support
CONFIG_MB_ASCII_ENABLED - Modbus ASCII support CONFIG_MB_ASCII_ENABLED - Modbus ASCII support
CONFIG_MB_ASCII_MASTER - Modbus ASCII master support
CONFIG_MB_RTU_ENABLED - Modbus RTU support CONFIG_MB_RTU_ENABLED - Modbus RTU support
CONFIG_MB_RTU_MASTER - Modbus RTU master support
CONFIG_MB_TCP_ENABLED - Modbus TCP support CONFIG_MB_TCP_ENABLED - Modbus TCP support
CONFIG_MB_ASCII_TIMEOUT_SEC - Character timeout value for Modbus ASCII. The CONFIG_MB_ASCII_TIMEOUT_SEC - Character timeout value for Modbus ASCII. The
character timeout value is not fixed for Modbus ASCII and is therefore character timeout value is not fixed for Modbus ASCII and is therefore

View File

@ -35,7 +35,14 @@
CSRCS += mbfunccoils.c mbfuncdiag.c mbfuncdisc.c mbfuncholding.c CSRCS += mbfunccoils.c mbfuncdiag.c mbfuncdisc.c mbfuncholding.c
CSRCS += mbfuncinput.c mbfuncother.c mbutils.c CSRCS += mbfuncinput.c mbfuncother.c mbutils.c
ifeq ($(CONFIG_MB_ASCII_MASTER),y)
CSRCS += mbfunccoils_m.c mbfuncdisc_m.c mbfuncholding_m.c mbfuncinput_m.c CSRCS += mbfunccoils_m.c mbfuncdisc_m.c mbfuncholding_m.c mbfuncinput_m.c
else
ifeq ($(CONFIG_MB_RTU_MASTER),y)
CSRCS += mbfunccoils_m.c mbfuncdisc_m.c mbfuncholding_m.c mbfuncinput_m.c
endif
endif
DEPPATH += --dep-path functions DEPPATH += --dep-path functions
VPATH += :functions VPATH += :functions

View File

@ -42,6 +42,7 @@
#include <apps/modbus/mb.h> #include <apps/modbus/mb.h>
#include <apps/modbus/mbframe.h> #include <apps/modbus/mbframe.h>
#include <apps/modbus/mbproto.h> #include <apps/modbus/mbproto.h>
#include <apps/modbus/mbport.h>
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* apps/modbus/functions/mbfunccoils_m.c * apps/modbus/functions/mbfunccoils_m.c
* *
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (C) 2013 Armink <armink.ztl@gmail.com> * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
* All rights reserved. * All rights reserved.
* *
@ -43,6 +43,7 @@
#include <apps/modbus/mb_m.h> #include <apps/modbus/mb_m.h>
#include <apps/modbus/mbframe.h> #include <apps/modbus/mbframe.h>
#include <apps/modbus/mbproto.h> #include <apps/modbus/mbproto.h>
#include <apps/modbus/mbport.h>
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@ -82,7 +83,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER) || defined(CONFIG_MB_ASCII_MASTER)
/**************************************************************************** /****************************************************************************
* Description: * Description:
@ -99,7 +100,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_READ_COILS_ENABLED > 0 #ifdef CONFIG_MB_FUNC_READ_COILS_ENABLED
eMBMasterReqErrCode eMBMasterReqReadCoils(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqReadCoils(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usCoilAddr,
uint16_t usNCoils, uint16_t usNCoils,
@ -108,7 +109,7 @@ eMBMasterReqErrCode eMBMasterReqReadCoils(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }
@ -229,7 +230,7 @@ eMBException eMBMasterFuncReadCoils(uint8_t *pucFrame, uint16_t *usLen)
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_WRITE_COIL_ENABLED > 0 #ifdef CONFIG_MB_FUNC_WRITE_COIL_ENABLED
eMBMasterReqErrCode eMBMasterReqWriteCoil(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqWriteCoil(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usCoilAddr,
uint16_t usCoilData, uint16_t usCoilData,
@ -238,7 +239,7 @@ eMBMasterReqErrCode eMBMasterReqWriteCoil(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }
@ -341,7 +342,7 @@ eMBException eMBMasterFuncWriteCoil(uint8_t *pucFrame, uint16_t *usLen)
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0 #ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED
eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils(uint8_t ucSndAddr,
uint16_t usCoilAddr, uint16_t usCoilAddr,
uint16_t usNCoils, uint16_t usNCoils,
@ -353,7 +354,7 @@ eMBMasterReqErrCode eMBMasterReqWriteMultipleCoils(uint8_t ucSndAddr,
uint8_t ucByteCount; uint8_t ucByteCount;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* apps/modbus/functions/mbfuncdisc_m.c * apps/modbus/functions/mbfuncdisc_m.c
* *
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (C) 2013 Armink <armink.ztl@gmail.com> * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
* All rights reserved. * All rights reserved.
* *
@ -65,7 +65,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER) || defined(CONFIG_MB_ASCII_MASTER)
/**************************************************************************** /****************************************************************************
* Description: * Description:
@ -82,7 +82,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0 #ifdef CONFIG_MB_FUNC_READ_DISCRETE_INPUTS_ENABLED
eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs(uint8_t ucSndAddr,
uint16_t usDiscreteAddr, uint16_t usDiscreteAddr,
uint16_t usNDiscreteIn, uint16_t usNDiscreteIn,
@ -91,7 +91,7 @@ eMBMasterReqErrCode eMBMasterReqReadDiscreteInputs(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* apps/modbus/functions/mbfuncholding_m.c * apps/modbus/functions/mbfuncholding_m.c
* *
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Library: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (C) 2013 Armink <armink.ztl@gmail.com> * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
* All rights reserved. * All rights reserved.
* *
@ -94,7 +94,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER) || defined(CONFIG_MB_ASCII_MASTER)
/**************************************************************************** /****************************************************************************
* Description: * Description:
@ -111,7 +111,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0 #ifdef CONFIG_MB_FUNC_WRITE_HOLDING_ENABLED
eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usRegAddr,
uint16_t usRegData, uint16_t usRegData,
@ -120,7 +120,7 @@ eMBMasterReqErrCode eMBMasterReqWriteHoldingRegister(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }
@ -195,7 +195,7 @@ eMBException eMBMasterFuncWriteHoldingRegister(uint8_t *pucFrame,
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0 #ifdef CONFIG_MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteMultipleHoldingRegister(uint8_t ucSndAddr, eMBMasterReqWriteMultipleHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usRegAddr,
@ -207,7 +207,7 @@ eMBMasterReqErrCode
uint16_t usRegIndex = 0; uint16_t usRegIndex = 0;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }
@ -313,7 +313,7 @@ eMBException eMBMasterFuncWriteMultipleHoldingRegister(uint8_t *pucFrame,
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_READ_HOLDING_ENABLED > 0 #ifdef CONFIG_MB_FUNC_READ_HOLDING_ENABLED
eMBMasterReqErrCode eMBMasterReqReadHoldingRegister(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqReadHoldingRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usRegAddr,
uint16_t usNRegs, uint16_t usNRegs,
@ -322,7 +322,7 @@ eMBMasterReqErrCode eMBMasterReqReadHoldingRegister(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }
@ -429,7 +429,7 @@ eMBException eMBMasterFuncReadHoldingRegister(uint8_t *pucFrame,
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0 #ifdef CONFIG_MB_FUNC_READWRITE_HOLDING_ENABLED
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadWriteMultipleHoldingRegister(uint8_t ucSndAddr, eMBMasterReqReadWriteMultipleHoldingRegister(uint8_t ucSndAddr,
uint16_t usReadRegAddr, uint16_t usReadRegAddr,
@ -443,7 +443,7 @@ eMBMasterReqErrCode
uint16_t usRegIndex = 0; uint16_t usRegIndex = 0;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* apps/modbus/functions/mbfuncinput_m.c * apps/modbus/functions/mbfuncinput_m.c
* *
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (C) 2013 Armink <armink.ztl@gmail.com> * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
* All rights reserved. * All rights reserved.
@ -67,7 +67,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER) || defined(CONFIG_MB_ASCII_MASTER)
/**************************************************************************** /****************************************************************************
* Description: * Description:
@ -84,7 +84,7 @@ eMBException prveMBError2Exception(eMBErrorCode eErrorCode);
* *
****************************************************************************/ ****************************************************************************/
#if MB_FUNC_READ_INPUT_ENABLED > 0 #if defined(CONFIG_MB_FUNC_READ_INPUT_ENABLED)
eMBMasterReqErrCode eMBMasterReqReadInputRegister(uint8_t ucSndAddr, eMBMasterReqErrCode eMBMasterReqReadInputRegister(uint8_t ucSndAddr,
uint16_t usRegAddr, uint16_t usRegAddr,
uint16_t usNRegs, uint16_t usNRegs,
@ -93,7 +93,7 @@ eMBMasterReqErrCode eMBMasterReqReadInputRegister(uint8_t ucSndAddr,
uint8_t *ucMBFrame; uint8_t *ucMBFrame;
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR; eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
if (ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSndAddr > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
eErrStatus = MB_MRE_ILL_ARG; eErrStatus = MB_MRE_ILL_ARG;
} }

View File

@ -36,7 +36,9 @@
ifeq ($(CONFIG_MB_RTU_ENABLED),y) ifeq ($(CONFIG_MB_RTU_ENABLED),y)
CSRCS += mbcrc.c mbrtu.c CSRCS += mbcrc.c mbrtu.c
ifeq ($(CONFIG_MB_RTU_MASTER),y)
CSRCS += mbrtu_m.c CSRCS += mbrtu_m.c
endif
DEPPATH += --dep-path rtu DEPPATH += --dep-path rtu
VPATH += :rtu VPATH += :rtu

View File

@ -37,7 +37,7 @@ PR_BEGIN_EXTERN_C
#endif #endif
/**************************************************************************** /****************************************************************************
* Global Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
eMBErrorCode eMBRTUInit(uint8_t slaveAddress, uint8_t ucPort, eMBErrorCode eMBRTUInit(uint8_t slaveAddress, uint8_t ucPort,
@ -53,7 +53,7 @@ bool xMBRTUTransmitFSM(void);
bool xMBRTUTimerT15Expired(void); bool xMBRTUTimerT15Expired(void);
bool xMBRTUTimerT35Expired(void); bool xMBRTUTimerT35Expired(void);
#if MB_MASTER_RTU_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER)
eMBErrorCode eMBMasterRTUInit(uint8_t ucPort, speed_t ulBaudRate, eMBErrorCode eMBMasterRTUInit(uint8_t ucPort, speed_t ulBaudRate,
eMBParity eParity); eMBParity eParity);
void eMBMasterRTUStart(void); void eMBMasterRTUStart(void);

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* apps/modbus/rtu/mbrtu_m.c * apps/modbus/rtu/mbrtu_m.c
* *
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (c) 2013 China Beijing Armink <armink.ztl@gmail.com> * Copyright (c) 2013 China Beijing Armink <armink.ztl@gmail.com>
* All rights reserved. * All rights reserved.
@ -48,7 +48,7 @@
#include <apps/modbus/mbport.h> #include <apps/modbus/mbport.h>
#if MB_MASTER_RTU_ENABLED > 0 #if defined(CONFIG_RTU_ASCII_MASTER)
/**************************************************************************** /****************************************************************************
* Included Files * Included Files
@ -132,7 +132,7 @@ eMBErrorCode eMBMasterRTUInit(uint8_t ucPort, speed_t ulBaudRate,
} }
else else
{ {
/* The timer reload value for a character is given by: ChTimeValue = /* The timer reload value for a character is given by: ChTimeValue =
* Ticks_per_1s / ( Baudrate / 11 ) = 11 * Ticks_per_1s / Baudrate = * Ticks_per_1s / ( Baudrate / 11 ) = 11 * Ticks_per_1s / Baudrate =
* 220000 / Baudrate The reload for t3.5 is 1.5 times this value and * 220000 / Baudrate The reload for t3.5 is 1.5 times this value and
* similary for t3.5. * similary for t3.5.
@ -155,7 +155,7 @@ void eMBMasterRTUStart(void)
{ {
ENTER_CRITICAL_SECTION(); ENTER_CRITICAL_SECTION();
/* Initially the receiver is in the state STATE_M_RX_INIT. we start the timer /* Initially the receiver is in the state STATE_M_RX_INIT. we start the timer
* and if no character is received within t3.5 we change to STATE_M_RX_IDLE. * and if no character is received within t3.5 we change to STATE_M_RX_IDLE.
* This makes sure that we delay startup of the modbus protocol stack until * This makes sure that we delay startup of the modbus protocol stack until
* the bus is free. * the bus is free.
@ -222,14 +222,14 @@ eMBErrorCode eMBMasterRTUSend(uint8_t ucSlaveAddress, const uint8_t *pucFrame,
eMBErrorCode eStatus = MB_ENOERR; eMBErrorCode eStatus = MB_ENOERR;
uint16_t usCRC16; uint16_t usCRC16;
if (ucSlaveAddress > MB_MASTER_TOTAL_SLAVE_NUM) if (ucSlaveAddress > CONFIG_MB_MASTER_TOTAL_SLAVE_NUM)
{ {
return MB_EINVAL; return MB_EINVAL;
} }
ENTER_CRITICAL_SECTION(); ENTER_CRITICAL_SECTION();
/* Check if the receiver is still in idle state. If not we where to slow with /* Check if the receiver is still in idle state. If not we where to slow with
* processing the received frame and the master sent another frame on the * processing the received frame and the master sent another frame on the
* network. We have to abort sending the frame. * network. We have to abort sending the frame.
*/ */
@ -304,7 +304,7 @@ bool xMBMasterRTUReceiveFSM(void)
*/ */
case STATE_M_RX_IDLE: case STATE_M_RX_IDLE:
/* In time of respond timeout,the receiver receive a frame. Disable timer /* In time of respond timeout,the receiver receive a frame. Disable timer
* of respond timeout and change the transmitter state to idle. * of respond timeout and change the transmitter state to idle.
*/ */