| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /*
- * FreeModbus Libary: BARE Demo Application
- * Copyright (C) 2006 Christian Walter <wolti@sil.at>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * File: $Id$
- */
- /* ----------------------- Modbus includes ----------------------------------*/
- #include "mb.h"
- #include "mbport.h"
- #include "Main.h"
- #include "string.h"
- #include "At24cxx.h"
- #include "ReadEeprom.h"
- #include "gd32e230x_conf.h"
- #include "Timer.h"
- /* ----------------------- Defines ------------------------------------------*/
- //输入寄存器
- #define REG_INPUT_START 3000
- #define REG_INPUT_NREGS 4
- //保持寄存器
- #define REG_HOLD_START Devid_ChalNumAddr
- #define REG_HOLD_NREGS 1000
- //线圈
- #define REG_COILS_START 0
- #define REG_COILS_NREGS 4
- //开关寄存器
- #define REG_DISCRETE_START 1000
- #define REG_DISCRETE_NREGS 4
- /* ----------------------- Static variables ---------------------------------*/
- static USHORT usRegInputStart = REG_INPUT_START;
- static USHORT usRegInputBuf[REG_INPUT_NREGS];
- static USHORT usRegHoldStart = REG_HOLD_START;
- static USHORT usRegHoldBuf[REG_HOLD_NREGS];
- static USHORT usRegCoilsStart = REG_COILS_START;
- static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
- static USHORT usRegDiscreteStart = REG_DISCRETE_START;
- static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
- uint8_t cmd_cnt = 0;
- uint32_t Modbus_Wr_buff[Mb_Wcmd_Width][Mb_Wcmd_len] = {0};
- extern uint32_t dev_info;
- extern bool RL_Allon_flag;
- extern bool RL_Alloff_flag;
- extern unsigned long int Delay_Ms;
- extern uint8_t RL_now_state;
- extern uint8_t AC3PPDU_SlaveAddress;
- extern uint32_t AC3PPDU_Modbus_Reg[AC3PPDU_Modbus_Reg_len];
- extern AC3PPDU_RELAY_Para_Reg AC3PPDU_RL_time;
- /****************************************************************************
- * 名 称:eMBRegInputCB
- * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNRegs: 要读取的寄存器个数
- * 出口参数:
- * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
- * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
- * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
- * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
- * 3 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- usAddress = usAddress - 1;
-
- if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
- {
- iRegIndex = ( int )( usAddress - usRegInputStart );
- while( usNRegs > 0 )
- {
- *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
- *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
- iRegIndex++;
- usNRegs--;
- }
- }
- else
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegHoldingCB
- * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
- * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
- * 03 读保持寄存器 eMBFuncReadHoldingRegister
- * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNRegs: 要读写的寄存器个数
- * eMode: 功能码
- * 出口参数:
- * 注 意:4 区
- ****************************************************************************/
- unsigned char WusNRegs;
- unsigned int WiRegIndex;
- eMBErrorCode
- eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
- {
- uint8_t temp0[4];
- uint32_t T_cnt = 0;
- uint32_t temp1;
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- usAddress = usAddress - 1;
- if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegHoldStart);
- WiRegIndex = iRegIndex;
- switch(eMode)
- {
- case MB_REG_READ://读寄存器
- {
- if((usAddress == Devid_ChalNumAddr)&&(usNRegs == Devid_ChalNumQua)) //读取设备类型和通道
- {
- AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr] = dev_info;
- usRegHoldBuf[iRegIndex]= AC3PPDU_Modbus_Reg[Mb_Dbuff_DevAddr];
- //memcpy(&usRegHoldBuf[iRegIndex],&dev_info,usNRegs * 2);
- //usRegHoldBuf[iRegIndex] = dev_info;
- }
- else if((usAddress >= AC3PPDU_VAP_Addr)&&(usAddress <= AC3PPDU_RL_Over_Ch9_Addr))//读取参数
- {
- T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
- memcpy(&usRegHoldBuf[iRegIndex],(AC3PPDU_Modbus_Reg + T_cnt),usNRegs * 2);
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- while(usNRegs > 0)
- {
- *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
- *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
- iRegIndex++;
- usNRegs--;
- }
- break;
- }
- case MB_REG_WRITE://写寄存器
- {
- WusNRegs = usNRegs;
- while(usNRegs > 0)
- {
- usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
- usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
- iRegIndex++;
- usNRegs--;
- }
- /*
- if(usAddress == Devid_ChalNumAddr)//写入通道
- {
- T_cnt = Devid_ChalNumAddr - AC3PPDU_Modbus_RegStAd;
- memcpy((AC3PPDU_Modbus_Reg + T_cnt),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
- temp0[0] = AC3PPDU_Modbus_Reg[T_cnt];
- AT24CxxWrite(ERom_RL_Ch_N_addr,temp0,1);
- temp1 = AC3PPDU_Device_ID << 8;
- AC3PPDU_Modbus_Reg[T_cnt] += temp1;
- }
- else */
- if((usAddress >= AC3PPDU_ViLit_max_Addr)&&(usAddress <= AC3PPDU_CLEAR_CHN_ALL_CONSUMER))
- {
-
- {
- T_cnt = usAddress - AC3PPDU_Modbus_RegStAd;
- memcpy((Modbus_Wr_buff[cmd_cnt]+2),&usRegHoldBuf[WiRegIndex],WusNRegs * 2);
- Modbus_Wr_buff[cmd_cnt][0] = 1;
- Modbus_Wr_buff[cmd_cnt][1] = T_cnt;
-
- cmd_cnt++;
- if(cmd_cnt >= Mb_Wcmd_Width)
- {
- cmd_cnt = 0;
- }
-
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- break;
- }
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegCoilsCB
- * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
- * 05 写线圈 eMBFuncWriteCoil
- * 15 写多个线圈 eMBFuncWriteMultipleCoils
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 线圈地址
- * usNCoils: 要读写的线圈个数
- * eMode: 功能码
- * 出口参数:
- * 注 意:如继电器
- * 0 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- USHORT iRegIndex;
- USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
- UCHAR ucStatus = 0;
- UCHAR ucBits = 0;
- UCHAR ucDisp = 0;
- usAddress = usAddress - 1;
-
- if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegCoilsStart);
- switch(eMode)
- {
- case MB_REG_READ://读线圈
- while(usCoilGroups--)
- {
- ucDisp = 0;
- ucBits = 8;
- while((usNCoils--) != 0 && (ucBits--) != 0)
- {
- ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
- }
- *pucRegBuffer++ = ucStatus;
- }
- break;
- case MB_REG_WRITE://写线圈
- while(usCoilGroups--)
- {
- ucStatus = *pucRegBuffer++;
- ucBits = 8;
- while((usNCoils--) != 0 && (ucBits--) != 0)
- {
- usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
- ucStatus >>= 1;
- }
- }
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegDiscreteCB
- * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNDiscrete: 要读取的寄存器个数
- * 出口参数:
- * 注 意:1 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- USHORT iRegIndex;
- USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
- UCHAR ucStatus = 0;
- UCHAR ucBits = 0;
- UCHAR ucDisp = 0;
- usAddress = usAddress - 1;
-
- if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegDiscreteStart);
- while(usDiscreteGroups--)
- {
- ucDisp = 0;
- ucBits = 8;
- while((usNDiscrete--) != 0 && (ucBits--) != 0)
- {
- if(usRegDiscreteBuf[iRegIndex])
- {
- ucStatus |= (1 << ucDisp);
- }
- ucDisp++;
- }
- *pucRegBuffer++ = ucStatus;
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
|