update
This commit is contained in:
@@ -12,6 +12,9 @@
|
||||
#define BIG_ENDIAN (0)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define LITTLE_ENDIAN (1)//С<><D0A1><EFBFBD><EFBFBD>
|
||||
|
||||
#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)/*û<>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>COM<4F>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
{
|
||||
if((++p_sensor->comm.tx_time_out_count) > 20)/*500ms<6D><73>ѯ 10<31><30>ͨѶ<CDA8><D1B6>ʱ*/
|
||||
return;
|
||||
}
|
||||
|
||||
if(0 == (p_sensor->comm.sensor_state_code & (PROTO_LEAKAGE_STATE_CODE_TIME_OUT)))
|
||||
{
|
||||
if((++p_sensor->comm.tx_time_out_count) > 10)/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ10<31><30> ͨѶ<CDA8><D1B6>ʱ*/
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
//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;
|
||||
}
|
||||
|
||||
/*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
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) /*<2A><><EFBFBD><EFBFBD><EFBFBD>쳣*/
|
||||
{
|
||||
p_sensor->comm.sensor_state_code |= (0x00000001 << p_sensor->comm.sensor_state_code);/*<2A><>¼<EFBFBD>쳣״̬*/
|
||||
p_sensor->comm.sensor_state_code |= (p_sensor->comm.sensor_state_code);/*<2A><>¼<EFBFBD>쳣״̬*/
|
||||
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); /*<2A>л<EFBFBD><D0BB>豸*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
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:
|
||||
{
|
||||
/*<2A><><EFBFBD>㵱ǰ<E3B5B1>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>*/
|
||||
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}; /*©Һ<C2A9><D2BA><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַƫ<D6B7><C6AB>*/
|
||||
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) /*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
#define PROTO_LEAKAGE_STATE_CODE_TIME_OUT (0x8000) /*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
@@ -1,326 +0,0 @@
|
||||
/*<2A><><EFBFBD><EFBFBD>ͨѶ modbus<75>ӻ<EFBFBD>*/
|
||||
#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;//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
static modbus_communication_send_buf_t send_struct;//<2F><><EFBFBD>ͽṹ<CDBD><E1B9B9>
|
||||
|
||||
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))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> У<><D0A3><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
|
||||
return;
|
||||
if (p_modbus->id != modbus_analysis_data.id && modbus_analysis_data.id != 0xe8)//<2F>ж<EFBFBD>ID<49>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
|
||||
return;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
|
||||
/* //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ƿ<C7B7><F1B3ACB3><EFBFBD>Χ
|
||||
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;
|
||||
/*<2A><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>*/
|
||||
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);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_write
|
||||
* <20><><EFBFBD><EFBFBD>: Modbusд<73>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
Value:<3A><><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
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<46><43>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
case 252:
|
||||
{
|
||||
print.set(data);
|
||||
}break;
|
||||
}
|
||||
return error_code;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_read
|
||||
* <20><><EFBFBD><EFBFBD>: Modbus<75><73><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>ַ<EFBFBD><D6B7>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
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;
|
||||
// /*ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD>*/
|
||||
// case 0 ... 639:
|
||||
// {
|
||||
// u16 size;
|
||||
// size = sizeof(gas_data_t);
|
||||
// /* TDLASĬ<53>϶<EFBFBD>ȡ*/
|
||||
// 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))/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
// {
|
||||
// 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;
|
||||
}
|
||||
299
leakage_system/usr/protocol/proto_modbus_tcp_slave_ex.c
Normal file
299
leakage_system/usr/protocol/proto_modbus_tcp_slave_ex.c
Normal file
@@ -0,0 +1,299 @@
|
||||
/*<2A><><EFBFBD><EFBFBD>ͨѶ modbus<75>ӻ<EFBFBD>*/
|
||||
#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;//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
static modbus_communication_send_buf_t send_struct;//<2F><><EFBFBD>ͽṹ<CDBD><E1B9B9>
|
||||
|
||||
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У<70><D0A3>*/
|
||||
transaction_type = pData[0] << 8 | pData[1]; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
proto_type = pData[2] << 8 | pData[3]; /*Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
proto_len = pData[4] << 8 | pData[5]; /*Э<>鳤<EFBFBD><E9B3A4>*/
|
||||
|
||||
/*Э<><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>*/
|
||||
if(MODBUS_TCP_PROTO_TYPE != proto_type || proto_len != (len - 6))
|
||||
{
|
||||
return;
|
||||
}
|
||||
p_W5500_Class = (bsp_W5500_Class_t *)other_data;
|
||||
/*<2A><><EFBFBD><EFBFBD>modbusЭ<73><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
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;
|
||||
|
||||
/* //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ƿ<C7B7><F1B3ACB3><EFBFBD>Χ
|
||||
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;
|
||||
/*<2A><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>*/
|
||||
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);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_write
|
||||
* <20><><EFBFBD><EFBFBD>: Modbusд<73>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
Value:<3A><><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
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;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_read
|
||||
* <20><><EFBFBD><EFBFBD>: Modbus<75><73><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>ַ<EFBFBD><D6B7>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
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; /*<2A><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ*/
|
||||
reg = addr % 19; /*<2A><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
switch(reg)
|
||||
{
|
||||
case 0:/*<2A>˿<EFBFBD>&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:/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
{
|
||||
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:/*<2A>豸<EFBFBD><E8B1B8>*/
|
||||
{
|
||||
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:/*ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
{
|
||||
sensor_ch = (addr - 11) / 2;
|
||||
if( (addr - 11) & 0x0001) /*©Һλ<D2BA><CEBB>*/
|
||||
{
|
||||
data = leakage.sub_device_data[sensor_index].ch_data[sensor_ch].distance;
|
||||
}
|
||||
else /*<2A>豸״̬*/
|
||||
{
|
||||
data = leakage.sub_device_data[sensor_index].ch_data[sensor_ch].state;
|
||||
}
|
||||
}break;
|
||||
default:data = 0;
|
||||
}
|
||||
|
||||
}break;
|
||||
default:data = 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
@@ -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