update
This commit is contained in:
@@ -12,6 +12,9 @@
|
||||
#define BIG_ENDIAN (0)//大端序
|
||||
#define LITTLE_ENDIAN (1)//小端序
|
||||
|
||||
#define MODBUS_TCP_PROTO_TYPE (0x0000)
|
||||
|
||||
|
||||
typedef struct {
|
||||
u8 send_buffer[MODBUS_SENDBUF_LEN];
|
||||
u16 len;
|
||||
|
||||
@@ -114,16 +114,22 @@ static void proto_leakage_tx_curr_data_get(proto_leakage_t *p_leakage)
|
||||
|
||||
static void proto_leakage_tx_task(proto_leakage_t *p_leakage)
|
||||
{
|
||||
|
||||
u16 i;
|
||||
proto_sensor_class_t *p_sensor;
|
||||
|
||||
p_sensor = &p_leakage->sensor[p_leakage->sensor_index];
|
||||
|
||||
if(0 == (p_sensor->comm.sensor_state_code & (0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT)))
|
||||
if(0 == p_leakage->sensor_num)/*没有传感器的COM口不发送*/
|
||||
{
|
||||
if((++p_sensor->comm.tx_time_out_count) > 20)/*500ms轮询 10秒通讯超时*/
|
||||
return;
|
||||
}
|
||||
|
||||
if(0 == (p_sensor->comm.sensor_state_code & (PROTO_LEAKAGE_STATE_CODE_TIME_OUT)))
|
||||
{
|
||||
if((++p_sensor->comm.tx_time_out_count) > 10)/*连续读取10次 通讯超时*/
|
||||
{
|
||||
p_sensor->comm.sensor_state_code |= (0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT);
|
||||
p_sensor->comm.sensor_state_code |= (PROTO_LEAKAGE_STATE_CODE_TIME_OUT);
|
||||
|
||||
|
||||
/*清除数据*/
|
||||
//memset(&gas_data[p_sensor->sensor_index],0,sizeof(gas_data_t));
|
||||
}
|
||||
@@ -131,6 +137,10 @@ static void proto_leakage_tx_task(proto_leakage_t *p_leakage)
|
||||
|
||||
switch(p_sensor->comm.state)
|
||||
{
|
||||
case PROTO_LEAKAGE_COMM_STATE_INIT:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET:
|
||||
{
|
||||
proto_leakage_tx_curr_data_get(p_leakage);
|
||||
@@ -140,14 +150,34 @@ static void proto_leakage_tx_task(proto_leakage_t *p_leakage)
|
||||
|
||||
}break;
|
||||
}
|
||||
|
||||
/*通讯超时*/
|
||||
if(p_sensor->comm.sensor_state_code & (PROTO_LEAKAGE_STATE_CODE_TIME_OUT))
|
||||
{
|
||||
for(i=0;i<APP_LEAKAGE_SUB_DEVICE_CH_NUM;i++)
|
||||
{
|
||||
leakage.sub_device_data[p_sensor->comm.leakage_data_index].ch_data[i].state |= APP_LEAKAGE_SUB_DEVICE_STATE_TIME_OUT;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0;i<APP_LEAKAGE_SUB_DEVICE_CH_NUM;i++)
|
||||
{
|
||||
leakage.sub_device_data[p_sensor->comm.leakage_data_index].ch_data[i].state &= (~APP_LEAKAGE_SUB_DEVICE_STATE_TIME_OUT);
|
||||
}
|
||||
}
|
||||
|
||||
p_sensor->comm.state_send_time++;
|
||||
if(p_sensor->comm.state_send_time >= 3) /*进入异常*/
|
||||
{
|
||||
p_sensor->comm.sensor_state_code |= (0x00000001 << p_sensor->comm.sensor_state_code);/*记录异常状态*/
|
||||
p_sensor->comm.sensor_state_code |= (p_sensor->comm.sensor_state_code);/*记录异常状态*/
|
||||
p_sensor->comm.state_send_time = 0;
|
||||
p_sensor->comm.sensor_state_code = PROTO_LEAKAGE_COMM_STATE_DEFAULT;
|
||||
p_sensor->comm.state = PROTO_LEAKAGE_COMM_STATE_DEFAULT;
|
||||
proto_leakage_switch(p_leakage); /*切换设备*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void proto_leakage_rx_task(u8 *p_data,u16 len,void *other_data)
|
||||
@@ -209,7 +239,7 @@ static void proto_leakage_rx_task(u8 *p_data,u16 len,void *other_data)
|
||||
|
||||
|
||||
p_sensor->comm.tx_time_out_count = 0;
|
||||
p_sensor->comm.sensor_state_code &= (~(0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT));
|
||||
p_sensor->comm.sensor_state_code &= (~PROTO_LEAKAGE_STATE_CODE_TIME_OUT);
|
||||
|
||||
switch(p_sensor->comm.state)
|
||||
{
|
||||
@@ -220,7 +250,7 @@ static void proto_leakage_rx_task(u8 *p_data,u16 len,void *other_data)
|
||||
case PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET:
|
||||
{
|
||||
/*计算当前设备索引*/
|
||||
u8 sensor_index = p_leakage->sensor_index;
|
||||
u8 sensor_index = p_sensor->comm.leakage_data_index;
|
||||
u16 ch_addr_offset[4] = {0,4,8,11}; /*漏液待数据地址偏移*/
|
||||
u16 temp;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#define PROTO_LEAKAGE_COMM_STATE_DEFAULT PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET
|
||||
|
||||
#define PROTO_LEAKAGE_STATE_CODE_TIME_OUT (30U) /*通讯超时*/
|
||||
#define PROTO_LEAKAGE_STATE_CODE_TIME_OUT (0x8000) /*通讯超时*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -1,326 +0,0 @@
|
||||
/*对外通讯 modbus从机*/
|
||||
#include "proto_modbus_slave_ex.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "app_timer.h"
|
||||
#include "bsp_relay.h"
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_Flash.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_master_leakage.h"
|
||||
|
||||
static modbus_analysis_data_t modbus_analysis_data;//指令解析结构体
|
||||
static modbus_communication_send_buf_t send_struct;//发送结构体
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len, void *data);
|
||||
static void proto_modbus_communication_data_send(u8 *pData, u16 len);
|
||||
static void proto_modbus_init(void);
|
||||
static void proto_modbus_task(void);
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 Addr, u16 Value);
|
||||
static u16 proto_modbus_data_read(u16 Addr);
|
||||
|
||||
proto_Modbus_t modbus_slave_ex=
|
||||
{
|
||||
.id = 0x01,
|
||||
.data_read = proto_modbus_data_read,
|
||||
.data_write = proto_modbus_data_write,
|
||||
.data_analysis = proto_modbus_communication_data_analysis,
|
||||
.init = proto_modbus_init,
|
||||
.task = proto_modbus_task,
|
||||
};
|
||||
|
||||
static proto_Modbus_t *p_modbus = &modbus_slave_ex;
|
||||
static bsp_uart_t * p_rx_uart;
|
||||
|
||||
static void proto_modbus_communication_data_send(u8 *p_data, u16 len)
|
||||
{
|
||||
if(p_rx_uart != NULL)
|
||||
{
|
||||
p_rx_uart->send(p_rx_uart,p_data,len);
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_modbus_init(void)
|
||||
{
|
||||
p_modbus->id = Usr_Flash.FlashData.modbus_id;
|
||||
com_uart1.rx_data_analysis = proto_modbus_communication_data_analysis;
|
||||
com_uart4.rx_data_analysis = proto_modbus_communication_data_analysis;
|
||||
}
|
||||
static void proto_modbus_task(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len,void *other_data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u16 inx;
|
||||
u16 TempAddr, TempData, crc_16;
|
||||
|
||||
|
||||
if (0 == modbus_lib_analysis(&modbus_analysis_data, pData, len))//检查数据长度 校验是否正确
|
||||
return;
|
||||
if (p_modbus->id != modbus_analysis_data.id && modbus_analysis_data.id != 0xe8)//判断ID是否正确
|
||||
return;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
|
||||
/* //检查地址是否超出范围
|
||||
if ((modbus_analysis_data.start_addr >= MODBUS_REG_LEN) || (modbus_analysis_data.reg_number >= MODBUS_REG_LEN) ||
|
||||
(modbus_analysis_data.start_addr + modbus_analysis_data.reg_number >= MODBUS_REG_LEN))
|
||||
{
|
||||
ErrorCode = ModbusErrorCode_IllegalAddr;
|
||||
goto Error;
|
||||
}
|
||||
*/
|
||||
p_rx_uart = (bsp_uart_t*)other_data;
|
||||
switch (modbus_analysis_data.func)
|
||||
{
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = 2 * modbus_analysis_data.reg_number;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[3 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[4 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 3 + send_struct.send_buffer[2]);
|
||||
send_struct.send_buffer[3 + send_struct.send_buffer[2]] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[4 + send_struct.send_buffer[2]] = (crc_16 >> 8) & 0xff;
|
||||
send_struct.len = 5 + send_struct.send_buffer[2];
|
||||
}goto Success;
|
||||
/*特殊协议*/
|
||||
case 0x41:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = (2 * modbus_analysis_data.reg_number) >> 8;
|
||||
send_struct.send_buffer[5] = (2 * modbus_analysis_data.reg_number) & 0xff;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[6 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[7 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6 + 2 * modbus_analysis_data.reg_number);
|
||||
send_struct.send_buffer[6 + 2 * modbus_analysis_data.reg_number] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7 + 2 * modbus_analysis_data.reg_number] = (crc_16 >> 8) & 0xff;
|
||||
send_struct.len = 8 + 2 * modbus_analysis_data.reg_number;
|
||||
}goto Success;
|
||||
|
||||
case 0x06:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
TempData = (modbus_analysis_data.write_data_addr[0] << 8) | modbus_analysis_data.write_data_addr[1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
|
||||
send_struct.len = 8;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = modbus_analysis_data.write_data_addr[0];
|
||||
send_struct.send_buffer[5] = modbus_analysis_data.write_data_addr[1];
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6);
|
||||
send_struct.send_buffer[6] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7] = (crc_16 >> 8) & 0xff;
|
||||
}break;
|
||||
|
||||
case 0x10:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = modbus_analysis_data.write_data_addr[2 * inx];
|
||||
TempData = (TempData << 8) | modbus_analysis_data.write_data_addr[2 * inx + 1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
TempAddr++;
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
send_struct.len = 8;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = modbus_analysis_data.reg_number >> 8;
|
||||
send_struct.send_buffer[5] = modbus_analysis_data.reg_number & 0xff;
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6);
|
||||
send_struct.send_buffer[6] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7] = (crc_16 >> 8) & 0xff;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalFunction;
|
||||
}
|
||||
goto Error;
|
||||
}
|
||||
|
||||
Success:
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
return;
|
||||
|
||||
Error:
|
||||
send_struct.len = 5;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func | 0x80;
|
||||
send_struct.send_buffer[2] = error_code;
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 3);
|
||||
send_struct.send_buffer[3] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[4] = (crc_16 >> 8) & 0xff;
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* 函数: proto_modbus_data_write
|
||||
* 功能: Modbus写寄存器
|
||||
* 参数: Addr: 地址
|
||||
Value:数据
|
||||
* 返回: 无
|
||||
* 描述: 无
|
||||
******************************************/
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 addr, u16 data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u8 temp_point,cali_point;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
case 1 ... 4:
|
||||
{
|
||||
relay.set(addr-1,data);
|
||||
}break;
|
||||
|
||||
/*sn*/
|
||||
case 20000 ... 20004:
|
||||
{
|
||||
Usr_Flash.FlashData.sn[addr - 20000] = data;
|
||||
Usr_Flash.Write();
|
||||
}break;
|
||||
/*modbus_id*/
|
||||
case 20005:
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_id = data;
|
||||
modbus_slave_ex.id = Usr_Flash.FlashData.modbus_id;
|
||||
Usr_Flash.Write();
|
||||
}break;
|
||||
/*modbus_read_reg_num*/
|
||||
case 20006:
|
||||
{
|
||||
if(data > 1000)
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalData;
|
||||
}
|
||||
else
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_read_reg_num = data;
|
||||
Usr_Flash.Write();
|
||||
}
|
||||
}break;
|
||||
case 20007:
|
||||
{
|
||||
if(data > 16)
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalData;
|
||||
}
|
||||
else
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_read_sensor_num = data;
|
||||
Usr_Flash.Write();
|
||||
}
|
||||
}break;
|
||||
/*FC打印相关数据*/
|
||||
case 252:
|
||||
{
|
||||
print.set(data);
|
||||
}break;
|
||||
}
|
||||
return error_code;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* 函数: proto_modbus_data_read
|
||||
* 功能: Modbus读寄存器数据
|
||||
* 参数: Addr: 地址
|
||||
* 返回: 地址对应的数据
|
||||
* 描述: 无
|
||||
******************************************/
|
||||
static u16 proto_modbus_data_read(u16 addr)
|
||||
{
|
||||
u16 data = 0;
|
||||
u16 *p_data;
|
||||
u16 num,offset;
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
case 1 ... 4:
|
||||
{
|
||||
data = relay.state[addr - 1];
|
||||
}break;
|
||||
// /*实时数据*/
|
||||
// case 0 ... 639:
|
||||
// {
|
||||
// u16 size;
|
||||
// size = sizeof(gas_data_t);
|
||||
// /* TDLAS默认读取*/
|
||||
// if( size== (2 * Usr_Flash.FlashData.modbus_read_reg_num))
|
||||
// {
|
||||
// p_data = (u16 *)&gas_data;
|
||||
// data = p_data[addr];
|
||||
// }
|
||||
// else if((2 * Usr_Flash.FlashData.modbus_read_reg_num )< sizeof(gas_data_t))/*其他传感器*/
|
||||
// {
|
||||
// num = addr / Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// offset = addr % Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// p_data = (u16 *)&gas_data[num];
|
||||
// data = p_data[offset];
|
||||
// }
|
||||
//
|
||||
// }break;
|
||||
//
|
||||
// /*sn*/
|
||||
// case 20000 ... 20004:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.sn[addr - 20000];
|
||||
// }break;
|
||||
// /*modbus_id*/
|
||||
// case 20005:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_id;
|
||||
// }break;
|
||||
// /*modbus_read_reg_num*/
|
||||
// case 20006:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// }break;
|
||||
// case 20007:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_read_sensor_num;
|
||||
// }break;
|
||||
default:data = 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
/*对外通讯 modbus从机*/
|
||||
#include "proto_modbus_tcp_slave_ex.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "app_timer.h"
|
||||
#include "app_leakage.h"
|
||||
|
||||
#include "bsp_relay.h"
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_Flash.h"
|
||||
#include "bsp_W5500.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_master_leakage.h"
|
||||
|
||||
static modbus_analysis_data_t modbus_analysis_data;//指令解析结构体
|
||||
static modbus_communication_send_buf_t send_struct;//发送结构体
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len, void *data);
|
||||
static void proto_modbus_communication_data_send(u8 *pData, u16 len);
|
||||
static void proto_modbus_init(void);
|
||||
static void proto_modbus_task(void);
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 Addr, u16 Value);
|
||||
static u16 proto_modbus_data_read(u16 Addr);
|
||||
|
||||
proto_Modbus_t modbus_slave_ex=
|
||||
{
|
||||
.id = 0x01,
|
||||
.data_read = proto_modbus_data_read,
|
||||
.data_write = proto_modbus_data_write,
|
||||
.data_analysis = proto_modbus_communication_data_analysis,
|
||||
.init = proto_modbus_init,
|
||||
.task = proto_modbus_task,
|
||||
};
|
||||
|
||||
static proto_Modbus_t *p_modbus = &modbus_slave_ex;
|
||||
bsp_W5500_Class_t *p_W5500_Class = NULL;
|
||||
|
||||
static void proto_modbus_communication_data_send(u8 *p_data, u16 len)
|
||||
{
|
||||
if(NULL != p_W5500_Class)
|
||||
{
|
||||
W5500.Socket_Send(p_W5500_Class,p_data,len);
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_modbus_init(void)
|
||||
{
|
||||
W5500.W5500_Class[0].Rx_DataAnalysis = proto_modbus_communication_data_analysis;
|
||||
}
|
||||
static void proto_modbus_task(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len,void *other_data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u16 inx;
|
||||
u16 TempAddr, TempData, crc_16;
|
||||
u16 send_buff_index_offset = 0;
|
||||
|
||||
u16 transaction_type,proto_type,proto_len;
|
||||
|
||||
/*mobuds_tcp校验*/
|
||||
transaction_type = pData[0] << 8 | pData[1]; /*事务类型*/
|
||||
proto_type = pData[2] << 8 | pData[3]; /*协议类型*/
|
||||
proto_len = pData[4] << 8 | pData[5]; /*协议长度*/
|
||||
|
||||
/*协议类型 长度判断*/
|
||||
if(MODBUS_TCP_PROTO_TYPE != proto_type || proto_len != (len - 6))
|
||||
{
|
||||
return;
|
||||
}
|
||||
p_W5500_Class = (bsp_W5500_Class_t *)other_data;
|
||||
/*正常modbus协议解析*/
|
||||
modbus_analysis_data.id = pData[6];
|
||||
modbus_analysis_data.func = pData[7];
|
||||
modbus_analysis_data.start_addr = pData[8] << 8 | pData[9];
|
||||
modbus_analysis_data.reg_number = pData[10] << 8 | pData[11];
|
||||
|
||||
send_buff_index_offset = 6;
|
||||
memcpy(send_struct.send_buffer,pData,send_buff_index_offset);
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
/* //检查地址是否超出范围
|
||||
if ((modbus_analysis_data.start_addr >= MODBUS_REG_LEN) || (modbus_analysis_data.reg_number >= MODBUS_REG_LEN) ||
|
||||
(modbus_analysis_data.start_addr + modbus_analysis_data.reg_number >= MODBUS_REG_LEN))
|
||||
{
|
||||
ErrorCode = ModbusErrorCode_IllegalAddr;
|
||||
goto Error;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
switch (modbus_analysis_data.func)
|
||||
{
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[send_buff_index_offset + 0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[send_buff_index_offset + 1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[send_buff_index_offset + 2] = 2 * modbus_analysis_data.reg_number;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[send_buff_index_offset + 3 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[send_buff_index_offset + 4 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
send_struct.len =send_buff_index_offset + 3 + send_struct.send_buffer[send_buff_index_offset + 2];
|
||||
}goto Success;
|
||||
/*特殊协议*/
|
||||
case 0x41:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[send_buff_index_offset + 0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[send_buff_index_offset + 1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[send_buff_index_offset + 2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[send_buff_index_offset + 3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[send_buff_index_offset + 4] = (2 * modbus_analysis_data.reg_number) >> 8;
|
||||
send_struct.send_buffer[send_buff_index_offset + 5] = (2 * modbus_analysis_data.reg_number) & 0xff;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[send_buff_index_offset + 6 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[send_buff_index_offset + 7 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
send_struct.len = send_buff_index_offset + 6 + 2 * modbus_analysis_data.reg_number;
|
||||
}goto Success;
|
||||
|
||||
case 0x06:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
TempData = (modbus_analysis_data.write_data_addr[0] << 8) | modbus_analysis_data.write_data_addr[1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
|
||||
send_struct.len = send_buff_index_offset + 6;
|
||||
send_struct.send_buffer[send_buff_index_offset + 0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[send_buff_index_offset + 1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[send_buff_index_offset + 2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[send_buff_index_offset + 3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[send_buff_index_offset + 4] = modbus_analysis_data.write_data_addr[0];
|
||||
send_struct.send_buffer[send_buff_index_offset + 5] = modbus_analysis_data.write_data_addr[1];
|
||||
}break;
|
||||
|
||||
case 0x10:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = modbus_analysis_data.write_data_addr[2 * inx];
|
||||
TempData = (TempData << 8) | modbus_analysis_data.write_data_addr[2 * inx + 1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
TempAddr++;
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
send_struct.len = send_buff_index_offset + 6;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = modbus_analysis_data.reg_number >> 8;
|
||||
send_struct.send_buffer[5] = modbus_analysis_data.reg_number & 0xff;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalFunction;
|
||||
}
|
||||
goto Error;
|
||||
}
|
||||
|
||||
Success:
|
||||
send_struct.send_buffer[4] = (send_struct.len - send_buff_index_offset) >> 8;
|
||||
send_struct.send_buffer[5] = (send_struct.len - send_buff_index_offset) & 0x00ff;
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
return;
|
||||
|
||||
Error:
|
||||
send_struct.len = send_buff_index_offset + 3;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func | 0x80;
|
||||
send_struct.send_buffer[2] = error_code;
|
||||
send_struct.send_buffer[4] = (send_struct.len - send_buff_index_offset) >> 8;
|
||||
send_struct.send_buffer[5] = (send_struct.len - send_buff_index_offset) & 0x00ff;
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* 函数: proto_modbus_data_write
|
||||
* 功能: Modbus写寄存器
|
||||
* 参数: Addr: 地址
|
||||
Value:数据
|
||||
* 返回: 无
|
||||
* 描述: 无
|
||||
******************************************/
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 addr, u16 data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u8 temp_point,cali_point;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
// case 1 ... 4:
|
||||
// {
|
||||
// relay.set(addr-1,data);
|
||||
// }break;
|
||||
//
|
||||
// /*sn*/
|
||||
// case 20000 ... 20004:
|
||||
// {
|
||||
// Usr_Flash.FlashData.sn[addr - 20000] = data;
|
||||
// Usr_Flash.Write();
|
||||
// }break;
|
||||
// /*modbus_id*/
|
||||
// case 20005:
|
||||
// {
|
||||
// Usr_Flash.FlashData.modbus_id = data;
|
||||
// modbus_slave_ex.id = Usr_Flash.FlashData.modbus_id;
|
||||
// Usr_Flash.Write();
|
||||
// }break;
|
||||
}
|
||||
return error_code;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* 函数: proto_modbus_data_read
|
||||
* 功能: Modbus读寄存器数据
|
||||
* 参数: Addr: 地址
|
||||
* 返回: 地址对应的数据
|
||||
* 描述: 无
|
||||
******************************************/
|
||||
static u16 proto_modbus_data_read(u16 addr)
|
||||
{
|
||||
u16 data = 0;
|
||||
u16 *p_data;
|
||||
u16 num,offset;
|
||||
u16 sensor_index,sensor_ch,reg;
|
||||
u16 *p_string;
|
||||
|
||||
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
case 0 ... 607:
|
||||
{
|
||||
sensor_index = addr / 19; /*哪一个传光强*/
|
||||
reg = addr % 19; /*哪一个数据*/
|
||||
switch(reg)
|
||||
{
|
||||
case 0:/*端口&ID*/
|
||||
{
|
||||
data = (leakage.sub_device_data[sensor_index].flash_data.com + 1)<< 8 | leakage.sub_device_data[sensor_index].flash_data.modbus_id;
|
||||
}break;
|
||||
case 1 ... 5:/*区域名*/
|
||||
{
|
||||
p_string = (u16 *)leakage.sub_device_data[sensor_index].flash_data.region_name;
|
||||
data = p_string[reg - 1]>> 8 | p_string[reg - 1] << 8;
|
||||
}break;
|
||||
case 6 ... 10:/*设备名*/
|
||||
{
|
||||
p_string = (u16 *)leakage.sub_device_data[sensor_index].flash_data.device_name;
|
||||
data = p_string[reg - 6]>> 8 | p_string[reg - 6] << 8;
|
||||
}break;
|
||||
case 11 ... 18:/*通道数据*/
|
||||
{
|
||||
sensor_ch = (addr - 11) / 2;
|
||||
if( (addr - 11) & 0x0001) /*漏液位置*/
|
||||
{
|
||||
data = leakage.sub_device_data[sensor_index].ch_data[sensor_ch].distance;
|
||||
}
|
||||
else /*设备状态*/
|
||||
{
|
||||
data = leakage.sub_device_data[sensor_index].ch_data[sensor_ch].state;
|
||||
}
|
||||
}break;
|
||||
default:data = 0;
|
||||
}
|
||||
|
||||
}break;
|
||||
default:data = 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
#ifndef _PROTO_MODBUS_SLAVE_EX_H_
|
||||
#define _PROTO_MODBUS_SLAVE_EX_H_
|
||||
#ifndef _PROTO_MODBUS_TCP_SLAVE_EX_H_
|
||||
#define _PROTO_MODBUS_TCP_SLAVE_EX_H_
|
||||
#include "main.h"
|
||||
#include "gas_data.h"
|
||||
#include "proto_modbus_lib.h"
|
||||
Reference in New Issue
Block a user