debug modbus
This commit is contained in:
+2
-2
@@ -1430,8 +1430,8 @@ CONFIG_BSP_USING_GPIO=y
|
||||
CONFIG_BSP_USING_UART=y
|
||||
CONFIG_BSP_STM32_UART_V1_TX_TIMEOUT=2000
|
||||
CONFIG_BSP_USING_UART1=y
|
||||
CONFIG_BSP_UART1_RX_USING_DMA=y
|
||||
CONFIG_BSP_UART1_TX_USING_DMA=y
|
||||
# CONFIG_BSP_UART1_RX_USING_DMA is not set
|
||||
# CONFIG_BSP_UART1_TX_USING_DMA is not set
|
||||
CONFIG_BSP_USING_UART2=y
|
||||
# CONFIG_BSP_UART2_RX_USING_DMA is not set
|
||||
# CONFIG_BSP_UART2_TX_USING_DMA is not set
|
||||
|
||||
@@ -99,3 +99,5 @@ static int chrg_i2c_init (void)
|
||||
|
||||
INIT_ENV_EXPORT(chrg_i2c_init);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -87,7 +87,8 @@ rt_err_t chrg_tmr_start (eIDX_TMR index, uint32_t timeo)
|
||||
pTMR->timeout.sec = timeo/1000000;
|
||||
pTMR->timeout.usec = timeo%1000000;
|
||||
|
||||
if (rt_device_write(pTMR->dev, 0, &pTMR->timeout, sizeof(rt_hwtimerval_t)) != sizeof(rt_hwtimerval_t)) {
|
||||
if (rt_device_write(pTMR->dev, 0, &pTMR->timeout,
|
||||
sizeof(rt_hwtimerval_t)) != sizeof(rt_hwtimerval_t)) {
|
||||
LOG_E("set timer '%s' timeout failed.", pTMR->name);
|
||||
return ret;
|
||||
}
|
||||
@@ -145,7 +146,7 @@ int chrg_tmrs_init (void)
|
||||
|
||||
INIT_ENV_EXPORT(chrg_tmrs_init);
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
|
||||
static int sec_tmr(int argc, char **argv)
|
||||
{
|
||||
|
||||
@@ -45,7 +45,7 @@ static int chrg_tty_cal_byte_tmo (int baudrate)
|
||||
}
|
||||
|
||||
// mode : 0--receive mode, 1--send mode
|
||||
static void chrg_tty_mode_set (struct chrg_tty_t *pTTY, int mode)
|
||||
void chrg_tty_mode_set (struct chrg_tty_t *pTTY, int mode)
|
||||
{
|
||||
if (pTTY->pin < 0) {
|
||||
return;
|
||||
|
||||
@@ -84,6 +84,8 @@ extern int chrg_tty_destory (struct chrg_tty_t *pTTY);
|
||||
extern int chrg_tty_config (struct chrg_tty_t *pTTY,
|
||||
int baudrate, int databits, int parity, int stopbits);
|
||||
|
||||
extern void chrg_tty_mode_set (struct chrg_tty_t *pTTY, int mode);
|
||||
|
||||
/*
|
||||
* @brief set wait datas timeout for receiving
|
||||
* @param pTTY - instance handle
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c')
|
||||
|
||||
path = [cwd]
|
||||
|
||||
group = DefineGroup('Applications/mb', src, depend = [''], CPPPATH = path)
|
||||
|
||||
list = os.listdir(cwd)
|
||||
for item in list:
|
||||
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
|
||||
group = group + SConscript(os.path.join(item, 'SConscript'))
|
||||
|
||||
Return('group')
|
||||
@@ -1,211 +0,0 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbfunc.h"
|
||||
#include "mbport.h"
|
||||
#include "mbrtu.h"
|
||||
|
||||
|
||||
static rt_uint8_t ucMBAddress;
|
||||
|
||||
static enum {
|
||||
STATE_ENABLED,
|
||||
STATE_DISABLED,
|
||||
STATE_NOT_INITIALIZED
|
||||
} eMBState = STATE_NOT_INITIALIZED;
|
||||
|
||||
|
||||
#define MB_FUNC_HANDLERS_MAX (16)
|
||||
|
||||
static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
||||
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
||||
{MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
|
||||
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
||||
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
||||
{MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
|
||||
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
||||
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
|
||||
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
||||
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
|
||||
};
|
||||
|
||||
eMBErrorCode eMBInit (rt_uint8_t ucSlaveAddress, rt_uint8_t ucPort,
|
||||
rt_uint32_t ulBaudRate, eMBParity eParity )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if ((ucSlaveAddress == MB_ADDRESS_BROADCAST) ||
|
||||
(ucSlaveAddress < MB_ADDRESS_MIN)||(ucSlaveAddress > MB_ADDRESS_MAX)) {
|
||||
eStatus = MB_EINVAL;
|
||||
} else {
|
||||
ucMBAddress = ucSlaveAddress;
|
||||
|
||||
eStatus = eMBRTUInit(ucMBAddress, ucPort, ulBaudRate, eParity);
|
||||
if (eStatus == MB_ENOERR) {
|
||||
if (!xMBPortEventInit()) {
|
||||
/* port dependent event module initalization failed. */
|
||||
eStatus = MB_EPORTERR;
|
||||
} else {
|
||||
eMBState = STATE_DISABLED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRegisterCB (rt_uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler)
|
||||
{
|
||||
int i;
|
||||
eMBErrorCode eStatus;
|
||||
|
||||
if ((0 < ucFunctionCode) && (ucFunctionCode <= 127)) {
|
||||
EnterCriticalSection();
|
||||
if ( pxHandler != NULL ) {
|
||||
for ( i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
|
||||
if ((xFuncHandlers[i].pxHandler == NULL) ||
|
||||
(xFuncHandlers[i].pxHandler == pxHandler)) {
|
||||
xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
|
||||
xFuncHandlers[i].pxHandler = pxHandler;
|
||||
break;
|
||||
}
|
||||
}
|
||||
eStatus = (i != MB_FUNC_HANDLERS_MAX) ? MB_ENOERR : MB_ENORES;
|
||||
} else {
|
||||
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
|
||||
if (xFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
|
||||
xFuncHandlers[i].ucFunctionCode = 0;
|
||||
xFuncHandlers[i].pxHandler = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Remove can't fail. */
|
||||
eStatus = MB_ENOERR;
|
||||
}
|
||||
ExitCriticalSection();
|
||||
} else {
|
||||
eStatus = MB_EINVAL;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
eMBErrorCode eMBClose (void)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if (eMBState == STATE_DISABLED) {
|
||||
} else {
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
eMBErrorCode eMBEnable (void)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
if (eMBState == STATE_DISABLED) {
|
||||
/* Activate the protocol stack. */
|
||||
eMBRTUStart();
|
||||
eMBState = STATE_ENABLED;
|
||||
} else {
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBDisable (void)
|
||||
{
|
||||
eMBErrorCode eStatus;
|
||||
|
||||
if (eMBState == STATE_ENABLED) {
|
||||
eMBRTUStop();
|
||||
eMBState = STATE_DISABLED;
|
||||
eStatus = MB_ENOERR;
|
||||
} else if (eMBState == STATE_DISABLED) {
|
||||
eStatus = MB_ENOERR;
|
||||
} else {
|
||||
eStatus = MB_EILLSTATE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBPoll (void)
|
||||
{
|
||||
static rt_uint8_t *ucMBFrame;
|
||||
static rt_uint8_t ucRcvAddress;
|
||||
static rt_uint8_t ucFunctionCode;
|
||||
static rt_uint16_t usLength;
|
||||
static eMBException eException;
|
||||
|
||||
int i;
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
eMBEventType eEvent;
|
||||
|
||||
/* Check if the protocol stack is ready. */
|
||||
if (eMBState != STATE_ENABLED) {
|
||||
return MB_EILLSTATE;
|
||||
}
|
||||
|
||||
/* Check if there is a event available. If not return control to caller.
|
||||
* Otherwise we will handle the event. */
|
||||
if (xMBPortEventGet(&eEvent) == TRUE) {
|
||||
switch (eEvent) {
|
||||
case EV_READY:
|
||||
break;
|
||||
|
||||
case EV_FRAME_RECEIVED:
|
||||
eStatus = eMBRTUReceive(&ucRcvAddress, &ucMBFrame, &usLength);
|
||||
if (eStatus == MB_ENOERR) {
|
||||
/* Check if the frame is for us. If not ignore the frame. */
|
||||
if ((ucRcvAddress == ucMBAddress) || (ucRcvAddress == MB_ADDRESS_BROADCAST)) {
|
||||
(void)xMBPortEventPost(EV_EXECUTE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case EV_EXECUTE:
|
||||
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
||||
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++) {
|
||||
/* No more function handlers registered. Abort. */
|
||||
if(xFuncHandlers[i].ucFunctionCode == 0) {
|
||||
break;
|
||||
} else if(xFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
|
||||
eException = xFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the request was not sent to the broadcast address we
|
||||
* return a reply. */
|
||||
if (ucRcvAddress != MB_ADDRESS_BROADCAST) {
|
||||
if (eException != MB_EX_NONE) {
|
||||
/* An exception occured. Build an error frame. */
|
||||
usLength = 0;
|
||||
ucMBFrame[usLength++] = (rt_uint8_t)(ucFunctionCode | MB_FUNC_ERROR);
|
||||
ucMBFrame[usLength++] = eException;
|
||||
}
|
||||
|
||||
eStatus = eMBRTUSend(ucMBAddress, ucMBFrame, usLength);
|
||||
}
|
||||
break;
|
||||
|
||||
case EV_FRAME_SENT:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return MB_ENOERR;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
#ifndef __MB_H__
|
||||
#define __MB_H__
|
||||
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef enum {
|
||||
MB_REG_READ, /*!< Read register values and pass to protocol stack. */
|
||||
MB_REG_WRITE /*!< Update register values. */
|
||||
} eMBRegisterMode;
|
||||
|
||||
typedef enum {
|
||||
MB_ENOERR, /*!< no error. */
|
||||
MB_ENOREG, /*!< illegal register address. */
|
||||
MB_EINVAL, /*!< illegal argument. */
|
||||
MB_EPORTERR, /*!< porting layer error. */
|
||||
MB_ENORES, /*!< insufficient resources. */
|
||||
MB_EIO, /*!< I/O error. */
|
||||
MB_EILLSTATE, /*!< protocol stack in illegal state. */
|
||||
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||
} eMBErrorCode;
|
||||
|
||||
typedef enum {
|
||||
EV_READY = 1<<0, /*!< Startup finished. */
|
||||
EV_FRAME_RECEIVED = 1<<1, /*!< Frame received. */
|
||||
EV_EXECUTE = 1<<2, /*!< Execute function. */
|
||||
EV_FRAME_SENT = 1<<3 /*!< Frame sent. */
|
||||
} eMBEventType;
|
||||
|
||||
typedef enum {
|
||||
MB_PAR_NONE, /*!< No parity. */
|
||||
MB_PAR_ODD, /*!< Odd parity. */
|
||||
MB_PAR_EVEN /*!< Even parity. */
|
||||
} eMBParity;
|
||||
|
||||
#define MB_ADDRESS_BROADCAST ( 0 ) /*! Modbus broadcast address. */
|
||||
#define MB_ADDRESS_MIN ( 1 ) /*! Smallest possible slave address. */
|
||||
#define MB_ADDRESS_MAX ( 247 ) /*! Biggest possible slave address. */
|
||||
|
||||
#define MB_FUNC_NONE (0x00)
|
||||
#define MB_FUNC_READ_COILS (0x01)
|
||||
#define MB_FUNC_READ_DISCRETE_INPUTS (0x02)
|
||||
#define MB_FUNC_READ_HOLDING_REGISTER (0x03)
|
||||
#define MB_FUNC_READ_INPUT_REGISTER (0x04)
|
||||
#define MB_FUNC_WRITE_SINGLE_COIL (0x05)
|
||||
#define MB_FUNC_WRITE_REGISTER (0x06)
|
||||
#define MB_FUNC_DIAG_READ_EXCEPTION (0x07)
|
||||
#define MB_FUNC_DIAG_DIAGNOSTIC (0x08)
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT (0x0B)
|
||||
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG (0x0C)
|
||||
#define MB_FUNC_WRITE_MULTIPLE_COILS (0x0F)
|
||||
#define MB_FUNC_WRITE_MULTIPLE_REGISTERS (0x10)
|
||||
#define MB_FUNC_OTHER_REPORT_SLAVEID (0x11)
|
||||
#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS (0x17)
|
||||
#define MB_FUNC_ERROR (0x80)
|
||||
|
||||
typedef enum {
|
||||
MB_EX_NONE = 0x00,
|
||||
MB_EX_ILLEGAL_FUNCTION = 0x01,
|
||||
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
|
||||
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
|
||||
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
|
||||
MB_EX_ACKNOWLEDGE = 0x05,
|
||||
MB_EX_SLAVE_BUSY = 0x06,
|
||||
MB_EX_MEMORY_PARITY_ERROR = 0x08,
|
||||
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
|
||||
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||
} eMBException;
|
||||
|
||||
typedef eMBException(*pxMBFunctionHandler) (rt_uint8_t * pucFrame, rt_uint16_t * pusLength);
|
||||
|
||||
eMBException prveMBError2Exception (eMBErrorCode eErrorCode);
|
||||
|
||||
typedef struct {
|
||||
rt_uint8_t ucFunctionCode;
|
||||
pxMBFunctionHandler pxHandler;
|
||||
} xMBFunctionHandler;
|
||||
|
||||
|
||||
eMBErrorCode eMBInit (rt_uint8_t ucSlaveAddress, rt_uint8_t ucPort,
|
||||
rt_uint32_t ulBaudRate, eMBParity eParity );
|
||||
|
||||
eMBErrorCode eMBClose (void);
|
||||
|
||||
eMBErrorCode eMBEnable (void);
|
||||
|
||||
eMBErrorCode eMBDisable (void);
|
||||
|
||||
eMBErrorCode eMBPoll (void);
|
||||
|
||||
eMBErrorCode eMBSetSlaveID (rt_uint8_t ucSlaveID, rt_uint8_t xIsRunning,
|
||||
rt_uint8_t const *pucAdditional, rt_uint16_t usAdditionalLen);
|
||||
|
||||
eMBErrorCode eMBRegisterCB (rt_uint8_t ucFunctionCode, pxMBFunctionHandler pxHandler);
|
||||
|
||||
eMBErrorCode eMBRegInputCB (rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNRegs);
|
||||
|
||||
eMBErrorCode eMBRegHoldingCB (rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNRegs, eMBRegisterMode eMode);
|
||||
|
||||
eMBErrorCode eMBRegCoilsCB (rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNCoils, eMBRegisterMode eMode);
|
||||
|
||||
eMBErrorCode eMBRegDiscreteCB (rt_uint8_t * pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNDiscrete);
|
||||
|
||||
/*!
|
||||
* Constants which defines the format of a modbus frame. The example is
|
||||
* shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
|
||||
* dependent on the underlying transport.
|
||||
*
|
||||
* <code>
|
||||
* <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | Address | Function Code | Data | CRC/LRC |
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | | | |
|
||||
* (2) (3/2') (3') (4)
|
||||
*
|
||||
* (1) ... MB_SER_PDU_SIZE_MAX = 256
|
||||
* (2) ... MB_SER_PDU_ADDR_OFF = 0
|
||||
* (3) ... MB_SER_PDU_PDU_OFF = 1
|
||||
* (4) ... MB_SER_PDU_SIZE_CRC = 2
|
||||
*
|
||||
* (1') ... MB_PDU_SIZE_MAX = 253
|
||||
* (2') ... MB_PDU_FUNC_OFF = 0
|
||||
* (3') ... MB_PDU_DATA_OFF = 1
|
||||
* </code>
|
||||
*/
|
||||
|
||||
#define MB_PDU_SIZE_MAX 253 /*!< Maximum size of a PDU. */
|
||||
#define MB_PDU_SIZE_MIN 1 /*!< Function Code */
|
||||
#define MB_PDU_FUNC_OFF 0 /*!< Offset of function code in PDU. */
|
||||
#define MB_PDU_DATA_OFF 1 /*!< Offset for response data in PDU. */
|
||||
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
|
||||
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_READ_REGCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_READ_COILCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF (MB_PDU_DATA_OFF + 4)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF (MB_PDU_DATA_OFF + 5)
|
||||
|
||||
#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF (MB_PDU_DATA_OFF + 4)
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF (MB_PDU_DATA_OFF + 6)
|
||||
#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF (MB_PDU_DATA_OFF + 8)
|
||||
#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF (MB_PDU_DATA_OFF + 9)
|
||||
|
||||
#define MB_PDU_FUNC_READ_SIZE (4)
|
||||
#define MB_PDU_FUNC_WRITE_SIZE (4)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN (5)
|
||||
#define MB_PDU_FUNC_READWRITE_SIZE_MIN (9)
|
||||
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_MAX (0x07D0)
|
||||
#define MB_PDU_FUNC_READ_COILCNT_MAX (0x07D0)
|
||||
#define MB_PDU_FUNC_READ_REGCNT_MAX (0x007D)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX (0x0078)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX (0x07B0)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
static const rt_uint8_t aucCRCHi[] = {
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40
|
||||
};
|
||||
|
||||
static const rt_uint8_t aucCRCLo[] = {
|
||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
||||
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
||||
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
||||
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
|
||||
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
||||
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
|
||||
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
|
||||
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
|
||||
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
|
||||
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
||||
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
|
||||
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
|
||||
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
|
||||
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
|
||||
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
||||
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
|
||||
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
|
||||
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
|
||||
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
|
||||
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
||||
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
|
||||
0x41, 0x81, 0x80, 0x40
|
||||
};
|
||||
|
||||
rt_uint16_t usMBCRC16 (rt_uint8_t * pucFrame, rt_uint16_t usLen)
|
||||
{
|
||||
rt_uint8_t ucCRCHi = 0xFF;
|
||||
rt_uint8_t ucCRCLo = 0xFF;
|
||||
int iIndex;
|
||||
|
||||
while ( usLen-- ) {
|
||||
iIndex = ucCRCLo ^ *( pucFrame++ );
|
||||
ucCRCLo = (rt_uint8_t)( ucCRCHi ^ aucCRCHi[iIndex] );
|
||||
ucCRCHi = aucCRCLo[iIndex];
|
||||
}
|
||||
|
||||
return (rt_uint16_t)( ucCRCHi << 8 | ucCRCLo );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
|
||||
#ifndef __MB_CRC_H__
|
||||
#define __MB_CRC_H__
|
||||
|
||||
extern rt_uint16_t usMBCRC16 (rt_uint8_t * pucFrame, rt_uint16_t usLen);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
|
||||
static struct rt_event xSlaveOsEvent;
|
||||
|
||||
rt_uint8_t xMBPortEventInit (void)
|
||||
{
|
||||
rt_event_init(&xSlaveOsEvent, "frame", RT_IPC_FLAG_PRIO);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortEventPost (eMBEventType eEvent)
|
||||
{
|
||||
rt_event_send(&xSlaveOsEvent, eEvent);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortEventGet (eMBEventType * eEvent)
|
||||
{
|
||||
rt_uint32_t recvedEvent;
|
||||
|
||||
rt_event_recv(&xSlaveOsEvent,
|
||||
EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE | EV_FRAME_SENT,
|
||||
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER,
|
||||
&recvedEvent);
|
||||
|
||||
switch (recvedEvent) {
|
||||
case EV_READY:
|
||||
*eEvent = EV_READY;
|
||||
break;
|
||||
case EV_FRAME_RECEIVED:
|
||||
*eEvent = EV_FRAME_RECEIVED;
|
||||
break;
|
||||
case EV_EXECUTE:
|
||||
*eEvent = EV_EXECUTE;
|
||||
break;
|
||||
case EV_FRAME_SENT:
|
||||
*eEvent = EV_FRAME_SENT;
|
||||
break;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,525 +0,0 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
|
||||
|
||||
eMBException eMBFuncReadCoils (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usCoilCount;
|
||||
rt_uint8_t ucNBytes;
|
||||
rt_uint8_t *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == (MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8);
|
||||
usCoilCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1]);
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if ((usCoilCount >= 1) &&
|
||||
(usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX)) {
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_COILS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if ((usCoilCount & 0x0007) != 0) {
|
||||
ucNBytes = (rt_uint8_t)(usCoilCount / 8 + 1);
|
||||
} else {
|
||||
ucNBytes = (rt_uint8_t)(usCoilCount / 8);
|
||||
}
|
||||
*pucFrameCur++ = ucNBytes;
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus = eMBRegCoilsCB(pucFrameCur, usRegAddress, usCoilCount, MB_REG_READ);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen += ucNBytes;;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncReadDiscreteInputs (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usDiscreteCnt;
|
||||
rt_uint8_t ucNBytes;
|
||||
rt_uint8_t *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == (MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usDiscreteCnt = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8);
|
||||
usDiscreteCnt |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1]);
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if ((usDiscreteCnt >= 1) &&
|
||||
(usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX)) {
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||
* byte is only partially field with unused coils set to zero. */
|
||||
if ((usDiscreteCnt & 0x0007) != 0) {
|
||||
ucNBytes = (rt_uint8_t)(usDiscreteCnt / 8 + 1);
|
||||
} else {
|
||||
ucNBytes = (rt_uint8_t)(usDiscreteCnt / 8);
|
||||
}
|
||||
|
||||
*pucFrameCur++ = ucNBytes;
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus = eMBRegDiscreteCB(pucFrameCur, usRegAddress, usDiscreteCnt);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen += ucNBytes;;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid read coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncReadHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usRegCount;
|
||||
rt_uint8_t *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8);
|
||||
usRegCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1]);
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if ((usRegCount >= 1) && (usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX)) {
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = (rt_uint8_t)(usRegCount * 2);
|
||||
*usLen += 1;
|
||||
|
||||
/* Make callback to fill the buffer. */
|
||||
eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ);
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*usLen += usRegCount * 2;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncReadInputRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usRegCount;
|
||||
rt_uint8_t *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == (MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8);
|
||||
usRegCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1]);
|
||||
|
||||
/* Check if the number of registers to read is valid. If not
|
||||
* return Modbus illegal data value exception.
|
||||
*/
|
||||
if ((usRegCount >= 1)
|
||||
&& (usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX)) {
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = (rt_uint8_t)(usRegCount * 2);
|
||||
*usLen += 1;
|
||||
|
||||
eRegStatus = eMBRegInputCB(pucFrameCur, usRegAddress, usRegCount);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*usLen += usRegCount * 2;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid read input register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncWriteCoil (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint8_t ucBuf[2];
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == (MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
if ((pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00) &&
|
||||
((pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF) ||
|
||||
(pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00))) {
|
||||
ucBuf[1] = 0;
|
||||
if (pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF) {
|
||||
ucBuf[0] = 1;
|
||||
} else {
|
||||
ucBuf[0] = 0;
|
||||
}
|
||||
eRegStatus = eMBRegCoilsCB(&ucBuf[0], usRegAddress, 1, MB_REG_WRITE);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncWriteHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen == (MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
/* Make callback to update the value. */
|
||||
eRegStatus = eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF], usRegAddress, 1, MB_REG_WRITE);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncWriteMultipleCoils (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usCoilCnt;
|
||||
rt_uint8_t ucByteCount;
|
||||
rt_uint8_t ucByteCountVerify;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen > (MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usCoilCnt = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8);
|
||||
usCoilCnt |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1]);
|
||||
|
||||
ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
/* Compute the number of expected bytes in the request. */
|
||||
if ((usCoilCnt & 0x0007) != 0) {
|
||||
ucByteCountVerify = (rt_uint8_t)(usCoilCnt / 8 + 1);
|
||||
} else {
|
||||
ucByteCountVerify = (rt_uint8_t)(usCoilCnt / 8);
|
||||
}
|
||||
|
||||
if ((usCoilCnt >= 1) &&
|
||||
(usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX) &&
|
||||
(ucByteCountVerify == ucByteCount)) {
|
||||
eRegStatus = eMBRegCoilsCB(&pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usCoilCnt, MB_REG_WRITE);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
/* The response contains the function code, the starting address
|
||||
* and the quantity of registers. We reuse the old values in the
|
||||
* buffer because they are still valid. */
|
||||
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid write coil register request because the length
|
||||
* is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncWriteMultipleHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegAddress;
|
||||
rt_uint16_t usRegCount;
|
||||
rt_uint8_t ucRegByteCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen >= (MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN)) {
|
||||
usRegAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8);
|
||||
usRegAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1]);
|
||||
usRegAddress++;
|
||||
|
||||
usRegCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8);
|
||||
usRegCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1]);
|
||||
|
||||
ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||
|
||||
if ((usRegCount >= 1) &&
|
||||
(usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX) &&
|
||||
(ucRegByteCount == (rt_uint8_t) (2 * usRegCount))) {
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus =
|
||||
eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||
usRegAddress, usRegCount, MB_REG_WRITE);
|
||||
|
||||
/* If an error occured convert it into a Modbus exception. */
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
/* The response contains the function code, the starting
|
||||
* address and the quantity of registers. We reuse the
|
||||
* old values in the buffer because they are still valid.
|
||||
*/
|
||||
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
} else {
|
||||
/* Can't be a valid request because the length is incorrect. */
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncReadWriteMultipleHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
rt_uint16_t usRegReadAddress;
|
||||
rt_uint16_t usRegReadCount;
|
||||
rt_uint16_t usRegWriteAddress;
|
||||
rt_uint16_t usRegWriteCount;
|
||||
rt_uint8_t ucRegWriteByteCount;
|
||||
rt_uint8_t *pucFrameCur;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (*usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN)) {
|
||||
usRegReadAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U);
|
||||
usRegReadAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1]);
|
||||
usRegReadAddress++;
|
||||
|
||||
usRegReadCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U);
|
||||
usRegReadCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1]);
|
||||
|
||||
usRegWriteAddress = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U);
|
||||
usRegWriteAddress |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1]);
|
||||
usRegWriteAddress++;
|
||||
|
||||
usRegWriteCount = (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U);
|
||||
usRegWriteCount |= (rt_uint16_t)(pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1]);
|
||||
|
||||
ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
|
||||
|
||||
if ((usRegReadCount >= 1) && (usRegReadCount <= 0x7D) &&
|
||||
(usRegWriteCount >= 1) && (usRegWriteCount <= 0x79) &&
|
||||
((2 * usRegWriteCount) == ucRegWriteByteCount)) {
|
||||
/* Make callback to update the register values. */
|
||||
eRegStatus = eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
|
||||
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE);
|
||||
|
||||
if (eRegStatus == MB_ENOERR) {
|
||||
/* Set the current PDU data pointer to the beginning. */
|
||||
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||
*usLen = MB_PDU_FUNC_OFF;
|
||||
|
||||
/* First byte contains the function code. */
|
||||
*pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||
*usLen += 1;
|
||||
|
||||
/* Second byte in the response contain the number of bytes. */
|
||||
*pucFrameCur++ = (rt_uint8_t)(usRegReadCount * 2);
|
||||
*usLen += 1;
|
||||
|
||||
/* Make the read callback. */
|
||||
eRegStatus = eMBRegHoldingCB(pucFrameCur, usRegReadAddress,
|
||||
usRegReadCount, MB_REG_READ);
|
||||
if ( eRegStatus == MB_ENOERR ) {
|
||||
*usLen += 2 * usRegReadCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#define MB_FUNC_OTHER_REP_SLAVEID_BUF (32)
|
||||
|
||||
static rt_uint8_t ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
||||
static rt_uint16_t usMBSlaveIDLen;
|
||||
|
||||
eMBErrorCode eMBSetSlaveID (rt_uint8_t ucSlaveID, rt_uint8_t xIsRunning,
|
||||
rt_uint8_t const *pucAdditional, rt_uint16_t usAdditionalLen)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
/* the first byte and second byte in the buffer is reserved for
|
||||
* the parameter ucSlaveID and the running flag. The rest of
|
||||
* the buffer is available for additional data. */
|
||||
if (usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF) {
|
||||
usMBSlaveIDLen = 0;
|
||||
ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
|
||||
ucMBSlaveID[usMBSlaveIDLen++] = (rt_uint8_t)(xIsRunning ? 0xFF : 0x00);
|
||||
if (usAdditionalLen > 0) {
|
||||
memcpy(&ucMBSlaveID[usMBSlaveIDLen], pucAdditional, (size_t)usAdditionalLen);
|
||||
usMBSlaveIDLen += usAdditionalLen;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_ENORES;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException eMBFuncReportSlaveID (rt_uint8_t *pucFrame, rt_uint16_t *usLen)
|
||||
{
|
||||
memcpy(&pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], (size_t)usMBSlaveIDLen);
|
||||
*usLen = (rt_uint16_t)(MB_PDU_DATA_OFF + usMBSlaveIDLen);
|
||||
|
||||
return MB_EX_NONE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
|
||||
#ifndef __MB_FUNC_H__
|
||||
#define __MB_FUNC_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
eMBException eMBFuncReadCoils (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncReadDiscreteInputs (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncReadHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncReadInputRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncWriteCoil (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncWriteHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncWriteMultipleCoils (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncWriteMultipleHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncReportSlaveID (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
eMBException eMBFuncReadWriteMultipleHoldingRegister (rt_uint8_t *pucFrame, rt_uint16_t *usLen);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
|
||||
#include "mbport.h"
|
||||
|
||||
static struct rt_semaphore lock;
|
||||
static int is_inited = 0;
|
||||
|
||||
void EnterCriticalSection(void)
|
||||
{
|
||||
rt_err_t err;
|
||||
if (!is_inited) {
|
||||
err = rt_sem_init(&lock, "mb.lock", 1, RT_IPC_FLAG_PRIO);
|
||||
if(err != RT_EOK) {
|
||||
rt_kprintf("modbus critical init failed!\r\n");
|
||||
}
|
||||
is_inited = 1;
|
||||
}
|
||||
rt_sem_take(&lock, RT_WAITING_FOREVER);
|
||||
}
|
||||
|
||||
void ExitCriticalSection(void)
|
||||
{
|
||||
rt_sem_release(&lock);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
#ifndef __MB_PORT_H__
|
||||
#define __MB_PORT_H__
|
||||
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <rtdef.h>
|
||||
#include "drv_gpio.h"
|
||||
|
||||
#include "mb.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#define RT_MODBUS_SLAVE_USE_CONTROL_PIN
|
||||
#if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
|
||||
#define MODBUS_SLAVE_RT_CONTROL_PIN_INDEX GET_PIN(A, 8)
|
||||
#endif
|
||||
|
||||
rt_uint8_t xMBPortEventInit (void);
|
||||
|
||||
rt_uint8_t xMBPortEventPost (eMBEventType eEvent);
|
||||
|
||||
rt_uint8_t xMBPortEventGet (eMBEventType * eEvent);
|
||||
|
||||
rt_uint8_t xMBPortSerialInit (rt_uint8_t ucPort, rt_uint32_t ulBaudRate,
|
||||
rt_uint8_t ucDataBits, eMBParity eParity);
|
||||
|
||||
void vMBPortClose (void);
|
||||
|
||||
void xMBPortSerialClose (void);
|
||||
|
||||
void vMBPortSerialEnable (rt_uint8_t xRxEnable, rt_uint8_t xTxEnable);
|
||||
|
||||
rt_uint8_t xMBPortSerialGetByte (char * pucByte );
|
||||
|
||||
rt_uint8_t xMBPortSerialPutByte (char ucByte );
|
||||
|
||||
rt_uint8_t xMBPortTimersInit (rt_uint16_t usTimeOut50us);
|
||||
|
||||
void xMBPortTimersClose (void);
|
||||
|
||||
void vMBPortTimersEnable (void);
|
||||
|
||||
void vMBPortTimersDisable (void);
|
||||
|
||||
|
||||
void EnterCriticalSection (void);
|
||||
void ExitCriticalSection (void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,293 +0,0 @@
|
||||
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbrtu.h"
|
||||
|
||||
#include "mbcrc.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
|
||||
#define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
|
||||
#define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
|
||||
#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
|
||||
#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
|
||||
|
||||
typedef enum {
|
||||
STATE_RX_INIT, /*!< Receiver is in initial state. */
|
||||
STATE_RX_IDLE, /*!< Receiver is in idle state. */
|
||||
STATE_RX_RCV, /*!< Frame is beeing received. */
|
||||
STATE_RX_ERROR /*!< If the frame is invalid. */
|
||||
} eMBRcvState;
|
||||
|
||||
typedef enum {
|
||||
STATE_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||
STATE_TX_XMIT /*!< Transmitter is in transfer state. */
|
||||
} eMBSndState;
|
||||
|
||||
static volatile eMBSndState eSndState;
|
||||
static volatile eMBRcvState eRcvState;
|
||||
|
||||
volatile rt_uint8_t ucRTUBuf[MB_SER_PDU_SIZE_MAX];
|
||||
|
||||
static volatile rt_uint8_t *pucSndBufferCur;
|
||||
static volatile rt_uint16_t usSndBufferCount;
|
||||
|
||||
static volatile rt_uint16_t usRcvBufferPos;
|
||||
|
||||
eMBErrorCode eMBRTUInit (rt_uint8_t ucSlaveAddress, rt_uint8_t ucPort,
|
||||
rt_uint32_t ulBaudRate, eMBParity eParity)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint32_t usTimerT35_50us;
|
||||
|
||||
(void)ucSlaveAddress;
|
||||
EnterCriticalSection();
|
||||
|
||||
/* Modbus RTU uses 8 Databits. */
|
||||
/* If baudrate > 19200 then we should use the fixed timer values
|
||||
* t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
|
||||
*/
|
||||
if (ulBaudRate > 19200) {
|
||||
usTimerT35_50us = 35; /* 1800us. */
|
||||
} else {
|
||||
/* The timer reload value for a character is given by:
|
||||
*
|
||||
* ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
|
||||
* = 11 * Ticks_per_1s / Baudrate
|
||||
* = 220000 / Baudrate
|
||||
* The reload for t3.5 is 1.5 times this value and similary
|
||||
* for t3.5.
|
||||
*/
|
||||
usTimerT35_50us = (7UL * 220000UL) / (2UL * ulBaudRate);
|
||||
}
|
||||
|
||||
if (xMBPortTimersInit((rt_uint16_t)usTimerT35_50us) != TRUE) {
|
||||
eStatus = MB_EPORTERR;
|
||||
} else if (xMBPortSerialInit(ucPort, ulBaudRate, 8, eParity) != TRUE) {
|
||||
eStatus = MB_EPORTERR;
|
||||
}
|
||||
|
||||
ExitCriticalSection();
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
void eMBRTUStart (void)
|
||||
{
|
||||
EnterCriticalSection();
|
||||
/* Initially the receiver is in the state STATE_RX_INIT. we start
|
||||
* the timer and if no character is received within t3.5 we change
|
||||
* to STATE_RX_IDLE. This makes sure that we delay startup of the
|
||||
* modbus protocol stack until the bus is free.
|
||||
*/
|
||||
eRcvState = STATE_RX_INIT;
|
||||
vMBPortSerialEnable(TRUE, FALSE);
|
||||
vMBPortTimersEnable();
|
||||
|
||||
ExitCriticalSection();
|
||||
}
|
||||
|
||||
void eMBRTUStop (void)
|
||||
{
|
||||
EnterCriticalSection();
|
||||
vMBPortSerialEnable(FALSE, FALSE);
|
||||
vMBPortTimersDisable();
|
||||
ExitCriticalSection();
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRTUReceive( rt_uint8_t * pucRcvAddress, rt_uint8_t ** pucFrame, rt_uint16_t * pusLength )
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
|
||||
EnterCriticalSection();
|
||||
|
||||
RT_ASSERT( usRcvBufferPos <= MB_SER_PDU_SIZE_MAX );
|
||||
|
||||
/* Length and CRC check */
|
||||
if ((usRcvBufferPos >= MB_SER_PDU_SIZE_MIN)
|
||||
&& (usMBCRC16((rt_uint8_t *)ucRTUBuf, usRcvBufferPos) == 0)) {
|
||||
/* Save the address field. All frames are passed to the upper layed
|
||||
* and the decision if a frame is used is done there.
|
||||
*/
|
||||
*pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
|
||||
|
||||
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||
* size of address field and CRC checksum.
|
||||
*/
|
||||
*pusLength = (rt_uint16_t)(usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC);
|
||||
|
||||
/* Return the start of the Modbus PDU to the caller. */
|
||||
*pucFrame = (rt_uint8_t *)&ucRTUBuf[MB_SER_PDU_PDU_OFF];
|
||||
} else {
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
ExitCriticalSection();
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBErrorCode eMBRTUSend (rt_uint8_t ucSlaveAddress, const rt_uint8_t * pucFrame, rt_uint16_t usLength)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t usCRC16;
|
||||
|
||||
EnterCriticalSection();
|
||||
|
||||
/* 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 network. We have to abort sending the frame.
|
||||
*/
|
||||
if (eRcvState == STATE_RX_IDLE) {
|
||||
/* First byte before the Modbus-PDU is the slave address. */
|
||||
pucSndBufferCur = (rt_uint8_t *) pucFrame - 1;
|
||||
usSndBufferCount = 1;
|
||||
|
||||
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||
usSndBufferCount += usLength;
|
||||
|
||||
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||
usCRC16 = usMBCRC16((rt_uint8_t *)pucSndBufferCur, usSndBufferCount);
|
||||
ucRTUBuf[usSndBufferCount++] = (rt_uint8_t)(usCRC16 & 0xFF);
|
||||
ucRTUBuf[usSndBufferCount++] = (rt_uint8_t)(usCRC16 >> 8);
|
||||
|
||||
/* Activate the transmitter. */
|
||||
eSndState = STATE_TX_XMIT;
|
||||
vMBPortSerialEnable(FALSE, TRUE);
|
||||
} else {
|
||||
eStatus = MB_EIO;
|
||||
}
|
||||
|
||||
ExitCriticalSection();
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBRTUReceiveFSM (void)
|
||||
{
|
||||
rt_uint8_t xTaskNeedSwitch = FALSE;
|
||||
rt_uint8_t ucByte;
|
||||
|
||||
RT_ASSERT(eSndState == STATE_TX_IDLE);
|
||||
|
||||
/* Always read the character. */
|
||||
(void)xMBPortSerialGetByte((char *)&ucByte);
|
||||
|
||||
rt_kprintf("%02X ", ucByte);
|
||||
|
||||
switch (eRcvState) {
|
||||
/* If we have received a character in the init state we have to
|
||||
* wait until the frame is finished.
|
||||
*/
|
||||
case STATE_RX_INIT:
|
||||
vMBPortTimersEnable();
|
||||
break;
|
||||
|
||||
/* In the error state we wait until all characters in the
|
||||
* damaged frame are transmitted.
|
||||
*/
|
||||
case STATE_RX_ERROR:
|
||||
vMBPortTimersEnable();
|
||||
break;
|
||||
|
||||
/* In the idle state we wait for a new character. If a character
|
||||
* is received the t1.5 and t3.5 timers are started and the
|
||||
* receiver is in the state STATE_RX_RECEIVCE.
|
||||
*/
|
||||
case STATE_RX_IDLE:
|
||||
usRcvBufferPos = 0;
|
||||
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||
eRcvState = STATE_RX_RCV;
|
||||
|
||||
/* Enable t3.5 timers. */
|
||||
vMBPortTimersEnable();
|
||||
break;
|
||||
|
||||
/* We are currently receiving a frame. Reset the timer after
|
||||
* every character received. If more than the maximum possible
|
||||
* number of bytes in a modbus frame is received the frame is
|
||||
* ignored.
|
||||
*/
|
||||
case STATE_RX_RCV:
|
||||
if (usRcvBufferPos < MB_SER_PDU_SIZE_MAX) {
|
||||
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||
} else {
|
||||
eRcvState = STATE_RX_ERROR;
|
||||
}
|
||||
vMBPortTimersEnable();
|
||||
break;
|
||||
}
|
||||
return xTaskNeedSwitch;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBRTUTransmitFSM (void)
|
||||
{
|
||||
rt_uint8_t xNeedPoll = FALSE;
|
||||
|
||||
RT_ASSERT(eRcvState == STATE_RX_IDLE);
|
||||
|
||||
switch (eSndState) {
|
||||
/* We should not get a transmitter event if the transmitter is in
|
||||
* idle state. */
|
||||
case STATE_TX_IDLE:
|
||||
/* enable receiver/disable transmitter. */
|
||||
vMBPortSerialEnable(TRUE, FALSE);
|
||||
break;
|
||||
|
||||
case STATE_TX_XMIT:
|
||||
/* check if we are finished. */
|
||||
if (usSndBufferCount != 0) {
|
||||
xMBPortSerialPutByte((char)*pucSndBufferCur);
|
||||
pucSndBufferCur++; /* next byte in sendbuffer. */
|
||||
usSndBufferCount--;
|
||||
} else {
|
||||
xNeedPoll = xMBPortEventPost(EV_FRAME_SENT);
|
||||
/* Disable transmitter. This prevents another transmit buffer
|
||||
* empty interrupt. */
|
||||
eSndState = STATE_TX_IDLE;
|
||||
vMBPortSerialEnable(TRUE, FALSE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBRTUTimerT35Expired (void)
|
||||
{
|
||||
rt_uint8_t xNeedPoll = FALSE;
|
||||
|
||||
switch (eRcvState) {
|
||||
/* Timer t35 expired. Startup phase is finished. */
|
||||
case STATE_RX_INIT:
|
||||
xNeedPoll = xMBPortEventPost(EV_READY);
|
||||
break;
|
||||
|
||||
/* A frame was received and t35 expired. Notify the listener that
|
||||
* a new frame was received. */
|
||||
case STATE_RX_RCV:
|
||||
xNeedPoll = xMBPortEventPost(EV_FRAME_RECEIVED);
|
||||
break;
|
||||
|
||||
/* An error occured while receiving the frame. */
|
||||
case STATE_RX_ERROR:
|
||||
break;
|
||||
|
||||
/* Function called in an illegal state. */
|
||||
default:
|
||||
RT_ASSERT((eRcvState == STATE_RX_INIT) ||
|
||||
(eRcvState == STATE_RX_RCV) || (eRcvState == STATE_RX_ERROR));
|
||||
break;
|
||||
}
|
||||
|
||||
vMBPortTimersDisable();
|
||||
eRcvState = STATE_RX_IDLE;
|
||||
|
||||
return xNeedPoll;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef __MB_RTU_H__
|
||||
#define __MB_RTU_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
eMBErrorCode eMBRTUInit (rt_uint8_t slaveAddress, rt_uint8_t ucPort,
|
||||
rt_uint32_t ulBaudRate, eMBParity eParity);
|
||||
void eMBRTUStart (void);
|
||||
void eMBRTUStop (void);
|
||||
eMBErrorCode eMBRTUReceive (rt_uint8_t * pucRcvAddress, rt_uint8_t ** pucFrame, rt_uint16_t * pusLength);
|
||||
eMBErrorCode eMBRTUSend (rt_uint8_t slaveAddress, const rt_uint8_t * pucFrame, rt_uint16_t usLength);
|
||||
rt_uint8_t xMBRTUReceiveFSM (void);
|
||||
rt_uint8_t xMBRTUTransmitFSM (void);
|
||||
|
||||
rt_uint8_t xMBRTUTimerT35Expired (void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "mbrtu.h"
|
||||
|
||||
/* software simulation serial transmit IRQ handler thread stack */
|
||||
//rt_align(RT_ALIGN_SIZE)
|
||||
|
||||
static rt_uint8_t serial_soft_trans_irq_stack[512];
|
||||
|
||||
/* software simulation serial transmit IRQ handler thread */
|
||||
static struct rt_thread thread_serial_soft_trans_irq;
|
||||
|
||||
/* serial event */
|
||||
static struct rt_event event_serial;
|
||||
|
||||
/* modbus slave serial device */
|
||||
static struct rt_serial_device *serial;
|
||||
|
||||
/* serial transmit event */
|
||||
#define EVENT_SERIAL_TRANS_START (1<<0)
|
||||
|
||||
static void serial_soft_trans_irq(void* parameter);
|
||||
|
||||
/**
|
||||
* This function is serial receive callback function
|
||||
*
|
||||
* @param dev the device of serial
|
||||
* @param size the data size that receive
|
||||
*
|
||||
* @return return RT_EOK
|
||||
*/
|
||||
static rt_err_t serial_rx_ind (rt_device_t dev, rt_size_t size)
|
||||
{
|
||||
while (size--) {
|
||||
xMBRTUReceiveFSM();
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Software simulation serial transmit IRQ handler.
|
||||
*
|
||||
* @param parameter parameter
|
||||
*/
|
||||
static void serial_soft_trans_irq (void* parameter)
|
||||
{
|
||||
rt_uint32_t recved_event;
|
||||
|
||||
while (1) {
|
||||
/* waiting for serial transmit start */
|
||||
rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START, RT_EVENT_FLAG_OR,
|
||||
RT_WAITING_FOREVER, &recved_event);
|
||||
|
||||
xMBRTUTransmitFSM();
|
||||
}
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortSerialInit (rt_uint8_t ucPORT, rt_uint32_t ulBaudRate,
|
||||
rt_uint8_t ucDataBits, eMBParity eParity)
|
||||
{
|
||||
rt_device_t dev = RT_NULL;
|
||||
char uart_name[20];
|
||||
|
||||
/**
|
||||
* set 485 mode receive and transmit control IO
|
||||
* @note MODBUS_SLAVE_RT_CONTROL_PIN_INDEX need be defined by user
|
||||
*/
|
||||
#if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
|
||||
rt_pin_mode(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_MODE_OUTPUT);
|
||||
#endif
|
||||
|
||||
/* set serial name */
|
||||
rt_snprintf(uart_name,sizeof(uart_name), "uart%d", ucPORT);
|
||||
|
||||
dev = rt_device_find(uart_name);
|
||||
if(dev == RT_NULL) {
|
||||
/* can not find uart */
|
||||
return FALSE;
|
||||
} else {
|
||||
serial = (struct rt_serial_device*)dev;
|
||||
}
|
||||
|
||||
/* set serial configure parameter */
|
||||
serial->config.baud_rate = ulBaudRate;
|
||||
serial->config.stop_bits = STOP_BITS_1;
|
||||
|
||||
switch (eParity) {
|
||||
case MB_PAR_NONE: {
|
||||
serial->config.data_bits = DATA_BITS_8;
|
||||
serial->config.parity = PARITY_NONE;
|
||||
}
|
||||
break;
|
||||
|
||||
case MB_PAR_ODD: {
|
||||
serial->config.data_bits = DATA_BITS_9;
|
||||
serial->config.parity = PARITY_ODD;
|
||||
}
|
||||
break;
|
||||
|
||||
case MB_PAR_EVEN: {
|
||||
serial->config.data_bits = DATA_BITS_9;
|
||||
serial->config.parity = PARITY_EVEN;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* set serial configure */
|
||||
serial->ops->configure(serial, &serial->config);
|
||||
|
||||
/* open serial device */
|
||||
if (!rt_device_open(&serial->parent, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX)) {
|
||||
rt_device_set_rx_indicate(&serial->parent, serial_rx_ind);
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* software initialize */
|
||||
rt_event_init(&event_serial, "trans", RT_IPC_FLAG_PRIO);
|
||||
rt_thread_init(&thread_serial_soft_trans_irq,
|
||||
"thr.trans",
|
||||
serial_soft_trans_irq,
|
||||
RT_NULL,
|
||||
serial_soft_trans_irq_stack,
|
||||
sizeof(serial_soft_trans_irq_stack),
|
||||
10, 5);
|
||||
rt_thread_startup(&thread_serial_soft_trans_irq);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vMBPortSerialEnable (rt_uint8_t xRxEnable, rt_uint8_t xTxEnable)
|
||||
{
|
||||
rt_uint32_t recved_event;
|
||||
|
||||
if (xRxEnable) {
|
||||
/* enable RX interrupt */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
/* switch 485 to receive mode */
|
||||
#if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
|
||||
rt_pin_write(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_LOW);
|
||||
#endif
|
||||
} else {
|
||||
/* switch 485 to transmit mode */
|
||||
#if defined(RT_MODBUS_SLAVE_USE_CONTROL_PIN)
|
||||
rt_pin_write(MODBUS_SLAVE_RT_CONTROL_PIN_INDEX, PIN_HIGH);
|
||||
#endif
|
||||
|
||||
/* disable RX interrupt */
|
||||
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||
}
|
||||
|
||||
if (xTxEnable) {
|
||||
/* start serial transmit */
|
||||
rt_event_send(&event_serial, EVENT_SERIAL_TRANS_START);
|
||||
} else {
|
||||
/* stop serial transmit */
|
||||
rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START,
|
||||
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 0,
|
||||
&recved_event);
|
||||
}
|
||||
}
|
||||
|
||||
void vMBPortClose (void)
|
||||
{
|
||||
serial->parent.close(&serial->parent);
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortSerialPutByte (char ucByte)
|
||||
{
|
||||
serial->parent.write(&serial->parent, 0, &ucByte, 1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortSerialGetByte (char * pucByte)
|
||||
{
|
||||
serial->parent.read(&serial->parent, 0, pucByte, 1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbport.h"
|
||||
#include "mbrtu.h"
|
||||
|
||||
static struct rt_timer timer;
|
||||
|
||||
void vMBPortTimersEnable (void)
|
||||
{
|
||||
rt_timer_start(&timer);
|
||||
}
|
||||
|
||||
void vMBPortTimersDisable (void)
|
||||
{
|
||||
rt_timer_stop(&timer);
|
||||
}
|
||||
|
||||
static void timer_timeout_ind (void* parameter)
|
||||
{
|
||||
xMBRTUTimerT35Expired();
|
||||
}
|
||||
|
||||
rt_uint8_t xMBPortTimersInit (rt_uint16_t usTim1Timerout50us)
|
||||
{
|
||||
rt_timer_init(&timer, "tmr.mb",
|
||||
timer_timeout_ind, /* bind timeout callback function */
|
||||
RT_NULL,
|
||||
(50 * usTim1Timerout50us) / (1000 * 1000 / RT_TICK_PER_SECOND) + 1,
|
||||
RT_TIMER_FLAG_ONE_SHOT); /* one shot */
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mbport.h"
|
||||
|
||||
#include "mb.h"
|
||||
|
||||
#define BITS_UCHAR 8U
|
||||
|
||||
void xMBUtilSetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset,
|
||||
rt_uint8_t ucNBits, rt_uint8_t ucValue)
|
||||
{
|
||||
rt_uint16_t usWordBuf;
|
||||
rt_uint16_t usMask;
|
||||
rt_uint16_t usByteOffset;
|
||||
rt_uint16_t usNPreBits;
|
||||
rt_uint16_t usValue = ucValue;
|
||||
|
||||
RT_ASSERT(ucNBits <= 8);
|
||||
RT_ASSERT((size_t)BITS_UCHAR == sizeof(rt_uint8_t) * 8);
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = (rt_uint16_t)((usBitOffset) / BITS_UCHAR);
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = (rt_uint16_t)(usBitOffset - usByteOffset * BITS_UCHAR);
|
||||
|
||||
/* Move bit field into position over bits to set */
|
||||
usValue <<= usNPreBits;
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = (rt_uint16_t)((1 << (rt_uint16_t)ucNBits) - 1);
|
||||
usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* Zero out bit field bits and then or value bits into them. */
|
||||
usWordBuf = (rt_uint16_t)((usWordBuf & (~usMask)) | usValue);
|
||||
|
||||
/* move bits back into storage */
|
||||
ucByteBuf[usByteOffset] = (rt_uint8_t)(usWordBuf & 0xFF);
|
||||
ucByteBuf[usByteOffset + 1] = (rt_uint8_t)(usWordBuf >> BITS_UCHAR);
|
||||
}
|
||||
|
||||
rt_uint8_t xMBUtilGetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_uint8_t ucNBits)
|
||||
{
|
||||
rt_uint16_t usWordBuf;
|
||||
rt_uint16_t usMask;
|
||||
rt_uint16_t usByteOffset;
|
||||
rt_uint16_t usNPreBits;
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = (rt_uint16_t)((usBitOffset) / BITS_UCHAR);
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = (rt_uint16_t)(usBitOffset - usByteOffset * BITS_UCHAR);
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = (rt_uint16_t)((1 << (rt_uint16_t)ucNBits) - 1);
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* throw away unneeded bits. */
|
||||
usWordBuf >>= usNPreBits;
|
||||
|
||||
/* mask away bits above the requested bitfield. */
|
||||
usWordBuf &= usMask;
|
||||
|
||||
return (rt_uint8_t)usWordBuf;
|
||||
}
|
||||
|
||||
eMBException prveMBError2Exception (eMBErrorCode eErrorCode)
|
||||
{
|
||||
eMBException eStatus;
|
||||
|
||||
switch (eErrorCode) {
|
||||
case MB_ENOERR:
|
||||
eStatus = MB_EX_NONE;
|
||||
break;
|
||||
|
||||
case MB_ENOREG:
|
||||
eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
|
||||
break;
|
||||
|
||||
case MB_ETIMEDOUT:
|
||||
eStatus = MB_EX_SLAVE_BUSY;
|
||||
break;
|
||||
|
||||
default:
|
||||
eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
|
||||
break;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
|
||||
#ifndef __MB_UTILS_H__
|
||||
#define __MB_UTILS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*! \brief Function to set bits in a byte buffer.
|
||||
*
|
||||
* This function allows the efficient use of an array to implement bitfields.
|
||||
* The array used for storing the bits must always be a multiple of two
|
||||
* bytes. Up to eight bits can be set or cleared in one operation.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored. Must be a
|
||||
* multiple of 2 bytes. No length checking is performed and if
|
||||
* usBitOffset / 8 is greater than the size of the buffer memory contents
|
||||
* is overwritten.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
* \param ucValues Thew new values for the bits. The value for the first bit
|
||||
* starting at <code>usBitOffset</code> is the LSB of the value
|
||||
* <code>ucValues</code>
|
||||
*
|
||||
* \code
|
||||
* ucBits[2] = {0, 0};
|
||||
*
|
||||
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
|
||||
* xMBUtilSetBits( ucBits, 4, 1, 1 );
|
||||
*
|
||||
* // Set bit 7 to 1 and bit 8 to 0.
|
||||
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
|
||||
*
|
||||
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
|
||||
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||
* \endcode
|
||||
*/
|
||||
void xMBUtilSetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset,
|
||||
rt_uint8_t ucNBits, rt_uint8_t ucValues);
|
||||
|
||||
/*! \brief Function to read bits in a byte buffer.
|
||||
*
|
||||
* This function is used to extract up bit values from an array. Up to eight
|
||||
* bit values can be extracted in one step.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
*
|
||||
* \code
|
||||
* rt_uint8_t ucBits[2] = {0, 0};
|
||||
* rt_uint8_t ucResult;
|
||||
*
|
||||
* // Extract the bits 3 - 10.
|
||||
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||
* \endcode
|
||||
*/
|
||||
rt_uint8_t xMBUtilGetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_uint8_t ucNBits);
|
||||
|
||||
/*! @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,33 +1,186 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
#include "mbutils.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#include "chrg_comm.h"
|
||||
#include "chrg_utils.h"
|
||||
|
||||
|
||||
#define LOG_TAG "chrg.comm"
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
//Slave mode:DiscreteInputs variables
|
||||
rt_uint16_t usSDiscInStart = S_DISCRETE_INPUT_START;
|
||||
rt_uint8_t ucSDiscInBuf[(S_DISCRETE_INPUT_NDISCRETES+7)/8];
|
||||
|
||||
//Slave mode:Coils variables
|
||||
rt_uint16_t usSCoilStart = S_COIL_START;
|
||||
rt_uint8_t ucSCoilBuf[(S_COIL_NCOILS+7)/8];
|
||||
|
||||
//Slave mode:InputRegister variables
|
||||
rt_uint16_t usSRegInStart = S_REG_INPUT_START;
|
||||
rt_uint16_t usSRegInBuf[S_REG_INPUT_NREGS];
|
||||
|
||||
//Slave mode:HoldingRegister variables
|
||||
rt_uint16_t usSRegHoldStart = S_REG_HOLDING_START;
|
||||
rt_uint16_t usSRegHoldBuf[S_REG_HOLDING_NREGS];
|
||||
|
||||
#define BITS_UCHAR 8U
|
||||
|
||||
struct chrg_comm_t chrgcomm = {
|
||||
.name = THR_NAME_COMM,
|
||||
.devname = DEV_NAME_COMM,
|
||||
.tid = RT_NULL,
|
||||
.tty = RT_NULL,
|
||||
.rx_len = 0,
|
||||
.tx_len = 0,
|
||||
.addr = MODBUS_ADDRESS_DEFAULT,
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*! \brief Function to set bits in a byte buffer.
|
||||
*
|
||||
* This function allows the efficient use of an array to implement bitfields.
|
||||
* The array used for storing the bits must always be a multiple of two
|
||||
* bytes. Up to eight bits can be set or cleared in one operation.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored. Must be a
|
||||
* multiple of 2 bytes. No length checking is performed and if
|
||||
* usBitOffset / 8 is greater than the size of the buffer memory contents
|
||||
* is overwritten.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
* \param ucValues Thew new values for the bits. The value for the first bit
|
||||
* starting at <code>usBitOffset</code> is the LSB of the value
|
||||
* <code>ucValues</code>
|
||||
*
|
||||
* \code
|
||||
* ucBits[2] = {0, 0};
|
||||
*
|
||||
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
|
||||
* xMBUtilSetBits( ucBits, 4, 1, 1 );
|
||||
*
|
||||
* // Set bit 7 to 1 and bit 8 to 0.
|
||||
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
|
||||
*
|
||||
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
|
||||
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||
* \endcode
|
||||
*/
|
||||
void xMBUtilSetBits (rt_uint8_t *ucByteBuf, rt_uint16_t usBitOffset,
|
||||
rt_uint8_t ucNBits, rt_uint8_t ucValue)
|
||||
{
|
||||
rt_uint16_t usWordBuf;
|
||||
rt_uint16_t usMask;
|
||||
rt_uint16_t usByteOffset;
|
||||
rt_uint16_t usNPreBits;
|
||||
rt_uint16_t usValue = ucValue;
|
||||
|
||||
RT_ASSERT(ucNBits <= 8);
|
||||
RT_ASSERT((size_t)BITS_UCHAR == sizeof(rt_uint8_t) * 8);
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = (rt_uint16_t)((usBitOffset) / BITS_UCHAR);
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = (rt_uint16_t)(usBitOffset - usByteOffset * BITS_UCHAR);
|
||||
|
||||
/* Move bit field into position over bits to set */
|
||||
usValue <<= usNPreBits;
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = (rt_uint16_t)((1 << (rt_uint16_t)ucNBits) - 1);
|
||||
usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* Zero out bit field bits and then or value bits into them. */
|
||||
usWordBuf = (rt_uint16_t)((usWordBuf & (~usMask)) | usValue);
|
||||
|
||||
/* move bits back into storage */
|
||||
ucByteBuf[usByteOffset] = (rt_uint8_t)(usWordBuf & 0xFF);
|
||||
ucByteBuf[usByteOffset + 1] = (rt_uint8_t)(usWordBuf >> BITS_UCHAR);
|
||||
}
|
||||
|
||||
/*! \brief Function to read bits in a byte buffer.
|
||||
*
|
||||
* This function is used to extract up bit values from an array. Up to eight
|
||||
* bit values can be extracted in one step.
|
||||
*
|
||||
* \param ucByteBuf A buffer where the bit values are stored.
|
||||
* \param usBitOffset The starting address of the bits to set. The first
|
||||
* bit has the offset 0.
|
||||
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||
* than 8.
|
||||
*
|
||||
* \code
|
||||
* rt_uint8_t ucBits[2] = {0, 0};
|
||||
* rt_uint8_t ucResult;
|
||||
*
|
||||
* // Extract the bits 3 - 10.
|
||||
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||
* \endcode
|
||||
*/
|
||||
rt_uint8_t xMBUtilGetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_uint8_t ucNBits)
|
||||
{
|
||||
rt_uint16_t usWordBuf;
|
||||
rt_uint16_t usMask;
|
||||
rt_uint16_t usByteOffset;
|
||||
rt_uint16_t usNPreBits;
|
||||
|
||||
/* Calculate byte offset for first byte containing the bit values starting
|
||||
* at usBitOffset. */
|
||||
usByteOffset = (rt_uint16_t)((usBitOffset) / BITS_UCHAR);
|
||||
|
||||
/* How many bits precede our bits to set. */
|
||||
usNPreBits = (rt_uint16_t)(usBitOffset - usByteOffset * BITS_UCHAR);
|
||||
|
||||
/* Prepare a mask for setting the new bits. */
|
||||
usMask = (rt_uint16_t)((1 << (rt_uint16_t)ucNBits) - 1);
|
||||
|
||||
/* copy bits into temporary storage. */
|
||||
usWordBuf = ucByteBuf[usByteOffset];
|
||||
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||
|
||||
/* throw away unneeded bits. */
|
||||
usWordBuf >>= usNPreBits;
|
||||
|
||||
/* mask away bits above the requested bitfield. */
|
||||
usWordBuf &= usMask;
|
||||
|
||||
return (rt_uint8_t)usWordBuf;
|
||||
}
|
||||
|
||||
static eMBException prveMBError2Exception (eMBErrorCode eErrorCode)
|
||||
{
|
||||
eMBException eStatus;
|
||||
|
||||
switch (eErrorCode) {
|
||||
case MB_ENOERR:
|
||||
eStatus = MB_EX_NONE;
|
||||
break;
|
||||
|
||||
case MB_ENOREG:
|
||||
eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
|
||||
break;
|
||||
|
||||
case MB_ETIMEDOUT:
|
||||
eStatus = MB_EX_SLAVE_BUSY;
|
||||
break;
|
||||
|
||||
default:
|
||||
eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
|
||||
break;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Modbus slave input register callback function.
|
||||
*
|
||||
@@ -46,9 +199,6 @@ eMBErrorCode eMBRegInputCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_
|
||||
rt_uint16_t REG_INPUT_NREGS = S_REG_INPUT_NREGS;
|
||||
rt_uint16_t usRegInStart = usSRegInStart;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= REG_INPUT_START)
|
||||
&& (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS)) {
|
||||
iRegIndex = usAddress - usRegInStart;
|
||||
@@ -75,7 +225,7 @@ eMBErrorCode eMBRegInputCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegHoldingCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
eMBErrorCode eMBRegHoldingCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNRegs, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
@@ -85,9 +235,6 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t REG_HOLDING_NREGS = S_REG_HOLDING_NREGS;
|
||||
rt_uint16_t usRegHoldStart = usSRegHoldStart;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= REG_HOLDING_START)
|
||||
&& (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS)) {
|
||||
iRegIndex = usAddress - usRegHoldStart;
|
||||
@@ -105,8 +252,8 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
/* write current register values with new values from the protocol stack. */
|
||||
case MB_REG_WRITE:
|
||||
while (usNRegs > 0) {
|
||||
pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
||||
pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
||||
pusRegHoldingBuf[iRegIndex] = *req++ << 8;
|
||||
pusRegHoldingBuf[iRegIndex] |= *req++;
|
||||
iRegIndex++;
|
||||
usNRegs--;
|
||||
}
|
||||
@@ -129,23 +276,19 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegCoilsCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
eMBErrorCode eMBRegCoilsCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
|
||||
rt_uint16_t iRegIndex = 0, iRegBitIndex = 0, iNReg = 0;
|
||||
rt_uint8_t *pucCoilBuf = ucSCoilBuf;
|
||||
rt_uint16_t COIL_START = S_COIL_START;
|
||||
rt_uint16_t COIL_NCOILS = S_COIL_NCOILS;
|
||||
rt_uint16_t usCoilStart = usSCoilStart;
|
||||
|
||||
iNReg = usNCoils / 8 + 1;
|
||||
iNReg = (usNCoils+7)/8;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= COIL_START) &&
|
||||
(usAddress + usNCoils <= COIL_START + COIL_NCOILS)) {
|
||||
if ((usAddress >= COIL_START) && (usAddress + usNCoils <= COIL_START + COIL_NCOILS)) {
|
||||
iRegIndex = (rt_uint16_t)(usAddress - usCoilStart) / 8;
|
||||
iRegBitIndex = (rt_uint16_t)(usAddress - usCoilStart) % 8;
|
||||
switch (eMode) {
|
||||
@@ -166,14 +309,14 @@ eMBErrorCode eMBRegCoilsCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
/* write current coil values with new values from the protocol stack. */
|
||||
case MB_REG_WRITE:
|
||||
while (iNReg > 1) {
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8, *pucRegBuffer++);
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8, *req++);
|
||||
iNReg--;
|
||||
}
|
||||
/* last coils */
|
||||
usNCoils = usNCoils % 8;
|
||||
/* xMBUtilSetBits has bug when ucNBits is zero */
|
||||
if (usNCoils != 0) {
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils, *pucRegBuffer++);
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils, *req++);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -203,9 +346,6 @@ eMBErrorCode eMBRegDiscreteCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usDiscreteInputStart = usSDiscInStart;
|
||||
iNReg = usNDiscrete / 8 + 1;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= DISCRETE_INPUT_START)
|
||||
&& ((usAddress + usNDiscrete) <= (DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES))) {
|
||||
iRegIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) / 8;
|
||||
@@ -228,63 +368,408 @@ eMBErrorCode eMBRegDiscreteCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
struct chrg_comm_t chrgcomm = {
|
||||
.tid = RT_NULL,
|
||||
.name = THR_NAME_POLL,
|
||||
.addr = MODBUS_ADDRESS_DEFAULT,
|
||||
|
||||
eMBException funcReadCoils (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntCoils = 0;
|
||||
rt_uint8_t ucNBytes = 0;
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_READ_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntCoils = u8v_to_u16(&reqFrame[4]);
|
||||
|
||||
if ((0 == cntCoils)||(cntCoils >= MB_PDU_FUNC_READ_COILCNT_MAX)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
ucNBytes = (cntCoils+7)/8;
|
||||
|
||||
resFrame[1] = MB_READ_COILS;
|
||||
resFrame[2] = ucNBytes;
|
||||
eRegStatus = eMBRegCoilsCB(RT_NULL, &resFrame[3], regAddress, cntCoils, MB_REG_READ);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*resLen = 3+ucNBytes;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcReadDiscreteInputs (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntDisCrete = 0;
|
||||
rt_uint8_t ucNBytes = 0;
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_READ_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntDisCrete = u8v_to_u16(&reqFrame[4]);
|
||||
|
||||
if ((0 == cntDisCrete)||(cntDisCrete >= MB_PDU_FUNC_READ_DISCCNT_MAX)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
ucNBytes = (cntDisCrete+7)/8;
|
||||
|
||||
resFrame[1] = MB_READ_DISCRETE_INPUTS;
|
||||
resFrame[2] = ucNBytes;
|
||||
eRegStatus = eMBRegDiscreteCB(&resFrame[3], regAddress, cntDisCrete);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*resLen = 3+ucNBytes;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcReadHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntReg = 0;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_READ_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntReg = u8v_to_u16(&reqFrame[4]);
|
||||
|
||||
if ((0 == cntReg)||(cntReg >= MB_PDU_FUNC_READ_REGCNT_MAX)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
resFrame[1] = MB_READ_HOLDING_REGISTER;
|
||||
resFrame[2] = cntReg * 2;
|
||||
|
||||
eRegStatus = eMBRegHoldingCB(RT_NULL, &resFrame[3], regAddress, cntReg, MB_REG_READ);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*resLen = 3+cntReg * 2;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcReadInputRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntReg = 0;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_READ_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntReg = u8v_to_u16(&reqFrame[4]);
|
||||
|
||||
if ((0 == cntReg)||(cntReg >= MB_PDU_FUNC_READ_REGCNT_MAX)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
resFrame[1] = MB_READ_INPUT_REGISTER;
|
||||
resFrame[2] = cntReg * 2;
|
||||
|
||||
eRegStatus = eMBRegDiscreteCB(&resFrame[3], regAddress, cntReg);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
} else {
|
||||
*resLen = 3+cntReg*2;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcWriteCoil (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, value = 0;
|
||||
rt_uint8_t ucBuf[2] = {0};
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_WRITE_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
value = u8v_to_u16(&reqFrame[4]);
|
||||
|
||||
if (0x0000 == value) {
|
||||
ucBuf[0] = 0;
|
||||
ucBuf[1] = 0;
|
||||
} else if (0xFF00 == value) {
|
||||
ucBuf[0] = 1;
|
||||
ucBuf[1] = 0;
|
||||
} else {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
memcpy(resFrame, reqFrame, reqLen);
|
||||
*resLen = reqLen-2;
|
||||
|
||||
eRegStatus = eMBRegCoilsCB(RT_NULL, ucBuf, regAddress, 1, MB_REG_WRITE);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcWriteHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_WRITE_SIZE != reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
|
||||
memcpy(resFrame, reqFrame, reqLen);
|
||||
*resLen = reqLen-2;
|
||||
|
||||
eRegStatus = eMBRegHoldingCB(&reqFrame[4], &resFrame[3], regAddress, 1, MB_REG_WRITE);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcWriteMultipleCoils (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntCoils = 0;
|
||||
rt_uint8_t ucByteCount;
|
||||
rt_uint8_t ucByteCountVerify;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_WRITE_SIZE > reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntCoils = u8v_to_u16(&reqFrame[4]);
|
||||
ucByteCount = reqFrame[6];
|
||||
ucByteCountVerify = (cntCoils+7)/8;
|
||||
|
||||
if ((0 == cntCoils)
|
||||
||(cntCoils > MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX)
|
||||
||(ucByteCountVerify != ucByteCount)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
memcpy(resFrame, reqFrame, reqLen);
|
||||
*resLen = reqLen-2;
|
||||
|
||||
eRegStatus = eMBRegCoilsCB(reqFrame, &resFrame[3], regAddress, cntCoils, MB_REG_WRITE);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
eMBException funcWriteMultipleHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen)
|
||||
{
|
||||
rt_uint16_t regAddress = 0, cntReg = 0;
|
||||
rt_uint8_t ucRegByteCount;
|
||||
|
||||
eMBException eStatus = MB_EX_NONE;
|
||||
eMBErrorCode eRegStatus;
|
||||
|
||||
if (4+MB_PDU_FUNC_WRITE_MUL_SIZE_MIN > reqLen) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
regAddress = u8v_to_u16(&reqFrame[2]);
|
||||
cntReg = u8v_to_u16(&reqFrame[4]);
|
||||
ucRegByteCount = reqFrame[6];
|
||||
|
||||
if ((0 == cntReg)
|
||||
||(cntReg > MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX)
|
||||
||(ucRegByteCount != cntReg*2)) {
|
||||
return MB_EX_ILLEGAL_DATA_VALUE;
|
||||
}
|
||||
|
||||
memcpy(resFrame, reqFrame, reqLen);
|
||||
*resLen = reqLen-2;
|
||||
|
||||
eRegStatus = eMBRegHoldingCB(reqFrame, &resFrame[3], regAddress, cntReg, MB_REG_WRITE);
|
||||
if (eRegStatus != MB_ENOERR) {
|
||||
eStatus = prveMBError2Exception(eRegStatus);
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct code_func_t funcHandlers[MB_HANDLERS_MAX] = {
|
||||
{MB_READ_COILS, funcReadCoils},
|
||||
{MB_READ_DISCRETE_INPUTS, funcReadDiscreteInputs},
|
||||
{MB_READ_HOLDING_REGISTER, funcReadHoldingRegister},
|
||||
{MB_READ_INPUT_REGISTER, funcReadInputRegister},
|
||||
{MB_WRITE_SINGLE_COIL, funcWriteCoil},
|
||||
{MB_WRITE_REGISTER, funcWriteHoldingRegister},
|
||||
{MB_WRITE_MULTIPLE_COILS, funcWriteMultipleCoils},
|
||||
{MB_WRITE_MULTIPLE_REGISTERS, funcWriteMultipleHoldingRegister},
|
||||
};
|
||||
|
||||
static void chrg_comm_poll (void *pdata)
|
||||
|
||||
static int chrg_comm_parse_msg (struct chrg_comm_t *pCOMM)
|
||||
{
|
||||
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)pdata;
|
||||
if (RT_NULL == pCOMM) {
|
||||
LOG_E("poll null.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pCOMM->rx_len < 8) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (0 != usMBCRC16(pCOMM->rx_buf, pCOMM->rx_len)) {
|
||||
rt_kprintf("crc %04X ", usMBCRC16(pCOMM->rx_buf, pCOMM->rx_len-2));
|
||||
return -3;
|
||||
}
|
||||
|
||||
int i = 0, ret = -1;
|
||||
rt_uint16_t crc = 0;
|
||||
eMBException eStatus = MB_EX_ILLEGAL_FUNCTION;
|
||||
rt_uint8_t address = 0, functionCode = 0x00;
|
||||
address = pCOMM->rx_buf[0];
|
||||
functionCode = pCOMM->rx_buf[1];
|
||||
|
||||
if ((address != pCOMM->addr)&&(MB_ADDRESS_BROADCAST != address)) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct code_func_t *pFunc = RT_NULL;
|
||||
for (i = 0; i < MB_HANDLERS_MAX; i++) {
|
||||
pFunc = &funcHandlers[i];
|
||||
if (RT_NULL == pFunc) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ((functionCode == pFunc->code)&&(RT_NULL != pFunc->func)) {
|
||||
eStatus = pFunc->func(pCOMM->rx_buf, pCOMM->rx_len,
|
||||
pCOMM->tx_buf, &pCOMM->tx_len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (MB_ADDRESS_BROADCAST != address) {
|
||||
pCOMM->tx_buf[0] = address;
|
||||
if (MB_EX_NONE != eStatus) {
|
||||
pCOMM->tx_buf[1] = (functionCode | MB_FUNC_ERROR);
|
||||
pCOMM->tx_buf[2] = eStatus;
|
||||
pCOMM->tx_len = 3;
|
||||
}
|
||||
|
||||
crc = usMBCRC16(pCOMM->tx_buf, pCOMM->tx_len);
|
||||
u16_to_u8v(crc, &pCOMM->tx_buf[pCOMM->tx_len]);
|
||||
pCOMM->tx_len += 2;
|
||||
|
||||
ret = chrg_tty_send(pCOMM->tty, pCOMM->tx_buf, pCOMM->tx_len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void chrg_comm_thread_entry (void *data)
|
||||
{
|
||||
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)data;
|
||||
|
||||
if ((RT_NULL == pCOMM)||(RT_NULL == pCOMM->tty)) {
|
||||
return ;
|
||||
}
|
||||
|
||||
eMBInit(pCOMM->addr, 1, 115200, MB_PAR_EVEN);
|
||||
int len = 0;
|
||||
struct chrg_tty_t *pTTY = pCOMM->tty;
|
||||
|
||||
eMBEnable();
|
||||
chrg_tty_set_recv_tmo(pTTY, 8);
|
||||
if (chrg_tty_connect(pTTY) != RT_EOK) {
|
||||
chrg_tty_destory(pTTY);
|
||||
return;
|
||||
}
|
||||
chrg_tty_mode_set(pTTY, 0);
|
||||
|
||||
while (1) {
|
||||
eMBPoll();
|
||||
rt_thread_mdelay(100);
|
||||
memset(pCOMM->rx_buf, 0, sizeof(pCOMM->rx_buf));
|
||||
len = chrg_tty_recv(pTTY, pCOMM->rx_buf, sizeof(pCOMM->rx_buf));
|
||||
if (len <= 0) {
|
||||
rt_thread_mdelay(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
pCOMM->rx_len = len;
|
||||
pCOMM->tx_len = 0;
|
||||
chrg_comm_parse_msg(pCOMM);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static int chrg_comm_init (void)
|
||||
{
|
||||
rt_err_t ret = RT_EOK;
|
||||
struct chrg_comm_t *pCOMM = &chrgcomm;
|
||||
|
||||
pCOMM->tid = rt_thread_create(pCOMM->name, chrg_comm_poll,
|
||||
pCOMM, 1024, RT_MAIN_THREAD_PRIORITY-1, 10);
|
||||
pCOMM->tty = chrg_tty_create(pCOMM->devname, 115200, 0, 8, 1);
|
||||
if (RT_NULL == pCOMM->tty) {
|
||||
LOG_E("tty can't create.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
rt_memset(pCOMM->rx_buf, 0, SIZE_BUF_TTY);
|
||||
|
||||
ret = rt_mutex_init(&pCOMM->lock, pCOMM->name, RT_IPC_FLAG_FIFO);
|
||||
if (ret != RT_EOK) {
|
||||
LOG_E("init mutex '%s' failed.", pCOMM->name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pCOMM->tid = rt_thread_create(pCOMM->name, chrg_comm_thread_entry, pCOMM, 4096,
|
||||
RT_MAIN_THREAD_PRIORITY, 20);
|
||||
if (RT_NULL != pCOMM->tid) {
|
||||
rt_thread_startup(pCOMM->tid);
|
||||
} else {
|
||||
LOG_E("create thread '%s' failed.", pCOMM->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
INIT_APP_EXPORT(chrg_comm_init);
|
||||
|
||||
int rs485_test_send (int argc, char **argv)
|
||||
{
|
||||
char i = 0;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
xMBPortSerialPutByte(i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT(rs485_test_send, test rs485 send);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef __CHRG_COMM_H__
|
||||
#define __CHRG_COMM_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "chrg_tty.h"
|
||||
|
||||
#define THR_NAME_POLL "thr.poll"
|
||||
#define THR_NAME_SEND "thr.send"
|
||||
|
||||
@@ -15,22 +19,137 @@
|
||||
#define S_REG_HOLDING_START 0
|
||||
#define S_REG_HOLDING_NREGS 100
|
||||
|
||||
/* salve mode: holding register's all address */
|
||||
#define S_HD_RESERVE 0
|
||||
/* salve mode: input register's all address */
|
||||
#define S_IN_RESERVE 0
|
||||
/* salve mode: coil's all address */
|
||||
#define S_CO_RESERVE 0
|
||||
/* salve mode: discrete's all address */
|
||||
#define S_DI_RESERVE 0
|
||||
#define MB_HANDLERS_MAX (16)
|
||||
|
||||
typedef enum {
|
||||
MB_REG_READ, /*!< Read register values and pass to protocol stack. */
|
||||
MB_REG_WRITE /*!< Update register values. */
|
||||
} eMBRegisterMode;
|
||||
|
||||
typedef enum {
|
||||
MB_ENOERR, /*!< no error. */
|
||||
MB_ENOREG, /*!< illegal register address. */
|
||||
MB_EINVAL, /*!< illegal argument. */
|
||||
MB_EPORTERR, /*!< porting layer error. */
|
||||
MB_ENORES, /*!< insufficient resources. */
|
||||
MB_EIO, /*!< I/O error. */
|
||||
MB_EILLSTATE, /*!< protocol stack in illegal state. */
|
||||
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||
} eMBErrorCode;
|
||||
|
||||
#define MB_ADDRESS_BROADCAST (0) // 广播地址
|
||||
#define MB_ADDRESS_MIN (1) // 地址最小值
|
||||
#define MB_ADDRESS_MAX (247) // 地址最大值
|
||||
|
||||
#define MB_READ_COILS (0x01) // 读线圈
|
||||
#define MB_READ_DISCRETE_INPUTS (0x02) // 读离散输入
|
||||
#define MB_READ_HOLDING_REGISTER (0x03) // 读保持寄存器
|
||||
#define MB_READ_INPUT_REGISTER (0x04) // 读输入寄存器
|
||||
#define MB_WRITE_SINGLE_COIL (0x05) // 写单个线圈
|
||||
#define MB_WRITE_REGISTER (0x06) // 写单个寄存器
|
||||
#define MB_WRITE_MULTIPLE_COILS (0x0F) // 写多个线圈
|
||||
#define MB_WRITE_MULTIPLE_REGISTERS (0x10) // 写多个寄存器
|
||||
#define MB_FUNC_ERROR (0x80)
|
||||
|
||||
typedef enum {
|
||||
MB_EX_NONE = 0x00,
|
||||
MB_EX_ILLEGAL_FUNCTION = 0x01,
|
||||
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
|
||||
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
|
||||
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
|
||||
MB_EX_ACKNOWLEDGE = 0x05,
|
||||
MB_EX_SLAVE_BUSY = 0x06,
|
||||
MB_EX_MEMORY_PARITY_ERROR = 0x08,
|
||||
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
|
||||
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||
} eMBException;
|
||||
|
||||
#define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
|
||||
#define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
|
||||
#define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
|
||||
#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
|
||||
#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
|
||||
|
||||
typedef eMBException(*pFunctionHandler) (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
|
||||
rt_uint8_t *resFrame, rt_uint16_t *resLen);
|
||||
|
||||
struct code_func_t {
|
||||
rt_uint8_t code;
|
||||
pFunctionHandler func;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Constants which defines the format of a modbus frame. The example is
|
||||
* shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
|
||||
* dependent on the underlying transport.
|
||||
*
|
||||
* <code>
|
||||
* <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
|
||||
* <----------- MODBUS PDU (1') ---------------->
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | Address | Function Code | Data | CRC/LRC |
|
||||
* +-----------+---------------+----------------------------+-------------+
|
||||
* | | | |
|
||||
* (2) (3/2') (3') (4)
|
||||
*
|
||||
* (1) ... MB_SER_PDU_SIZE_MAX = 256
|
||||
* (2) ... MB_SER_PDU_ADDR_OFF = 0
|
||||
* (3) ... MB_SER_PDU_PDU_OFF = 1
|
||||
* (4) ... MB_SER_PDU_SIZE_CRC = 2
|
||||
*
|
||||
* (1') ... MB_PDU_SIZE_MAX = 253
|
||||
* (2') ... MB_PDU_FUNC_OFF = 0
|
||||
* (3') ... MB_PDU_DATA_OFF = 1
|
||||
* </code>
|
||||
*/
|
||||
|
||||
#define MB_PDU_SIZE_MAX 253 /*!< Maximum size of a PDU. */
|
||||
#define MB_PDU_SIZE_MIN 1 /*!< Function Code */
|
||||
#define MB_PDU_FUNC_OFF 0 /*!< Offset of function code in PDU. */
|
||||
#define MB_PDU_DATA_OFF 1 /*!< Offset for response data in PDU. */
|
||||
|
||||
#define MB_PDU_FUNC_READ_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF (MB_PDU_DATA_OFF)
|
||||
|
||||
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_READ_REGCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_READ_COILCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF (MB_PDU_DATA_OFF + 2)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF (MB_PDU_DATA_OFF + 4)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF (MB_PDU_DATA_OFF + 5)
|
||||
|
||||
#define MB_PDU_FUNC_READ_SIZE (4)
|
||||
#define MB_PDU_FUNC_WRITE_SIZE (4)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN (5)
|
||||
|
||||
#define MB_PDU_FUNC_READ_DISCCNT_MAX (0x07D0)
|
||||
#define MB_PDU_FUNC_READ_COILCNT_MAX (0x07D0)
|
||||
#define MB_PDU_FUNC_READ_REGCNT_MAX (0x007D)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX (0x0078)
|
||||
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX (0x07B0)
|
||||
|
||||
#define THR_NAME_COMM "thr.comm"
|
||||
#define DEV_NAME_COMM "uart1"
|
||||
|
||||
struct chrg_comm_t {
|
||||
rt_thread_t tid;
|
||||
const char *name;
|
||||
const char *name;
|
||||
const char *devname;
|
||||
struct rt_thread *tid;
|
||||
struct rt_mutex lock;
|
||||
|
||||
rt_uint8_t addr; // slave address
|
||||
struct chrg_tty_t *tty;
|
||||
|
||||
rt_uint16_t rx_len;
|
||||
rt_uint8_t rx_buf[SIZE_BUF_TTY];
|
||||
rt_uint16_t tx_len;
|
||||
rt_uint8_t tx_buf[SIZE_BUF_TTY];
|
||||
|
||||
rt_uint8_t addr; // slave address
|
||||
};
|
||||
|
||||
extern struct chrg_comm_t chrgcomm;
|
||||
|
||||
@@ -56,8 +56,8 @@ static void chrg_north_thread_entry (void *data)
|
||||
}
|
||||
|
||||
|
||||
rt_kprintf("%s\r\n", (char *)pNOR->rx_buf);
|
||||
chrg_tty_send(pTTY, pNOR->rx_buf, len);
|
||||
//rt_kprintf("%s\r\n", (char *)pNOR->rx_buf);
|
||||
//chrg_tty_send(pTTY, pNOR->rx_buf, len);
|
||||
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ static int chrg_north_init (void)
|
||||
INIT_APP_EXPORT(chrg_north_init);
|
||||
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
static int north_send(rt_uint8_t argc, char **argv)
|
||||
{
|
||||
return chrg_north_send("1234567890abcdef", 16);
|
||||
|
||||
@@ -53,8 +53,8 @@ static void chrg_south_thread_entry (void *data)
|
||||
continue;
|
||||
}
|
||||
|
||||
rt_kprintf("%s\r\n", (char *)pSOU->rx_buf);
|
||||
chrg_tty_send(pTTY, pSOU->rx_buf, len);
|
||||
//rt_kprintf("%s\r\n", (char *)pSOU->rx_buf);
|
||||
//chrg_tty_send(pTTY, pSOU->rx_buf, len);
|
||||
|
||||
|
||||
}
|
||||
@@ -94,6 +94,7 @@ static int chrg_south_init (void)
|
||||
|
||||
INIT_APP_EXPORT(chrg_south_init);
|
||||
|
||||
#if 0
|
||||
static int south_send(rt_uint8_t argc, char **argv)
|
||||
{
|
||||
return chrg_south_send("1234567890abcdef", 16);
|
||||
@@ -101,6 +102,6 @@ static int south_send(rt_uint8_t argc, char **argv)
|
||||
|
||||
|
||||
MSH_CMD_EXPORT(south_send, south send test);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,70 @@
|
||||
|
||||
#include "chrg_utils.h"
|
||||
|
||||
static const rt_uint8_t aucCRCHi[] = {
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||
0x00, 0xC1, 0x81, 0x40
|
||||
};
|
||||
|
||||
static const rt_uint8_t aucCRCLo[] = {
|
||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
||||
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
||||
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
||||
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
|
||||
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
||||
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
|
||||
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
|
||||
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
|
||||
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
|
||||
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
||||
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
|
||||
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
|
||||
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
|
||||
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
|
||||
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
||||
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
|
||||
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
|
||||
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
|
||||
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
|
||||
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
||||
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
|
||||
0x41, 0x81, 0x80, 0x40
|
||||
};
|
||||
|
||||
rt_uint16_t usMBCRC16 (rt_uint8_t *pucFrame, rt_uint16_t usLen)
|
||||
{
|
||||
rt_uint8_t ucCRCHi = 0xFF;
|
||||
rt_uint8_t ucCRCLo = 0xFF;
|
||||
int iIndex;
|
||||
|
||||
while ( usLen-- ) {
|
||||
iIndex = ucCRCLo ^ *(pucFrame++);
|
||||
ucCRCLo = (rt_uint8_t)(ucCRCHi ^ aucCRCHi[iIndex]);
|
||||
ucCRCHi = aucCRCLo[iIndex];
|
||||
}
|
||||
|
||||
return (rt_uint16_t)(ucCRCHi << 8 | ucCRCLo);
|
||||
}
|
||||
|
||||
rt_uint8_t _crc8 (rt_uint8_t res, rt_uint8_t data)
|
||||
{
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
extern rt_uint16_t usMBCRC16 (rt_uint8_t * pucFrame, rt_uint16_t usLen);
|
||||
|
||||
extern rt_uint8_t calc_crc8 (rt_uint8_t *data, rt_uint8_t length);
|
||||
|
||||
extern rt_uint8_t u16_to_u8v (rt_uint16_t value, rt_uint8_t *buf);
|
||||
|
||||
+1
-51
@@ -339,7 +339,7 @@
|
||||
<MiscControls></MiscControls>
|
||||
<Define>__STDC_LIMIT_MACROS, RT_USING_LIBC, RT_USING_ARMLIBC, USE_HAL_DRIVER, STM32F405xx, __RTTHREAD__, __CLK_TCK=RT_TICK_PER_SECOND</Define>
|
||||
<Undefine></Undefine>
|
||||
<IncludePath>..\..\rt-thread\components\drivers\phy;applications\mb;..\..\rt-thread\components\drivers\include;applications\thread;..\..\rt-thread\components\libc\compilers\common\extension\fcntl\octal;..\..\rt-thread\libcpu\arm\common;applications;..\..\rt-thread\components\libc\compilers\common\extension;..\..\rt-thread\components\drivers\include;.;applications\utils;..\..\libs\stm32f4\STM32F4_CMSIS\Include;..\..\rt-thread\include;board\ports;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers\config;..\..\libs\stm32f4\STM32F4_HAL\Inc;..\..\rt-thread\components\libc\posix\ipc;..\..\rt-thread\components\libc\compilers\common\include;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers;..\..\rt-thread\components\libc\posix\io\epoll;board\CubeMX_Config\Inc;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers\drv_flash;..\..\rt-thread\components\finsh;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\smp_call;..\..\rt-thread\libcpu\arm\cortex-m4;..\..\rt-thread\components\libc\posix\io\poll;..\..\rt-thread\components\fal\inc;..\..\libs\cmsis-core\Include;..\..\rt-thread\components\drivers\include;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\libc\posix\io\eventfd;applications\bsp;..\..\rt-thread\components\drivers\include;..\..\libs\stm32f4\STM32F4_HAL\Inc\Legacy;board;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\include</IncludePath>
|
||||
<IncludePath>..\..\rt-thread\components\drivers\phy;..\..\rt-thread\components\drivers\include;applications\thread;..\..\rt-thread\components\libc\compilers\common\extension\fcntl\octal;..\..\rt-thread\libcpu\arm\common;applications;..\..\rt-thread\components\libc\compilers\common\extension;..\..\rt-thread\components\drivers\include;.;applications\utils;..\..\libs\stm32f4\STM32F4_CMSIS\Include;..\..\rt-thread\include;board\ports;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers\config;..\..\libs\stm32f4\STM32F4_HAL\Inc;..\..\rt-thread\components\libc\posix\ipc;..\..\rt-thread\components\libc\compilers\common\include;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers;..\..\rt-thread\components\libc\posix\io\epoll;board\CubeMX_Config\Inc;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers\drivers\drv_flash;..\..\rt-thread\components\finsh;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\smp_call;..\..\rt-thread\libcpu\arm\cortex-m4;..\..\rt-thread\components\libc\posix\io\poll;..\..\rt-thread\components\fal\inc;..\..\libs\cmsis-core\Include;..\..\rt-thread\components\drivers\include;..\..\rt-thread\bsp\stm32\libraries\HAL_Drivers;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\libc\posix\io\eventfd;applications\bsp;..\..\rt-thread\components\drivers\include;..\..\libs\stm32f4\STM32F4_HAL\Inc\Legacy;board;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\include;..\..\rt-thread\components\drivers\include</IncludePath>
|
||||
</VariousControls>
|
||||
</Cads>
|
||||
<Aads>
|
||||
@@ -445,56 +445,6 @@
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>Applications/mb</GroupName>
|
||||
<Files>
|
||||
<File>
|
||||
<FileName>mbserial.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbserial.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbevent.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbevent.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbport.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbport.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbutils.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbutils.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mb.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mb.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbrtu.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbrtu.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbtimer.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbtimer.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbfunc.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbfunc.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>mbcrc.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>applications\mb\mbcrc.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
<GroupName>Applications/thread</GroupName>
|
||||
<Files>
|
||||
|
||||
@@ -426,8 +426,6 @@
|
||||
#define BSP_USING_UART
|
||||
#define BSP_STM32_UART_V1_TX_TIMEOUT 2000
|
||||
#define BSP_USING_UART1
|
||||
#define BSP_UART1_RX_USING_DMA
|
||||
#define BSP_UART1_TX_USING_DMA
|
||||
#define BSP_USING_UART2
|
||||
#define BSP_USING_UART3
|
||||
#define BSP_UART3_RX_USING_DMA
|
||||
|
||||
Reference in New Issue
Block a user