Browse Source

Merge branch 'master' of http://jiecheng2024.tpddns.cn:3000/liyuezong/SmartPDCode

guohui 1 year ago
parent
commit
dcc7620ec1

+ 117 - 11
f407/applications/cascade.c

@@ -9,6 +9,7 @@
 #include "stdio.h"
 #include "string.h"
 #include "power.h"
+#include "paras.h"
 
 
 typedef struct {
@@ -66,10 +67,31 @@ typedef struct cascade_info{
 	}data;						//modbus to user
 }cascade_handle_t;
 
+
+
+
 static int mb_write_read(cascade_handle_t *cas, int addr, data_t *wd, data_t *rd);
+static int master_query(cascade_handle_t *cas);
+void slave_func(uint16_t reg,void *r_w_data,uint32_t lenth);
+
+
+
+
+const mb_cb_table_t mb_table = {
+    .read_disc = mb_port_read_disc,      //读离散量输入
+    .read_coil = mb_port_read_coil,      //读线圈
+    .write_coil = mb_port_write_coil,    //写线圈
+    .read_input = mb_port_read_input,    //读输入寄存器
+    .read_hold = mb_port_read_hold,      //读保持寄存器
+    .write_hold = mb_port_write_hold,    //写保持寄存器
+};
+
+
+
 
 
 static cascade_handle_t slave_info = {0};
+static mb_cascade_fun_t func = {0};
 
 uint8_t cascade_init(const char* uart,uint8_t mode,uint8_t addr,uint8_t baud)
 {	
@@ -81,6 +103,8 @@ uint8_t cascade_init(const char* uart,uint8_t mode,uint8_t addr,uint8_t baud)
 	para.para.rtu.parity = 2;
 	para.para.rtu.pin = -1;
   para.para.rtu.lvl = 0;
+	paras_data_t* p_data = paras_get();
+	addr = p_data->cas.addr;
 	if(addr == 0){
 		para.mode = MB_MODE_MASTER;
 	}else
@@ -88,7 +112,7 @@ uint8_t cascade_init(const char* uart,uint8_t mode,uint8_t addr,uint8_t baud)
 		para.mode = MB_MODE_SLAVE;
 		slave_info.saddr = addr;
 	}
