Browse Source

现在支持通过协议获取版本信息

dma_temp
Lizongdi 1 week ago
parent
commit
9107d07c73
  1. 1
      project/Spoolend/include/modbus.h
  2. 13
      project/Spoolend/modbus.c

1
project/Spoolend/include/modbus.h

@ -8,6 +8,7 @@
/* Modbus 相关参数 */ /* Modbus 相关参数 */
#define MODBUS_SLAVE_ADDRESS 0x34 #define MODBUS_SLAVE_ADDRESS 0x34
#define MODBUS_FUNCTION_CODE_01 0x01
#define MODBUS_FUNCTION_CODE_03 0x03 #define MODBUS_FUNCTION_CODE_03 0x03
#define MODBUS_FUNCTION_CODE_06 0x06 #define MODBUS_FUNCTION_CODE_06 0x06
#define MODBUS_FUNCTION_CODE_10 0x10 #define MODBUS_FUNCTION_CODE_10 0x10

13
project/Spoolend/modbus.c

@ -75,6 +75,9 @@ int Modbus_Slave_Check(char *_pBuffer, uint32_t _iSize)
switch (func) switch (func)
{ {
case MODBUS_FUNCTION_CODE_01:
expected_len = 4;
break;
case MODBUS_FUNCTION_CODE_03: case MODBUS_FUNCTION_CODE_03:
case MODBUS_FUNCTION_CODE_06: case MODBUS_FUNCTION_CODE_06:
expected_len = 8; expected_len = 8;
@ -108,6 +111,8 @@ int Modbus_Slave_Check(char *_pBuffer, uint32_t _iSize)
return expected_len; return expected_len;
} }
extern TComCtrl *g_ptRS485_1;
/*----------------------------------------------* /*----------------------------------------------*
* TComCtrl decode * * TComCtrl decode *
* rd_ComRead Modbus * * rd_ComRead Modbus *
@ -116,6 +121,7 @@ void Modbus_Slave_Decode(const char *_pBuffer, uint32_t _iSize)
{ {
uint16_t startAddress = 0; uint16_t startAddress = 0;
uint16_t registerCount = 0; uint16_t registerCount = 0;
char *pVersion = NULL;
RS485_1_Host_Existed = 1; RS485_1_Host_Existed = 1;
@ -125,6 +131,10 @@ void Modbus_Slave_Decode(const char *_pBuffer, uint32_t _iSize)
switch ((uint8_t)_pBuffer[1]) 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: case MODBUS_FUNCTION_CODE_03:
startAddress = ((uint8_t)_pBuffer[2] << 8) | (uint8_t)_pBuffer[3]; startAddress = ((uint8_t)_pBuffer[2] << 8) | (uint8_t)_pBuffer[3];
registerCount = ((uint8_t)_pBuffer[4] << 8) | (uint8_t)_pBuffer[5]; 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 & 0xFF;
response[index++] = (crc >> 8) & 0xFF; response[index++] = (crc >> 8) & 0xFF;
extern TComCtrl *g_ptRS485_1;
Modbus_SendResponse(g_ptRS485_1, response, index); 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 & 0xFF;
response[index++] = (crc >> 8) & 0xFF; response[index++] = (crc >> 8) & 0xFF;
extern TComCtrl *g_ptRS485_1;
Modbus_SendResponse(g_ptRS485_1, response, index); 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) static void Modbus_SendExceptionResponse(uint8_t exceptionCode)
{ {
extern TComCtrl *g_ptRS485_1;
response[0] = MODBUS_SLAVE_ADDRESS; response[0] = MODBUS_SLAVE_ADDRESS;
response[1] = 0x80; //这里与原版差异为直接回复0x80而不是与一个功能码 response[1] = 0x80; //这里与原版差异为直接回复0x80而不是与一个功能码

Loading…
Cancel
Save