/* * FreeModbus Libary: BARE Demo Application * Copyright (C) 2006 Christian Walter * * 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 "string.h" #include "main.h" /* ----------------------- Defines ------------------------------------------*/ //输入寄存器 #define REG_INPUT_START 3000 #define REG_INPUT_NREGS 1 //保持寄存器 #define REG_HOLD_START STS_R_START #define REG_HOLD_NREGS 500 //线圈 #define REG_COILS_START 0 #define REG_COILS_NREGS 1 //开关寄存器 #define REG_DISCRETE_START 0 #define REG_DISCRETE_NREGS 1 /* ----------------------- 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]; extern unsigned long int Delay_Ms; extern unsigned char detection_value; /* ----------------------- Start implementation -----------------------------*/ //int //main( void ) //{ // eMBErrorCode eStatus; // eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN ); // /* Enable the Modbus Protocol Stack. */ // eStatus = eMBEnable( ); // for( ;; ) // { // ( void )eMBPoll( ); // /* Here we simply count the number of poll cycles. */ // usRegInputBuf[0]++; // } //} /**************************************************************************** * 名 称: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 Chal_num; uint16_t WusNRegs; uint16_t WiRegIndex; unsigned int WeeiRegIndex; eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) { eMBErrorCode eStatus = MB_ENOERR; uint16_t iRegIndex; usAddress = usAddress - 1; sts_data_t *t_data = get_sts(); md_data_t *data = &t_data->m_data; if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS))) { iRegIndex = (uint16_t)(usAddress - usRegHoldStart); WiRegIndex = iRegIndex; switch(eMode) { case MB_REG_READ://读寄存器 { uint16_t *start = &data->dev; memcpy(&usRegHoldBuf[iRegIndex],start+iRegIndex,usNRegs*2); } 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 == STS_OUTPUT_STATUS) { uint16_t recive = 0; memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2); if(recive == 1) { t_data->write_at24(IIC_OUT_CHANNEL,&recive,sizeof(recive)); t_data->open_output(); }else if(recive == 0) { t_data->close_output(); t_data->write_at24(IIC_OUT_CHANNEL,&recive,sizeof(recive)); }else { eStatus = MB_ENOREG; } }else if(usAddress == STS_CHOISE_INPUT) { uint16_t recive = 0; memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2); if(recive == 0) { t_data->choise_input(INPUT_CHANNEL_1); }else { t_data->choise_input(INPUT_CHANNEL_2); } }else if(usAddress >= STS_INPUT_THRESHOLD && usAddress < STS_OUTPUT_THRESHOLD) { WeeiRegIndex = (uint16_t)(usAddress - STS_INPUT_THRESHOLD); uint16_t *p_start = &(data->input_th[0].voltage_max); memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2); t_data->write_at24(IIC_INPUT_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2); for(int i =0 ; i < 2; i++) { if(data->input_th[i].voltage_max <= SMALL_VALUE) UNSET_VOLTAGE_MAX_EN(t_data->input_wanning_en[i]); else SET_VOLTAGE_MAX_EN(t_data->input_wanning_en[i]); if(data->input_th[i].voltage_min <= SMALL_VALUE) UNSET_VOLTAGE_MIN_EN(t_data->input_wanning_en[i]); else SET_VOLTAGE_MIN_EN(t_data->input_wanning_en[i]); if(data->input_th[i].freq_max <= SMALL_VALUE) UNSET_FREQ_MAX_EN(t_data->input_wanning_en[i]); else SET_FREQ_MAX_EN(t_data->input_wanning_en[i]); if(data->input_th[i].freq_min <= SMALL_VALUE) UNSET_FREQ_MIN_EN(t_data->input_wanning_en[i]); else SET_FREQ_MIN_EN(t_data->input_wanning_en[i]); } }else if(usAddress >= STS_OUTPUT_THRESHOLD && usAddress < STS_TEMP_THRESHOLD) { WeeiRegIndex = (uint16_t)(usAddress - STS_OUTPUT_THRESHOLD); uint16_t *p_start = &data->output_th; memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2); t_data->write_at24(IIC_OUTPUT_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2); if(data->output_th.voltage_max <= SMALL_VALUE) UNSET_VOLTAGE_MAX_EN(t_data ->output_wanning_en); else SET_VOLTAGE_MAX_EN(t_data ->output_wanning_en); if(data->output_th.voltage_min <= SMALL_VALUE) UNSET_VOLTAGE_MIN_EN(t_data ->output_wanning_en); else SET_VOLTAGE_MIN_EN(t_data ->output_wanning_en); if(data->output_th.current_max <= SMALL_VALUE) UNSET_CURRENT_MAX_EN(t_data ->output_wanning_en); else SET_CURRENT_MAX_EN(t_data ->output_wanning_en); if(data->output_th.power_max <= SMALL_VALUE) UNSET_POWER_MAX_EN(t_data ->output_wanning_en); else SET_POWER_MAX_EN(t_data ->output_wanning_en); if(data->output_th.consumer_max <= SMALL_VALUE) UNSET_CONSUMER_MAX_EN(t_data ->output_wanning_en); else SET_CONSUMER_MAX_EN(t_data ->output_wanning_en); }else if(usAddress >= STS_TEMP_THRESHOLD && usAddress < STS_OVER_FUNC) { WeeiRegIndex = (uint16_t)(usAddress - STS_TEMP_THRESHOLD); uint16_t *p_start = &data->temprature_th; memcpy(p_start+WeeiRegIndex,&usRegHoldBuf[WiRegIndex],WusNRegs*2); t_data->write_at24(IIC_TEMPRATURE_THRESHOLD_START+(WeeiRegIndex*2),&usRegHoldBuf[WiRegIndex],WusNRegs*2); }else if(usAddress == STS_OVER_FUNC) { uint16_t recive = 0; if(recive > 0x003F) { eStatus = MB_ENOREG; }else{ memcpy(&recive,&usRegHoldBuf[WiRegIndex],WusNRegs*2); t_data->m_data.over_func = recive; t_data->write_at24(IIC_OVER_FUNC_START,&recive,sizeof(recive)); } }else { eStatus = MB_ENOREG; } } } } 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; }