FIX 1.优化modbus;

main
wangbo 2 months ago
parent a0e6a2758c
commit 87b5a96e63

@ -117,7 +117,8 @@ enum DEBUG_CM_CMD
CSG_PRV_CONFIG_REAL_WAVE = 5, /* 实时波形配置 */
CSG_PRV_TREND = 10,
CSG_PRV_REAL_PRPS = 11,
CSG_PRV_EVENT = 12
CSG_PRV_EVENT = 12,
CSG_PRV_MODBUS = 13
};
/*趋势上送数据报文头c语言定义*/
@ -483,6 +484,18 @@ typedef struct
uint8_t result;
} csg_add_dau_ack_t;
typedef struct
{
uint32_t current;
uint8_t alarm;
uint8_t reserved[3];
} csg_modbus_iron_t; // 铁芯
typedef struct
{
uint32_t current;
} csg_modbus_magneto_t; // 直流偏磁
/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/

@ -3,6 +3,8 @@
#ifdef CFG_DEV_TYPE_LAND_PD
#include "pd_main.h"
#define WRITE 1
#define READ 0
@ -18,22 +20,27 @@
#define MODBUS_ADDR_ALARM 0x32
#define MODBUS_ALARM_LEN 2
#define DEVICE_IR_TYPE 0x0304
#define DEVICE_DC_TYPE 0x0305
#define DEVICE_M_TYPE 0x03
#define DEVICE_IR_TYPE 0x04
#define DEVICE_DC_TYPE 0x05
#define POS_DATA_LEN 4
#define POS_UINT_ID 6
#define POS_FUNCTION 7
#define MAX_SLOT 6
//#define MAX_SLOT 6
typedef struct
{
uint8_t unit_id;
uint8_t status;
uint16_t type;
uint8_t is_insert;
uint8_t is_connect;
uint8_t type_m;
uint8_t type_s;
uint32_t current;
uint16_t alarm;
} stat_t;
} card_t;
// 定义Modbus TCP MBAP头
@ -60,9 +67,12 @@ typedef struct
int fd;
uint8_t txbuf[512];
uint8_t rxbuf[512];
stat_t stat[MAX_SLOT];
card_t card[PD_SLOTS_MAX];
}modbus_t;
extern modbus_t modbus;
extern int32_t modbus_handle_init(void);
extern int32_t modbus_handle_init_after(void);
#endif

@ -66,6 +66,7 @@
#include "pd_upgrade.h"
#include "pd_dau.h"
#include "pd_hf.h"
#include "pd_modbus.h"
/* Private define ------------------------------------------------------------*/
@ -366,6 +367,7 @@ void _csg_add_dau_recv(char *pkt)
slot--;
printf("_csg_add_dau_recv slot = %d\n", slot);
daus[slot].state.is_insert = TRUE;
modbus.card[slot].is_insert = TRUE;
}
/* 解析心跳报文. */
@ -1481,6 +1483,17 @@ int32_t _csg_send_trend_data(uint8_t slot, void *data)
return E_NONE;
}
int32_t _csg_send_modbus_data(uint8_t slot, int32_t len, void *data)
{
char *pkt = csg.buf_send;
if (NULL == data)
{
return E_BAD_PARAM;
}
memcpy(pkt + sizeof(csg_pkt_head_t), data, len);
_csg_send_data(CSG_PRV_REPLY, CSG_PRV_MODBUS, pkt, len, slot + 1);
return E_NONE;
}
/* csg向后台发包处理函数 */
int32_t _csg_send_process(uint8_t type, uint8_t slot, void *data)
@ -2009,6 +2022,34 @@ void *_csg_recv_handle(void *arg)
return NULL;
}
void _csg_report_modbus()
{
csg_modbus_iron_t iron;
csg_modbus_magneto_t magneto;
for (int i = 0; i < PD_SLOTS_MAX; i++)
{
if (modbus.card[i].is_connect == TRUE
&& modbus.card[i].is_insert == TRUE
&& modbus.card[i].type_m == DEVICE_M_TYPE)
{
switch (modbus.card[i].type_s)
{
case DEVICE_IR_TYPE:
iron.current = modbus.card[i].current;
iron.alarm = modbus.card[i].alarm;
_csg_send_modbus_data(i, sizeof(iron), &iron);
break;
case DEVICE_DC_TYPE:
magneto.current = modbus.card[i].current;
_csg_send_modbus_data(i, sizeof(magneto), &magneto);
break;
default:
break;
}
}
}
}
/* 心跳和连接处理函数. */
void *_csg_heartbeat_handle(void *arg)
@ -2016,6 +2057,7 @@ void *_csg_heartbeat_handle(void *arg)
time_t now = 0;
time_t t_connect = 0;
time_t t_heartbeat = 0;
int count = 0;
/* 等待初始化完成 */
while(!is_system_init)
@ -2062,6 +2104,20 @@ void *_csg_heartbeat_handle(void *arg)
{
_csg_send_add_dau(i, &daus[i].info);
}
else if (modbus.card[i].is_connect == TRUE
&& modbus.card[i].is_insert != TRUE)
{
csg_contact_t info = {0};
info.type_m = modbus.card[i].type_m;
info.type_s = modbus.card[i].type_s;
_csg_send_add_dau(i, &info);
}
}
if (count++ > 60)
{
count = 0;
_csg_report_modbus();
}
}

