diff --git a/project/Spoolend/include/modbus.h b/project/Spoolend/include/modbus.h index 6121e3d..8cfc967 100644 --- a/project/Spoolend/include/modbus.h +++ b/project/Spoolend/include/modbus.h @@ -8,6 +8,7 @@ /* Modbus 相关参数 */ #define MODBUS_SLAVE_ADDRESS 0x34 +#define MODBUS_FUNCTION_CODE_01 0x01 #define MODBUS_FUNCTION_CODE_03 0x03 #define MODBUS_FUNCTION_CODE_06 0x06 #define MODBUS_FUNCTION_CODE_10 0x10 diff --git a/project/Spoolend/modbus.c b/project/Spoolend/modbus.c index 1b725e7..0d54abd 100644 --- a/project/Spoolend/modbus.c +++ b/project/Spoolend/modbus.c @@ -75,6 +75,9 @@ int Modbus_Slave_Check(char *_pBuffer, uint32_t _iSize) switch (func) { + case MODBUS_FUNCTION_CODE_01: + expected_len = 4; + break; case MODBUS_FUNCTION_CODE_03: case MODBUS_FUNCTION_CODE_06: expected_len = 8; @@ -108,6 +111,8 @@ int Modbus_Slave_Check(char *_pBuffer, uint32_t _iSize) return expected_len; } +extern TComCtrl *g_ptRS485_1; + /*----------------------------------------------* * TComCtrl decode 回调 * * 由 rd_ComRead 内部调用,处理完整 Modbus 帧 * @@ -116,6 +121,7 @@ void Modbus_Slave_Decode(const char *_pBuffer, uint32_t _iSize) { uint16_t startAddress = 0; uint16_t registerCount = 0; + char *pVersion = NULL; RS485_1_Host_Existed = 1; @@ -125,6 +131,10 @@ void Modbus_Slave_Decode(const char *_pBuffer, uint32_t _iSize) switch ((uint8_t)_pBuffer[1]) { + case MODBUS_FUNCTION_CODE_01: + pVersion = Rd_GetBuildTime(); + Modbus_SendResponse(g_ptRS485_1, (uint8_t *)pVersion, strlen(pVersion)); + break; case MODBUS_FUNCTION_CODE_03: startAddress = ((uint8_t)_pBuffer[2] << 8) | (uint8_t)_pBuffer[3]; registerCount = ((uint8_t)_pBuffer[4] << 8) | (uint8_t)_pBuffer[5]; @@ -226,7 +236,6 @@ static void Modbus_HandleFunction06(uint16_t registerAddress, uint16_t value) response[index++] = crc & 0xFF; response[index++] = (crc >> 8) & 0xFF; - extern TComCtrl *g_ptRS485_1; Modbus_SendResponse(g_ptRS485_1, response, index); } @@ -266,7 +275,6 @@ static void Modbus_HandleFunction10(uint16_t startAddress, uint16_t registerCoun response[index++] = crc & 0xFF; response[index++] = (crc >> 8) & 0xFF; - extern TComCtrl *g_ptRS485_1; Modbus_SendResponse(g_ptRS485_1, response, index); } @@ -275,7 +283,6 @@ static void Modbus_HandleFunction10(uint16_t startAddress, uint16_t registerCoun *----------------------------------------------*/ static void Modbus_SendExceptionResponse(uint8_t exceptionCode) { - extern TComCtrl *g_ptRS485_1; response[0] = MODBUS_SLAVE_ADDRESS; response[1] = 0x80; //这里与原版差异为直接回复0x80而不是与一个功能码