#include <stdio.h>
#include <string.h>
#include "User_modbus_TCP.h"
#include "mb.h"
#include "mbutils.h"
void ModbusTCPInit(void)
{
eMBTCPInit(MODBUS_TCP_PORT);
eMBEnable();
}
void ModbusTCPDeInit(void)
{
eMBDisable();
eMBClose();
}
void ModbusTCPMain(void)
{
if (MB_ENOERR != eMBPoll())
{
ModbusTCPDeInit();
ModbusTCPInit();
}
}
//线圈
#define REG_Coils_START 1
#define REG_Coils_SIZE 10
uint8_t Coils_Data[REG_Coils_SIZE] = {1,1,0,1,0,0,1,1,1,0};
/**
* @brief: 读线圈---01,写线圈---05
*
* @param pucRegBuffer 缓存指针
* @param usAddress 起始地址
* @param usNCoils 线圈数量
* @param eMode 读写模式
* [url=home.php?mod=space&uid=266161]@return[/url] eMBErrorCode 错误码
*/
eMBErrorCode eMBRegCoilsCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode)
{
uint16_t i = 0,byteOffset=0,bitOffset=0,RegIndex = usAddress - REG_Coils_START-1;
if ((usAddress >= REG_Coils_START)&&(usAddress + usNCoils <= REG_Coils_START + REG_Coils_SIZE+1))
{
if (MB_REG_READ == eMode)
{
for(i=0;i<usNCoils;i++)
{
byteOffset = i / 8;
bitOffset = i % 8;
xMBUtilSetBits(&pucRegBuffer[byteOffset], bitOffset, 1, Coils_Data[RegIndex+i]);
}
}
else
{
for(i=0;i<usNCoils;i++)
{
byteOffset = i / 8;
bitOffset = i % 8;
Coils_Data[RegIndex+i]=xMBUtilGetBits(&pucRegBuffer[byteOffset], bitOffset, 1);
}
}
}
else
{
return MB_ENOREG;
}
return MB_ENOERR;
}
//离散寄存器
#define REG_DISCRETE_START 10
#define REG_DISCRETE_SIZE 20
uint8_t Discrete_Data[REG_DISCRETE_SIZE] = {1,1,0,1,0,0,1,1,1,0,1,0,0,1};
/**
* @brief:读离散寄存器---02
*
* @param pucRegBuffer 缓存指针
* @param usAddress 起始地址
* @param usNDiscrete 寄存器个数
* @return eMBErrorCode 返回错误码
*/
eMBErrorCode eMBRegDiscreteCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)
{
uint16_t i = 0,byteOffset=0,bitOffset=0,RegIndex = usAddress - REG_DISCRETE_START-1;
if ((usAddress >= REG_DISCRETE_START)&&(usAddress + usNDiscrete <= REG_DISCRETE_START + REG_DISCRETE_SIZE+1))
{
for(i=0;i<usNDiscrete;i++)
{
byteOffset = i / 8;
bitOffset = i % 8;
xMBUtilSetBits(&pucRegBuffer[byteOffset], bitOffset, 1, Discrete_Data[RegIndex+i]);
}
}
else
{
return MB_ENOREG;
}
return MB_ENOERR;
}
//保持寄存器
#define REG_HOLDING_REGISTER_START 10
#define REG_HOLDING_REGISTER_SIZE 30
uint16_t Holding_Data[REG_HOLDING_REGISTER_SIZE] =
{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12};
/**
* @brief: 读保持寄存器---03,写保持寄存器---06
*
* @param pucRegBuffer 缓存指针
* @param usAddress 起始地址
* @param usNRegs 寄存器个数
* @param eMode 读写模式
* @return eMBErrorCode 返回错误码
*/
eMBErrorCode eMBRegHoldingCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode)
{
uint16_t i = 0,RegIndex = usAddress - REG_HOLDING_REGISTER_START-1;
if ((usAddress >= REG_HOLDING_REGISTER_START )&&(usAddress + usNRegs <= REG_HOLDING_REGISTER_START + REG_HOLDING_REGISTER_SIZE+1))
{
if (MB_REG_READ == eMode)//读
{
for(i=0;i<usNRegs;i++)
{
pucRegBuffer[i*2] = (UCHAR)(Holding_Data[RegIndex+i]>>8);
pucRegBuffer[i*2+1] = (UCHAR)Holding_Data[RegIndex+i];
}
}
else//写
{
for(i=0;i<usNRegs;i++)
{
Holding_Data[RegIndex+i]=(pucRegBuffer[i*2]<<8)|(pucRegBuffer[i*2+1]);
}
}
}
else
{
return MB_ENOREG;
}
return MB_ENOERR;
}
//输入寄存器
#define REG_INPUT_REGISTER_START 1
#define REG_INPUT_REGISTER_SIZE 20
uint16_t Input_Data[REG_DISCRETE_SIZE] =
{100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119};
/**
* @brief: 读输入寄存器---04
*
* @param pucRegBuffer 缓存指针
* @param usAddress 起始地址
* @param usNRegs 寄存器个数
* @return eMBErrorCode 返回错误码
*/
eMBErrorCode eMBRegInputCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)
{
uint16_t i = 0,RegIndex = usAddress - REG_INPUT_REGISTER_START-1;
if ((usAddress >= REG_INPUT_REGISTER_START)&&(usAddress + usNRegs <= REG_INPUT_REGISTER_START + REG_INPUT_REGISTER_SIZE+1))
{
for(i=0;i<usNRegs;i++)
{
pucRegBuffer[i*2] = (UCHAR)(Input_Data[RegIndex+i]>>8);
pucRegBuffer[i*2+1] = (UCHAR)Input_Data[RegIndex+i];
}
}
else
{
return MB_ENOREG;
}
return MB_ENOERR;
}