@ -97,6 +97,145 @@ CMD(pd_slot_terminal,
return CMD_SUCCESS;
}
/* 趋势数据上传间隔. */
CMD(pd_trend_interval,
pd_trend_interval_cmd,
"trend-interval <1-60>",
"Trend data interval\n"
"Interval time: min\n")
{
pd_config.config.trend_period = strtol(argv[0], NULL, 10) * 60;
if (is_system_init)
{
//dau_trend_inv_set();
}
return CMD_SUCCESS;
}
/* 心跳上传间隔. */
CMD(pd_heartbeat_interval,
pd_heartbeat_interval_cmd,
"heartbeat-interval <1-60>",
"heartbeat interval\n"
"Interval time: min\n")
{
pd_config.config.heartbeat_period = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
/* 趋势数据文件数量. */
CMD(pd_trend_amount,
pd_trend_amount_cmd,
"trend-amount <1-5000>",
"Trend file amount\n"
"Trend file amount\n")
{
pd_config.config.trend_storage = strtol(argv[0], NULL, 10);
csg.trend_file.files_max = pd_config.config.trend_storage;
return CMD_SUCCESS;
}
/* 事件数据文件数量. */
CMD(pd_evnet_amount,
pd_evnet_amount_cmd,
"evnet-amount <500-5000>",
"Evnet file amount\n"
"Evnet file amount\n")
{
pd_config.config.event_storage = strtol(argv[0], NULL, 10);
csg.event_file.files_max = pd_config.config.event_storage;
return CMD_SUCCESS;
}
/* 同步模式. */
CMD(pd_sync_mode,
pd_sync_mode_cmd,
"sync-mode (pt|inside|outside)",
"Synchronize mode\n"
"PT\n"
"Power\n"
"Outside\n")
{
uint8_t mode = 0;
if (0 == strncmp(argv[0], "pt", 2))
{
mode = PD_SYNC_PT;
}
else if (0 == strncmp(argv[0], "inside", 5))
{
mode = PD_SYNC_INSIDE;
}
else if (0 == strncmp(argv[0], "outside", 5))
{
mode = PD_SYNC_OUTSIDE;
}
else
{
mode = 0;
}
pd_config.config.sync_mode = mode;
if (is_system_init)
{
//dau_sync_mode_set();
}
return CMD_SUCCESS;
}
/* 同步模式. */
CMD(pd_sync_fre,
pd_sync_fre_cmd,
"sync-inside <400-3000>",
"Synchronize outside mode\n"
"Outside frequency\n")
{
pd_config.config.power_frequency = strtol(argv[0], NULL, 10);
if (is_system_init)
{
//dau_sync_fre_set();
}
return CMD_SUCCESS;
}
/* 同步模式. */
CMD(pd_pps_mode,
pd_pps_mode_cmd,
"pps-mode (auto|master|slave)",
"PPS mode\n"
"Master\n"
"Slave\n")
{
uint8_t mode = 0;
if (0 == strncmp(argv[0], "a", 1))
{
mode = PD_PPS_AUTO;
}
else if (0 == strncmp(argv[0], "m", 1))
{
mode = PD_PPS_MASTER;
}
else
{
mode = PD_PPS_SLAVE;
}
pd_config.config.pps_mode = mode;
if (is_system_init)
{
//dau_pps_mode_set();
}
return CMD_SUCCESS;
}
/* 4G 模块是否使能. */
CMD(pd_4G_enable,
pd_4G_enable_cmd,
@ -576,6 +715,13 @@ int32_t _pd_main_init(void)
pd_slot_node.prompt = XMALLOC(MTYPE_DAU, PD_PORT_PROMPT_LEN);
pd_slot_node.configs = array_init(PD_PORT_CMD_PRI_COUNT, MTYPE_DAU);
cmd_install_element(CONFIG_NODE, &pd_evnet_amount_cmd);
cmd_install_element(CONFIG_NODE, &pd_trend_interval_cmd);
cmd_install_element(CONFIG_NODE, &pd_trend_amount_cmd);
cmd_install_element(CONFIG_NODE, &pd_heartbeat_interval_cmd);
cmd_install_element(CONFIG_NODE, &pd_sync_mode_cmd);
cmd_install_element(CONFIG_NODE, &pd_sync_fre_cmd);
cmd_install_element(CONFIG_NODE, &pd_pps_mode_cmd);
cmd_install_element(CONFIG_NODE, &pd_4G_enable_cmd);
cmd_install_element(CONFIG_NODE, &pd_4G_APN_cmd);
cmd_install_element(CONFIG_NODE, &pd_protocol_type_cmd);

@ -15,6 +15,31 @@
/* Private variables ---------------------------------------------------------*/
modbus_t modbus;
void _modbus_show(void)
{
card_t *pcard = modbus.card;
printh("ID ST IN CO M S\r\n");
for (int slot = 0; slot < PD_SLOTS_MAX; slot++)
{
printh("%-02d %-02d %-02d %-02d %d %d\r\n", pcard->unit_id, pcard->status,
pcard->is_insert, pcard->is_connect, pcard->type_m, pcard->type_s);
pcard++;
}
}
/* modbus 状态显示 */
CMD(modbus_show,
modbus_show_cmd,
"show modbus",
"Show\n"
"Modbus\n")
{
_modbus_show();
return CMD_SUCCESS;
}
int32_t _modbus_tcp_transaction(uint16_t transaction_id, uint8_t unit_id, uint16_t start_addr, uint16_t data_num)
{
int txlen = 0;
@ -44,8 +69,9 @@ int32_t _modbus_tcp_transaction(uint16_t transaction_id, uint8_t unit_id, uint16
DBG(DBG_M_PD_MODBUS_ERR, "Modbus write error\r\n");
return E_ERROR;
}
DBG(DBG_M_PD_MODBUS, "Modbus write len = %d\r\n", txlen);
buf_print((char *)modbus.txbuf, txlen);
//DBG(DBG_M_PD_MODBUS, "Modbus write len = %d\r\n", txlen);
//buf_print((char *)modbus.txbuf, txlen);
// 等待传输完成
usleep(13000); // 13000
@ -54,15 +80,21 @@ int32_t _modbus_tcp_transaction(uint16_t transaction_id, uint8_t unit_id, uint16
rxlen = serial_read(modbus.fd, modbus.rxbuf, sizeof(modbus.rxbuf));
if (rxlen > 0)
{
DBG(DBG_M_PD_MODBUS, "Modbus read len = %d\r\n", rxlen);
buf_print((char *)modbus.rxbuf, rxlen);
uint16_t length;
//DBG(DBG_M_PD_MODBUS, "Modbus read len = %d\r\n", rxlen);
//buf_print((char *)modbus.rxbuf, rxlen);
length = modbus.rxbuf[POS_DATA_LEN] << 8;
length += modbus.rxbuf[POS_DATA_LEN + 1];
if (rxlen == length + 6)
{
uint16_t trans_id = (modbus.rxbuf[0] << 8) + modbus.rxbuf[1];
if (trans_id == transaction_id
&& modbus.rxbuf[POS_UINT_ID] == unit_id)
if (trans_id == transaction_id && modbus.rxbuf[POS_UINT_ID] == unit_id)
{
return E_NONE;
}
}
}
return E_ERROR;
}
@ -102,15 +134,13 @@ int32_t _modbus_rtu_transaction(void)
return E_NONE;
}
int32_t _modbus_get_device_type(uint8_t unit, uint16_t *type)
int32_t _modbus_get_device_type(uint8_t unit, uint8_t *typem, uint8_t *types)
{
uint16_t device_type = 0;
if (!_modbus_tcp_transaction(5, unit, MODBUS_DEVICE_TYPE_ADDR, 1))
{
device_type = modbus.rxbuf[9] << 8;
device_type += modbus.rxbuf[10];
*typem = modbus.rxbuf[9];
*types = modbus.rxbuf[10];
}
*type = device_type;
return E_NONE;
}
@ -129,17 +159,16 @@ int32_t _modbus_get_current(uint8_t unit, uint16_t addr, uint16_t len, uint32_t
return E_NONE;
}
void _modbus_read_slot_stat(stat_t *pstat)
void _modbus_read_slot_stat(card_t *pcard)
{
stat_t *ptmp = pstat;
for (int slot = 0; slot < MAX_SLOT; slot++)
card_t *ptmp = pcard;
for (int slot = 0; slot < PD_SLOTS_MAX; slot++)
{
//pstat[slot].status = 0;
ptmp->status = 0;
ptmp++;
}
ptmp = pstat;
ptmp = pcard;
gpio_val_get(gpio_dau1_idx, &ptmp->status);
ptmp++;
gpio_val_get(gpio_dau2_idx, &ptmp->status);
@ -156,7 +185,7 @@ void _modbus_read_slot_stat(stat_t *pstat)
void *_modbus_send_handle()
{
stat_t *pstat = NULL;
card_t *pcard = NULL;
while (1)
{
#if 0
@ -173,28 +202,31 @@ void *_modbus_send_handle()
}
}
#else
pstat = modbus.stat;
_modbus_read_slot_stat(pstat);
pcard = modbus.card;
_modbus_read_slot_stat(pcard);
for (int slot = 0; slot < MAX_SLOT; slot++)
for (int slot = 0; slot < PD_SLOTS_MAX; slot++)
{
if (pstat->status)
if (pcard->status)
{
_modbus_get_device_type(pstat->unit_id, &pstat->type);
_modbus_get_device_type(pcard->unit_id, &pcard->type_m, &pcard->type_s);
if (DEVICE_IR_TYPE == pstat->type)
if (DEVICE_IR_TYPE == pcard->type_s && DEVICE_M_TYPE == pcard->type_m)
{
_modbus_get_current(pstat->unit_id,
MODBUS_IR_CURRENT_ADDR, MODBUS_IR_CURRENT_LEN, &pstat->current);
DBG(DBG_M_PD_MODBUS, "current:%d\n", pstat->current);
pcard->is_connect = TRUE;
_modbus_get_current(pcard->unit_id,
MODBUS_IR_CURRENT_ADDR, MODBUS_IR_CURRENT_LEN, &pcard->current);
//DBG(DBG_M_PD_MODBUS, "current:%d\n", pcard->current);
}
else if (DEVICE_DC_TYPE == pstat->type)
else if (DEVICE_DC_TYPE == pcard->type_s && DEVICE_M_TYPE == pcard->type_m)
{
_modbus_get_current(pstat->unit_id,
MODBUS_DC_CURRENT_ADDR, MODBUS_DC_CURRENT_LEN, &pstat->current);
pcard->is_connect = TRUE;
_modbus_get_current(pcard->unit_id,
MODBUS_DC_CURRENT_ADDR, MODBUS_DC_CURRENT_LEN, &pcard->current);
}
}
pstat++;
pcard++;
}
#endif
@ -211,11 +243,13 @@ void *_modbus_send_handle()
int32_t modbus_handle_init(void)
{
memset(&modbus, 0, sizeof(modbus));
for (int slot = 0; slot < MAX_SLOT; slot++)
for (int slot = 0; slot < PD_SLOTS_MAX; slot++)
{
modbus.stat[slot].unit_id = slot + 1;
modbus.card[slot].unit_id = slot + 1;
}
modbus.fd = -1;
cmd_install_element(COMMON_NODE, &modbus_show_cmd);
return E_NONE;
}
@ -232,7 +266,6 @@ int32_t modbus_handle_init_after(void)
return E_ERROR;
}
modbus.fd = fd;
DBG(DBG_M_PD_MODBUS, "fd=%d\n", modbus.fd);
param.arg = NULL;
param.priority = 80;

@ -73,8 +73,8 @@ dbg_module_t _dbg_module[DBG_M_COUNT] =
{DBG_M_PD_HF, FALSE, "pd_hf"},
{DBG_M_PD_HF_ERR, TRUE, "pd_hf_err"},
{DBG_M_SERIAL, TRUE, "serial"},
{DBG_M_PD_MODBUS, FALSE, "pd_modbus"},
{DBG_M_PD_MODBUS_ERR, FALSE, "pd_modbus_err"},
{DBG_M_PD_MODBUS, TRUE, "pd_modbus"},
{DBG_M_PD_MODBUS_ERR, TRUE, "pd_modbus_err"},
{DBG_M_FILE_FIFO, TRUE, "file_fifo"},
{DBG_M_FILE_FIFO_ERR, TRUE, "file_fifo_err"},
};

Loading…
Cancel
Save