-  slave_info.lock = lock_d_init();
+  slave_info.lock = lock_init();
 	slave_info.mb = mb_init(&para);
   if(!slave_info.mb) {
       return -1;
@@ -97,7 +121,9 @@ uint8_t cascade_init(const char* uart,uint8_t mode,uint8_t addr,uint8_t baud)
 	{
 		mb_handle_t *handle = (mb_handle_t *)(slave_info.mb);
 		mb_set_slave(handle->rtu.h,slave_info.saddr);
-		//mb_set_cb_table();
+		mb_set_cb_table(handle->rtu.h,&mb_table);
+		func.slave_func = slave_func;
+		mb_set_cb_cascade(handle->rtu.h,&func);
 	}
 	//cas data
 	
@@ -115,9 +141,9 @@ static uint8_t master_scan(cascade_handle_t *cas)
 	{
 		//scan
 		uint16_t value = 0;
-		lock_s_hold(cas->lock);
+		lock_on(cas->lock);
 		int r = mb_read(cas->mb,slave_addr,CASCADE_REG_SCAN,&value,1,300);
-		lock_s_release(cas->lock);
+		lock_off(cas->lock);
 		if(r > 0)
 		{
 			cas->slaves[slave_addr].addr = slave_addr;
@@ -135,24 +161,103 @@ static uint8_t master_scan(cascade_handle_t *cas)
 
 void * cascade_scan_thread(void *arg)
 {
+	
 	while(1)
 	{
 		if(slave_info.md_type == MB_MODE_MASTER)
 		{
 			master_scan(&slave_info);
+		}else	//做从机//拷贝数据到北向
+		{
+			paras_data_t* p_data = paras_get();
+			power_data_t power = power_data_get();
+			if(p_data->prod.type == PDU_AC_I1O1 || p_data->prod.type == PDU_DC_I1O1)
+			{
+				slave_info.data.ac_dc.dev_cnt = (p_data->prod.type << 8 | power.chs);
+				int cnt = power.chs > 64 ? 64 : power.chs;
+				//电量信息
+				for(int i = 0; i < cnt;i++)
+				{
+					slave_info.data.ac_dc.power[i].voltage 	= power.pch[i].power[0].voltage *1000;
+					slave_info.data.ac_dc.power[i].current 	= power.pch[i].power[0].current *1000;
+					slave_info.data.ac_dc.power[i].power 		= power.pch[i].power[0].power *1000;
+					slave_info.data.ac_dc.power[i].consumer = power.pch[i].power[0].consump *1000;
+					slave_info.data.ac_dc.power[i].factor 	= power.pch[i].power[0].factor *1000;
+					slave_info.data.ac_dc.power[i].freq 		= power.pch[i].power[0].freq *1000;
+				}
+				//总信息
+				//slave_info.data.ac_dc.power_all. = power.ttl.total;
+				slave_info.data.ac_dc.power_all.all_consumer = power.ttl.total[0].consump *1000;
+				slave_info.data.ac_dc.power_all.all_current = power.ttl.total[0].current *1000;
+				slave_info.data.ac_dc.power_all.all_power = power.ttl.total[0].power *1000;
+				slave_info.data.ac_dc.power_all.all_voltage = power.ttl.total[0].voltage *1000;
+				//温湿度
+				slave_info.data.ac_dc.temp = 0;				//后续加上传感器数据
+				slave_info.data.ac_dc.humity = 0;
+				for(int i = 0; i < cnt;i++)
+					slave_info.data.ac_dc.switch_stu[i] = 0;
+				for(int i = 0; i < cnt;i++)
+				{
+					slave_info.data.ac_dc.delay_open[i] = power.pch[i].info.start_delay;
+				}
+				for(int i = 0; i < cnt;i++)
+				{
+					slave_info.data.ac_dc.delay_close[i] = power.pch[i].info.stop_delay;
+				}
+			}else
+			{
+				slave_info.data.ac_3.dev_cnt = (p_data->prod.type << 8 | power.chs);
+				for(int i = A_P; i <= C_P;i++)
+				{
+					slave_info.data.ac_3.ac_3_all_pw[i].consumer = power.ttl.total[i].consump *1000;
+					slave_info.data.ac_3.ac_3_all_pw[i].current = power.ttl.total[i].current *1000;
+					slave_info.data.ac_3.ac_3_all_pw[i].power = power.ttl.total[i].power *1000;
+					slave_info.data.ac_3.ac_3_all_pw[i].voltage = power.ttl.total[i].voltage *1000;
+				}
+				
+			}
 		}
+		
+		
+		
 		sleep(1);
 	}
 }
 
+static void* cascade_thread(void *arg)
+{
+    int r;
+    //thread_handle_t *h=(thread_handle_t*)arg;
+    cascade_handle_t *cas=&slave_info;
+    //ModbusInfo_t *info=&cas->mInfo;
+    mb_handle_t *handle=(mb_handle_t *)(cas->mb);
+    LOGD("__ cascade %s\n", (cas->md_type==MODBUS_MASTER)?"master":"slave");
+    while(handle->quit==0) {
+        if(cas->md_type==MODBUS_MASTER) {      //主模
+            r = master_query(cas);
+            sleep(1);
+        }
+        else {                                  //从模式,等待主设备发起数据请�?
+            //r = slave_receive(cas);
+					mb_slave_fsm(handle->rtu.h);
+        }  
+    }
+    
+    pthread_exit(NULL);
+}
+
+
 static int slave_get_info(cascade_handle_t *cas)
 {
 	slave_info_t *info=&cas->info;
-	board_all_t *board = get_control_board();
-	info->channels = board->chs;
+	power_data_t power = power_data_get();
+	info->channels = power.chs;
 	//info->channels=0;
 	//memcpy(info->ch,board->pbrd[],)
 	//copy mem to info
+	if(info->channels  > SLAVE_CH_MAX)
+			info->channels = SLAVE_CH_MAX;
+	memcpy(info->ch,&power.pch[0],sizeof(power.pch[0]) * info->channels);
 	
 }
 
@@ -304,7 +409,7 @@ static int _mb_write(cascade_handle_t *cas, int addr, data_t *d)
 	if(d->dlen<=0) {
         return -1;
   }
-	lock_d_hold(cas->lock);
+	lock_on(cas->lock);
 	while(1)
 	{
 		 if(wlen+oncelen>d->dlen) {
@@ -328,7 +433,7 @@ static int _mb_write(cascade_handle_t *cas, int addr, data_t *d)
     }
     wlen += xlen;
 	}
-	lock_d_release(cas->lock);
+	lock_off(cas->lock);
 }
 static int _mb_read(cascade_handle_t *cas, int addr, data_t *d)
 {
@@ -338,7 +443,7 @@ static int _mb_read(cascade_handle_t *cas, int addr, data_t *d)
     if(d->dlen<=0) {
         return -1;
     }
-		lock_d_hold(cas->lock);
+		lock_on(cas->lock);
     while(1) {
 			if(rlen+oncelen>d->dlen) {
          xlen = d->dlen-rlen;
@@ -695,8 +800,9 @@ int mb_port_read_hold(u16 addr, u16 *preg)//读保持寄存器, 返回 : 0-成
 
 int mb_port_write_hold(u16 addr, u16 reg)//写保持寄存器, 返回 : 0-成功, -2-地址错误, -3-值非法, -4-设备故障
 {
-	board_all_t *board = get_control_board();
-  if(AC_DC_SET_DELAY_OPEN <= addr && addr <= AC_DC_ALL_SET_SWITCH_REG)
+	//board_all_t *board = get_control_board();
+  power_data_t power = power_data_get();
+	if(AC_DC_SET_DELAY_OPEN <= addr && addr <= AC_DC_ALL_SET_SWITCH_REG)
 	{
 		switch(addr)
 		{

+ 76 - 0
f407/applications/datadef.h

@@ -14,6 +14,11 @@ enum {
     RW_READ=0,                                 //读
     RW_WRITE                                   //写
 };
+enum{
+	A_P = 0,
+	B_P,
+	C_P,
+};
 
 enum {
     TIME_ABS=0,                                 //绝对时间
@@ -144,6 +149,10 @@ enum {
 };
 ////////////////////////////////////////////////
 #pragma pack (1)
+
+
+
+
 typedef struct {
     uint8_t             mode;
     uint8_t             addr;               
@@ -516,6 +525,73 @@ typedef struct {
     uint8_t   data[0];
 }pkt_hdr_t;
 
+typedef struct ac_dc_out{
+	uint32_t voltage;
+	uint32_t current;
+	uint32_t power;
+	uint32_t freq;
+	uint32_t consumer;
+	uint32_t factor;
+}ac_dc_out_t;
+
+typedef struct ac_dc_in{
+	uint32_t all_voltage;
+	uint32_t all_current;
+	uint32_t all_power;
+	uint32_t all_consumer;
+}ac_dc_in_t;
+
+
+typedef struct md_ac_dc{
+    uint16_t          dev_cnt;                   //设备类型和通道数
+    ac_dc_out_t       power[64];                 //最大64通道
+		ac_dc_in_t        power_all;
+    uint16_t          temp;
+		uint16_t          humity;
+    uint16_t          switch_stu[64];
+    uint16_t          delay_open[64];
+    uint16_t          delay_close[64];
+    sensor_info_t     md_s[8];
+    uint16_t          dry_node[4]; 
+}md_ac_dc_t;
+
+typedef struct all_info{
+    uint32_t voltage;
+    uint32_t current;
+    uint32_t power;
+    uint32_t consumer;
+}all_info_t;
+
+typedef struct {
+    uint32_t power;
+    uint32_t freq;
+    uint32_t consumer;
+    uint16_t fac;
+    uint32_t out_type;
+}all_chn_info_t;
+
+
+typedef struct chn_phase_info{
+    uint32_t p_voltage;
+    uint32_t p_current;
+    uint32_t p_power;
+    uint32_t p_consumer;
+    uint32_t p_status;
+}chn_phase_info_t;
+
+typedef struct md_3_ac{
+    uint16_t            dev_cnt;                                        //设备类型和通道数
+    all_info_t          ac_3_all_pw[3];
+    all_chn_info_t      ac_3_all_chn[64];
+    chn_phase_info_t    ac_3_chn_p[64][3];
+    uint16_t            temp;
+		uint16_t            humity;
+    uint16_t            switch_chn[64];
+    uint16_t            delay_open[64];
+    uint16_t            delay_close[64];
+    sensor_info_t       md_s[8];
+    uint16_t            dry_node[4];
+}md_3_ac_t;
 
 
 #pragma pack ()

+ 16 - 16
f407/applications/mb.c

@@ -34,22 +34,22 @@ static void mb_thread(void *arg)
 
 static int mb_start(mb_handle_t *h)
 {
-    if(h->mode==MB_MODE_MASTER) {
-        return -1;
-    }
-    
-    if(h->tid) {
-        mb_stop(h);
-    }
-    
-    h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
-    if(h->tid==NULL) {
-        //LOG_E("___ thread create failed\n");
-        return -1;
-    }
-    
-    h->quit = 0;
-    rt_thread_startup(h->tid);
+//    if(h->mode==MB_MODE_MASTER) {
+//        return -1;
+//    }
+//    
+//    if(h->tid) {
+//        mb_stop(h);
+//    }
+    
+   // h->tid = rt_thread_create(NULL, mb_thread, h, 2048, 5, 20);
+//    if(h->tid==NULL) {
+//        //LOG_E("___ thread create failed\n");
+//        return -1;
+//    }
+//    
+   // h->quit = 0;
+   // rt_thread_startup(h->tid);
     
     return 0;
 }

+ 1 - 0
f407/applications/packages/qmodbus-latest/inc/modbus.h

@@ -115,6 +115,7 @@ int mb_write_and_read_regs(mb_inst_t *hinst, u16 wr_addr, int wr_nb, const u16 *
 #ifdef MB_USING_SLAVE
 //修改从机回调函数表, 默认使用modbus_port中接口函数做回调函数
 void mb_set_cb_table(mb_inst_t *hinst, const mb_cb_table_t *cb);
+void mb_set_cb_cascade(mb_inst_t *hinst, const mb_cascade_fun_t *func);
 //从机状态机处理, 在线程中循环调用即可
 void mb_slave_fsm(mb_inst_t *hinst);
 #endif

File diff suppressed because it is too large
+ 410 - 398
f407/project/app/app.uvoptx


+ 5 - 0
f407/project/app/app.uvprojx

@@ -703,6 +703,11 @@
                 </FileArmAds>
               </FileOption>
             </File>
+            <File>
+              <FileName>cascade.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\..\applications\cascade.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>