| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /**
- * @file MODBUS_S_LOW_ATY.c
- *
- * @param Project DEVICE_GENERAL_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2023 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief Familiar functions of modbus for all device
- *
- * @note 4 byte per reg, save float or uint32
- *
- * @version
- * - 1_01_221029 > ATY
- * -# Preliminary version, first Release
- ********************************************************************************
- */
- #ifndef __MODBUS_S_LOW_ATY_C
- #define __MODBUS_S_LOW_ATY_C
- #include "MODBUS_S_LOW_ATY.h"
- /******************************* For user *************************************/
- /******************************************************************************/
- __XDATA uint16_t mbRegHodingS[MODBUS_REG_HOLDING_SIZE] = {0};
- /**
- * @brief modbus process
- * @note only make 03/06/10 function code, others will return err 01
- */
- void Modbus_Process(uint8_t* buf, uint8_t len)
- {
- uint8_t sendBuf[(MODBUS_REG_HOLDING_SIZE * 2) + 4] = {0x00};
- uint16_t sendBufSize = 4;
- sendBuf[0] = buf[0];
- sendBuf[1] = buf[1];
- if(buf[0] != MODBUS_ADDR){ // check addr
- if(len < 4){ // check buf min size
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x02;
- sendBufSize = 6;
- break;
- }
- else if(CheckCrc16Modbus(buf, len)){ // check crc
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x08;
- sendBufSize = 6;
- break;
- }
- else{
- switch(buf[1]) // check function code
- {
- // case MODBUS_FUNC_READ_COILS:{break; }
- // case MODBUS_FUNC_READ_DISCRETE_INPUTS:{break; }
- case MODBUS_FUNC_READ_HOLDING_REGISTERS:{
- uint16_t i = 0, startAddr = 0, regCount = 0;
- startAddr = ((buf[2] << 8) + buf[3]);
- regCount = ((buf[4] << 8) + buf[5]);
- if(startAddr + regCount - 1 > MODBUS_REG_HOLDING_SIZE){
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x02;
- sendBufSize = 6;
- break;
- }
- sendBuf[2] = regCount * 4;
- sendBufSize += 1;
- for(i = 0; i < regCount; i++){
- #ifdef BIG_ENDIAN
- sendBuf[2 + (i * 2) + 1] = mbRegHodingS[startAddr + i];
- sendBuf[2 + (i * 2) + 2] = mbRegHodingS[startAddr + i] >> 8;
- #else
- sendBuf[2 + (i * 2) + 1] = mbRegHodingS[startAddr + i] >> 8;
- sendBuf[2 + (i * 2) + 2] = mbRegHodingS[startAddr + i];
- #endif
- sendBufSize += 4;
- }
- break;
- }
- // case MODBUS_FUNC_READ_INPUT_REGISTERS:{break; }
- // case MODBUS_FUNC_WRITE_SINGLE_COIL:{break; }
- case MODBUS_FUNC_WRITE_SINGLE_HOLDING_REGISTERS:{
- uint16_t startAddr = 0;
- startAddr = (((uint16_t)buf[2] << 8) + (uint16_t)buf[3]);
- if(startAddr >= MODBUS_REG_HOLDING_SIZE){
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x02;
- sendBufSize = 6;
- break;
- }
- #ifdef BIG_ENDIAN
- mbRegHodingS[startAddr] = buf[4] + buf[5] << 8;
- #else
- mbRegHodingS[startAddr] = buf[4] << 8 + buf[5];
- #endif
- sendBuf[2] = startAddr >> 8;
- sendBuf[3] = startAddr;
- #ifdef BIG_ENDIAN
- sendBuf[4] = mbRegHodingS[startAddr];
- sendBuf[5] = mbRegHodingS[startAddr] >> 8;
- #else
- sendBuf[4] = mbRegHodingS[startAddr] >> 8;
- sendBuf[5] = mbRegHodingS[startAddr];
- #endif
- sendBufSize += 6;
- break;
- }
- case MODBUS_FUNC_WRITE_MULTY_HOLDING_REGISTERS:{
- uint16_t i = 0, startAddr = 0, regCount = 0;
- startAddr = ((buf[2] << 8) + buf[3]);
- regCount = ((buf[4] << 8) + buf[5]);
- if(startAddr + regCount - 1 > MODBUS_REG_HOLDING_SIZE){
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x02;
- sendBufSize = 6;
- break;
- }
- for(i = 0; i < regCount; i++)
- {
- #ifdef BIG_ENDIAN
- mbRegHodingS[startAddr + i] = buf[6 + (i * 4) + 1] + buf[6 + (i * 4) + 2] << 8;
- #else
- mbRegHodingS[startAddr + i] = buf[6 + (i * 4) + 1] << 8 + buf[6 + (i * 4) + 2];
- #endif
- }
- sendBuf[2] = startAddr >> 8;
- sendBuf[3] = startAddr;
- sendBuf[4] = regCount >> 8;
- sendBuf[5] = regCount;
- sendBufSize += 4;
- break; }
- default:{
- sendBuf[1] = buf[1] + 0x80;
- sendBuf[2] = 0x01;
- sendBufSize = 6;
- break; }
- }
- }
- sendBuf[sendBufSize - 2] = GenCrc16ModbusHL(sendBuf, sendBufSize - 2, 1);
- sendBuf[sendBufSize - 1] = GenCrc16ModbusHL(sendBuf, sendBufSize - 2, 0);
- UartSendBytes(sendBuf, sendBufSize);
- }
- }
- #endif /* __MODBUS_S_LOW_ATY_C */
- /******************************** End Of File *********************************/
|