河北水利局项目
This commit is contained in:
274
hb/dl485.c
Normal file
274
hb/dl485.c
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
|
||||
extern struct st_uart_port COM_485_port;
|
||||
extern QueueSetHandle_t mPortsQueueSet;
|
||||
extern QueueHandle_t WATERMETER485_Task;
|
||||
extern void pro_645_97_read_addr(u8 *outBuf, u16 *outLen);
|
||||
extern st_rtc_ext ERtctime;
|
||||
extern u16 datacount_getcurcount();
|
||||
extern void rp_setcount(u16 count);
|
||||
extern void rp_start();
|
||||
extern void datacount_add(u8 year,u8 month,u8 day,u8 hour);
|
||||
extern void rp_current(u16 count,
|
||||
u8 * current,
|
||||
u8 * sumdata,u8 * surplus,u8 * ala,
|
||||
u8 year,u8 month,u8 day,u8 hour);
|
||||
|
||||
extern void hb_waterdata_one_write(u16 index,u8 * sumdata,u8 year,u8 month,u8 day,u8 hour);
|
||||
|
||||
|
||||
#define PRO_645_97_READADDR (1)
|
||||
#define PRO_645_97_READDATA (2)
|
||||
|
||||
u8 send_485_buff[256];
|
||||
u16 send_485_len = 0;
|
||||
SemaphoreHandle_t sendto_485_semaphore;
|
||||
|
||||
static u8 pro_645_97_type = 0;
|
||||
static u8 meter_addr[7] = {0};
|
||||
|
||||
void dl_485_initbuf(u8 * buf,u16 len)
|
||||
{
|
||||
memmove(send_485_buff,buf,len);
|
||||
send_485_len = len;
|
||||
}
|
||||
|
||||
void dl_485_start(u8 type,u8 *addr, u8 addrlen)
|
||||
{
|
||||
pro_645_97_type = type;
|
||||
if(addr != NULL)
|
||||
{
|
||||
MemCpy(meter_addr,addr,addrlen);
|
||||
}
|
||||
xSemaphoreGive(sendto_485_semaphore);
|
||||
}
|
||||
|
||||
void RS485_gpio_init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
|
||||
RCC_APB2PeriphClockCmd( RS485_CTRL_PORT_CLK | RS485_EXTIN_CLK | RS485_OVERLOAD_CLK , ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = RS485_CTRL_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(RS485_CTRL_PIN_PORT, &GPIO_InitStructure);
|
||||
close_RS485_switch();
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = RS485_EXTIN_PIN; //检测低电压
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_Init(RS485_EXTIN_PORT, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = RS485_OVERLOAD_PIN; //检测低电压
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_Init(RS485_OVERLOAD_PORT, &GPIO_InitStructure);
|
||||
|
||||
hal_sRF_ITConfig(RS485_OVERLOAD_FLAG_LINE, DISABLE);
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = RS485_OVERLOAD_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
}
|
||||
|
||||
bool j645_ackPacket_analyse(u8 *inbuf, u16 len,u8 *out_datas)
|
||||
{
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
||||
//68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
|
||||
if(inbuf[0] == 0x68 && inbuf[7] == 0x68 && inbuf[8] == 0x81)
|
||||
{
|
||||
if(inbuf[16] == GetSum(inbuf,16))
|
||||
{
|
||||
out_datas[0] = 0;
|
||||
out_datas[1] = inbuf[15]-0x33;
|
||||
out_datas[2] = inbuf[14]-0x33;
|
||||
out_datas[3] = inbuf[13]-0x33;
|
||||
out_datas[4] = inbuf[12]-0x33;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool read_meter_process(u8 meter_protcl)
|
||||
{
|
||||
ST_645_head * head_645_ptr = (ST_645_head *)NULL;
|
||||
u8* WaterMeter485Data = NULL;
|
||||
|
||||
u16 count = 0;
|
||||
u8 current_data[5];//当前流量
|
||||
u8 sum_data[5];//累计流量
|
||||
u8 surplus_data[6];//剩余流量
|
||||
u8 ala_data[4];//报警
|
||||
|
||||
bool is_sumdata = false;
|
||||
|
||||
u16 send_length;
|
||||
QueueSetMemberHandle_t rxPortMember;
|
||||
|
||||
|
||||
MemSet(current_data,0,sizeof(current_data));
|
||||
MemSet(sum_data,0,sizeof(sum_data));
|
||||
MemSet(surplus_data,0,sizeof(surplus_data));
|
||||
MemSet(ala_data,0,sizeof(ala_data));
|
||||
|
||||
|
||||
RS485DataRequenstReady();
|
||||
//vTaskDelay(500/portTICK_RATE_MS);
|
||||
vTaskDelay(2000/portTICK_RATE_MS);
|
||||
|
||||
if(meter_protcl == PRO_645_97_READADDR)
|
||||
{
|
||||
pro_645_97_read_addr(COM_485_port.data.txBuf, &send_length);
|
||||
}
|
||||
|
||||
send_length = create_queue_send_packet(COM_485_port.data.txBuf, send_length);
|
||||
printf("[485] TX:");
|
||||
printf_buf(COM_485_port.data.txBuf, send_length);
|
||||
port_send(&COM_485_port, COM_485_port.data.txBuf, send_length);
|
||||
|
||||
if (xQueueReceive(WATERMETER485_Task, &WaterMeter485Data, pdMS_TO_TICKS(2000)) == pdTRUE)
|
||||
{
|
||||
rxPortMember = COM_485_port.xSemaphore;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[485] timeover\r\n");
|
||||
}
|
||||
|
||||
close_RS485_switch();
|
||||
printf("[485] 12V close\r\n");
|
||||
printf_buf(COM_485_port.data.rxBuf, COM_485_port.data.rx_len);
|
||||
|
||||
if (rxPortMember == COM_485_port.xSemaphore)
|
||||
{
|
||||
head_645_ptr = (ST_645_head *)search_645_packet(COM_485_port.data.rxBuf, COM_485_port.data.rx_len);
|
||||
if(head_645_ptr != NULL)
|
||||
{
|
||||
printf("[485] head_645_ptr data anay\r\n");
|
||||
if(j645_ackPacket_analyse((u8*)head_645_ptr,COM_485_port.data.rx_len,sum_data))
|
||||
{
|
||||
is_sumdata = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[485] data error\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[485] format error\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//如果抄表成功
|
||||
if(is_sumdata)
|
||||
{
|
||||
//得到当前计数
|
||||
count = datacount_getcurcount();
|
||||
|
||||
//保存当前数据
|
||||
hb_waterdata_one_write(count,sum_data,ERtctime.year,ERtctime.month,ERtctime.day,ERtctime.hour);
|
||||
|
||||
//上报当前数据
|
||||
rp_current(count,current_data,sum_data,surplus_data,ala_data,ERtctime.year,ERtctime.month,ERtctime.day,ERtctime.hour);
|
||||
|
||||
//计数增加
|
||||
datacount_add(ERtctime.year,ERtctime.month,ERtctime.day,ERtctime.hour);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
|
||||
处理485端口1 接收线程
|
||||
*****************************************************************************/
|
||||
void XYDX485Data_process_task(void *ptr)
|
||||
{
|
||||
WATERMETER485_Task = xQueueCreate( 1, sizeof( void* ) );
|
||||
u32 WaterMeter485Data = (u32)&COM_485_port.data.rxBuf[0];
|
||||
|
||||
vTaskDelay( 500 / portTICK_RATE_MS );
|
||||
|
||||
for (;;)
|
||||
{
|
||||
//此处可改为等待PLC与485两种信号,对信号做同样处理,但是对于485做上行,需要统一下行接口在哪里
|
||||
xSemaphoreTake(COM_485_port.xSemaphore, portMAX_DELAY);
|
||||
|
||||
printf("[485] RX:");
|
||||
printf_buf(COM_485_port.data.rxBuf, COM_485_port.data.rx_len);
|
||||
|
||||
xQueueSendToBack( WATERMETER485_Task, &WaterMeter485Data, 10);
|
||||
|
||||
xQueueReset((QueueHandle_t)COM_485_port.xSemaphore);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void send_to_485(void *ptr)
|
||||
{
|
||||
vSemaphoreCreateBinary(sendto_485_semaphore);
|
||||
xSemaphoreTake(sendto_485_semaphore, 0);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
xSemaphoreTake(sendto_485_semaphore, portMAX_DELAY);
|
||||
if(0 == pro_645_97_type)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(pro_645_97_type == PRO_645_97_READADDR)
|
||||
{
|
||||
read_meter_process(pro_645_97_type);
|
||||
}
|
||||
else if(pro_645_97_type == PRO_645_97_READDATA)
|
||||
{
|
||||
//MemCpy(MeterUnit.Addr.Address,meter_addr,ADDRESS_LENGTH-1);
|
||||
//ret = ReadAddrListDataInfo( &MeterUnit , true);
|
||||
}
|
||||
pro_645_97_type = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
//读地址
|
||||
FE FE FE FE 68 99 99 99 99 99 99 68 01 02 43 C3 6F 16
|
||||
68 06 88 10 40 06 00 68 81 06 43 C0 31 48 03 23 04 00 41 06 00 68 81 06 43 C3 33 C5 CC 33 B6 16
|
||||
|
||||
|
||||
//读水表数据
|
||||
FE FE FE FE 68 06 88 10 40 06 00 68 01 02 43 C3 BD 16
|
||||
68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
水表数据是15
|
||||
|
||||
|
||||
FE FE FE FE 68 99 99 99 99 99 99 68 01 02 43 C3 6F 16
|
||||
FE FE FE 68 59 23 39 09 21 20 68 81 06 43 C3 33 34 33 33 29 16
|
||||
*/
|
||||
|
||||
117
hb/dl485pro.c
Normal file
117
hb/dl485pro.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
|
||||
//读地址
|
||||
void pro_645_97_read_addr(u8 *outBuf, u16 *outLen)
|
||||
{
|
||||
//FE FE FE FE 68 99 99 99 99 99 99 68 01 02 43 C3 6F 16
|
||||
// 68 06 88 10 40 06 00 68 81 06 43 C0 31 48 03 23 04 00 41 06 00 68 81 06 43 C3 33 C5 CC 33 B6 16
|
||||
outBuf[0] = 0x68;
|
||||
outBuf[1] = 0x99;
|
||||
outBuf[2] = 0x99;
|
||||
outBuf[3] = 0x99;
|
||||
outBuf[4] = 0x99;
|
||||
outBuf[5] = 0x99;
|
||||
outBuf[6] = 0x99;
|
||||
outBuf[7] = 0x68;
|
||||
outBuf[8] = 0x01;
|
||||
outBuf[9] = 0x02;
|
||||
outBuf[10] = 0x43;
|
||||
outBuf[11] = 0xC3;
|
||||
outBuf[12] = GetSum(outBuf,12);
|
||||
outBuf[13] = 0x16;
|
||||
*outLen = 14;
|
||||
}
|
||||
|
||||
bool pro_645_97_recv_addr(u8 *inbuf, u16 len, u8 *addr)
|
||||
{
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
||||
//68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
|
||||
if(inbuf[0] == 0x68 && inbuf[7] == 0x68 && inbuf[8] == 0x81)
|
||||
{
|
||||
if(inbuf[16] == GetSum(inbuf,16))
|
||||
{
|
||||
addr[0] = inbuf[1];
|
||||
addr[1] = inbuf[2];
|
||||
addr[2] = inbuf[3];
|
||||
addr[3] = inbuf[4];
|
||||
addr[4] = inbuf[5];
|
||||
addr[5] = inbuf[6];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//读数据
|
||||
void pro_645_97_read_data(u8 *addr,u8 *outBuf, u16 *outLen)
|
||||
{
|
||||
//FE FE FE FE 68 06 88 10 40 06 00 68 01 02 43 C3 BD 16
|
||||
//68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
//水表数据是15
|
||||
outBuf[0] = 0x68;
|
||||
outBuf[1] = addr[0];
|
||||
outBuf[2] = addr[1];
|
||||
outBuf[3] = addr[2];
|
||||
outBuf[4] = addr[3];
|
||||
outBuf[5] = addr[4];
|
||||
outBuf[6] = addr[5];
|
||||
outBuf[7] = 0x68;
|
||||
outBuf[8] = 0x01;
|
||||
outBuf[9] = 0x02;
|
||||
outBuf[10] = 0x43;
|
||||
outBuf[11] = 0xC3;
|
||||
outBuf[12] = GetSum(outBuf,12);
|
||||
outBuf[13] = 0x16;
|
||||
|
||||
*outLen = 14;
|
||||
|
||||
}
|
||||
|
||||
bool pro_645_97_recv_data(u8 *inbuf, u16 len, u8 *inID, u8 *out_datas, u8 *out_status)
|
||||
{
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
||||
//68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
|
||||
if(inbuf[0] == 0x68 && inbuf[7] == 0x68 && inbuf[8] == 0x81)
|
||||
{
|
||||
if(inbuf[16] == GetSum(inbuf,16))
|
||||
{
|
||||
if(0 == memcmp(&inbuf[1],inID,5))
|
||||
{
|
||||
out_datas[0] = 0x2C;
|
||||
out_datas[1] = inbuf[12]-0x33;
|
||||
out_datas[2] = inbuf[13]-0x33;
|
||||
out_datas[3] = inbuf[14]-0x33;
|
||||
out_datas[4] = inbuf[15]-0x33;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
//读地址
|
||||
FE FE FE FE 68 99 99 99 99 99 99 68 01 02 43 C3 6F 16
|
||||
68 06 88 10 40 06 00 68 81 06 43 C0 31 48 03 23 04 00 41 06 00 68 81 06 43 C3 33 C5 CC 33 B6 16
|
||||
|
||||
|
||||
//读水表数据
|
||||
FE FE FE FE 68 06 88 10 40 06 00 68 01 02 43 C3 BD 16
|
||||
68 06 88 10 40 06 00 68 81 06 43 C3 33 48 33 33 22 16
|
||||
水表数据是15
|
||||
*/
|
||||
|
||||
|
||||
92
hb/filei2time.c
Normal file
92
hb/filei2time.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "common.h"
|
||||
#include "rtc_task.h"
|
||||
#include "filei2c.h"
|
||||
|
||||
//include/linux/time.h
|
||||
//ÄÚºËÖеÄmktime()º¯ÊýλÓÚkernel/time.cÄÚ
|
||||
|
||||
|
||||
unsigned long mktime (unsigned int year, unsigned int mon,
|
||||
unsigned int day, unsigned int hour,
|
||||
unsigned int min, unsigned int sec)
|
||||
{
|
||||
if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
|
||||
mon += 12; /* Puts Feb last since it has leap day */
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
return (((
|
||||
(unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
|
||||
year*365 - 719499
|
||||
)*24 + hour /* now have hours */
|
||||
)*60 + min /* now have minutes */
|
||||
)*60 + sec; /* finally seconds */
|
||||
}
|
||||
|
||||
uint8_t bcd_to_dec(uint8_t val)
|
||||
{
|
||||
return ((((val)>>4)*10) + ((val)&0x0F));
|
||||
}
|
||||
|
||||
uint8_t get_space_day(uint32_t oldtime,uint32_t newtime)
|
||||
{
|
||||
uint8_t day = 0;
|
||||
if(newtime>oldtime)
|
||||
{
|
||||
day = (newtime - oldtime)/(3600*24);
|
||||
}
|
||||
return day;
|
||||
}
|
||||
|
||||
uint32_t get_curtime(uint8_t *curyear,uint8_t *curmon,uint8_t *curday)
|
||||
{
|
||||
RTC_TimeDateTypeDef rtctime;
|
||||
uint32_t time = 0;
|
||||
uint32_t year = 0;
|
||||
uint8_t mon = 0;
|
||||
uint8_t day = 0;
|
||||
rtc_getRTC(&rtctime);
|
||||
|
||||
year = bcd_to_dec(rtctime.Year);
|
||||
mon = bcd_to_dec(rtctime.Month);
|
||||
day = bcd_to_dec(rtctime.Date);
|
||||
|
||||
year = year + DEC_YEAR;
|
||||
time = mktime(year,mon,day,0,0,0);
|
||||
|
||||
*curyear = year;
|
||||
*curmon = mon;
|
||||
*curday = day;
|
||||
|
||||
#ifdef DEBUG_FILEI2C
|
||||
HT_PRINT("time = %d year = %d mon = %d day = %d\r\n",time,year,mon,day);
|
||||
#endif
|
||||
return time;
|
||||
}
|
||||
|
||||
|
||||
void get_complete_curtime(uint32_t *curyear,uint8_t *curmon,uint8_t *curday,uint8_t *curhour,uint8_t *curmin)
|
||||
{
|
||||
RTC_TimeDateTypeDef rtctime;
|
||||
uint32_t year = 0;
|
||||
uint8_t mon = 0;
|
||||
uint8_t day = 0;
|
||||
uint8_t hour = 0;
|
||||
uint8_t min = 0;
|
||||
rtc_getRTC(&rtctime);
|
||||
|
||||
year = bcd_to_dec(rtctime.Year);
|
||||
mon = bcd_to_dec(rtctime.Month);
|
||||
day = bcd_to_dec(rtctime.Date);
|
||||
hour = bcd_to_dec(rtctime.Hour);
|
||||
min = bcd_to_dec(rtctime.Minute);
|
||||
|
||||
year = year + DEC_YEAR;
|
||||
|
||||
*curyear = year;
|
||||
*curmon = mon;
|
||||
*curday = day;
|
||||
*curhour = hour;
|
||||
*curmin = min;
|
||||
}
|
||||
|
||||
236
hb/hbdatacount.c
Normal file
236
hb/hbdatacount.c
Normal file
@@ -0,0 +1,236 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
#include "hbframe.h"
|
||||
|
||||
#define DATA_INVALID 0xFF //无效
|
||||
#define DATA_VALID 0xAA //有效
|
||||
#define REPORT_SUCESS_FLAG 0xBB //上报成功
|
||||
#define REPORT_READY_FLAG 0xFF //未上报的状态
|
||||
|
||||
#define DATA_VALID_POS 0
|
||||
#define DATA_COUNT_POS 1
|
||||
#define DATA_RPFLAG_POS 3
|
||||
#define DATA_YEARFLAG_POS 4
|
||||
#define DATA_MONTHFLAG_POS 5
|
||||
#define DATA_DAYFLAG_POS 6
|
||||
#define DATA_HOURFLAG_POS 7
|
||||
#define DATA_CRCFLAG_POS 8
|
||||
#define DATA_CRC_LEN 8 //未用
|
||||
|
||||
extern bool I2C_eeprom_write_buf(u16 startAddr, u8 * buf, u16 length);
|
||||
extern bool I2C_eeprom_read_buf(u16 addr, u8 * buf, u16 length);
|
||||
|
||||
|
||||
#pragma pack(1)
|
||||
struct datacount_struct
|
||||
{
|
||||
u8 valid; //0
|
||||
u16 count; //1
|
||||
u8 rpflag; //3
|
||||
u8 year; //4
|
||||
u8 month; //5
|
||||
u8 day; //6
|
||||
u8 hour; //7
|
||||
u8 crc; //8
|
||||
};
|
||||
|
||||
#pragma pack()
|
||||
|
||||
static u16 count = 0;
|
||||
static u16 rpcount = 0;
|
||||
static u16 rphistroycount = 0;
|
||||
static u16 index_rphistroycount = 0;
|
||||
|
||||
static struct datacount_struct datacount[DATA_COUNT_MAX];
|
||||
|
||||
u16 datacount_getcurcount()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
//得到当前上报索引值
|
||||
u16 datacount_getrpcurcount()
|
||||
{
|
||||
return rpcount;
|
||||
}
|
||||
|
||||
//得到历史上报索引值
|
||||
u16 datacount_getrphistroycount()
|
||||
{
|
||||
return rphistroycount;
|
||||
}
|
||||
|
||||
//判断当前数据抄成功
|
||||
bool datacount_dlsucess(u8 year,u8 month,u8 day,u8 hour)
|
||||
{
|
||||
u16 i = 0;
|
||||
for(i=0;i<DATA_COUNT_MAX;i++)
|
||||
{
|
||||
if( (datacount[i].year == year) &&
|
||||
(datacount[i].month == month) &&
|
||||
(datacount[i].day == day) &&
|
||||
(datacount[i].hour == hour) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//得到当前上报索引值
|
||||
bool datacount_getrpcur(u8 year,u8 month,u8 day,u8 hour)
|
||||
{
|
||||
u16 i = 0;
|
||||
for(i=0;i<DATA_COUNT_MAX;i++)
|
||||
{
|
||||
if( (datacount[i].year == year) &&
|
||||
(datacount[i].month == month) &&
|
||||
(datacount[i].day == day) &&
|
||||
(datacount[i].hour == hour) )
|
||||
{
|
||||
if(datacount[i].rpflag != REPORT_SUCESS_FLAG)
|
||||
{
|
||||
rpcount = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//得到以前未上报索引值
|
||||
bool datacount_gethistroy()
|
||||
{
|
||||
u16 i = 0;
|
||||
|
||||
for(i=index_rphistroycount;i<DATA_COUNT_MAX;i++)
|
||||
{
|
||||
if(datacount[i].rpflag != REPORT_SUCESS_FLAG)
|
||||
{
|
||||
if(datacount[i].year != 0xFF && datacount[i].year >=0x21 )
|
||||
{
|
||||
if(datacount[i].month <= 0x12 && datacount[i].day <= 0x31 && datacount[i].hour <= 0x24) //数据有效
|
||||
{
|
||||
rphistroycount = i;
|
||||
index_rphistroycount++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
index_rphistroycount++;
|
||||
}
|
||||
index_rphistroycount = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void datacount_read()
|
||||
{
|
||||
u16 i = 0;
|
||||
u8 valid= 0;
|
||||
I2C_eeprom_read_buf(0,(u8*)&datacount[0],sizeof(datacount));
|
||||
for(i = 0;i<DATA_COUNT_MAX;i++)
|
||||
{
|
||||
valid = datacount[i].valid;
|
||||
if(valid == DATA_VALID)
|
||||
{
|
||||
if(i == DATA_COUNT_MAX - 1)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
count = i+1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
|
||||
void datacount_write(u16 index)
|
||||
{
|
||||
u32 addr = 0;
|
||||
addr = index * sizeof(struct datacount_struct);
|
||||
datacount[index].crc = GetSum((u8*)&datacount[index],sizeof(struct datacount_struct)-1);
|
||||
I2C_eeprom_write_buf(addr,(u8*)&datacount[index],sizeof(struct datacount_struct));
|
||||
}
|
||||
|
||||
|
||||
void datarp_sucess_write(u16 index)
|
||||
{
|
||||
datacount[index].rpflag = REPORT_SUCESS_FLAG;
|
||||
datacount_write(index);
|
||||
}
|
||||
|
||||
void addcount_addnew(u16 count,u8 year,u8 month,u8 day,u8 hour)
|
||||
{
|
||||
datacount[count].year = year;
|
||||
datacount[count].month = month;
|
||||
datacount[count].day = day;
|
||||
datacount[count].hour = hour;
|
||||
|
||||
datacount[count].valid = DATA_VALID;
|
||||
datacount[count].count = count;
|
||||
datacount[count].rpflag = REPORT_READY_FLAG;
|
||||
|
||||
//保存现在的标记
|
||||
datacount_write(count);
|
||||
}
|
||||
|
||||
void datacount_add(u8 year,u8 month,u8 day,u8 hour)
|
||||
{
|
||||
if(count<DATA_COUNT_MAX)
|
||||
{
|
||||
//保存现在的标记
|
||||
addcount_addnew(count,year,month,day,hour);
|
||||
|
||||
//清除以前的计数标志
|
||||
if(0 == count)
|
||||
{
|
||||
datacount[DATA_COUNT_MAX-1].valid = DATA_INVALID;
|
||||
datacount_write(DATA_COUNT_MAX-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
datacount[count-1].valid = DATA_INVALID;
|
||||
datacount_write(count-1);
|
||||
}
|
||||
count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
count = 0;
|
||||
//保存现在的标记
|
||||
addcount_addnew(count,year,month,day,hour);
|
||||
|
||||
//清除以前的计数标志
|
||||
datacount[DATA_COUNT_MAX-1].valid = DATA_INVALID;
|
||||
datacount_write(DATA_COUNT_MAX-1);
|
||||
}
|
||||
}
|
||||
|
||||
void datacount_printf()
|
||||
{
|
||||
printf("\r\n索引值 有效(AA有效) 计数 上报(BB上报成功) 时间\r\n");
|
||||
for(u16 i = 0;i<DATA_COUNT_MAX;i++)
|
||||
{
|
||||
printf("%04d %02x %05d %02x %02x-%02x-%02x %02x:00:00\r\n",
|
||||
i+1,
|
||||
datacount[i].valid,
|
||||
datacount[i].count,
|
||||
datacount[i].rpflag,
|
||||
datacount[i].year,datacount[i].month,datacount[i].day,datacount[i].hour);
|
||||
}
|
||||
}
|
||||
|
||||
502
hb/hbdatapro.c
Normal file
502
hb/hbdatapro.c
Normal file
@@ -0,0 +1,502 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
#include "hbframe.h"
|
||||
#include "filedata.h"
|
||||
|
||||
//7E7E //帧起始符 2
|
||||
//01 //中心站地址 1
|
||||
//1310020028 //遥测站地址 40=28 5
|
||||
//1234 //密码 (根据主站下发的变化) 2
|
||||
//32 //功能码 1
|
||||
//003C //报文上下行标志及长度 0000 上行 1000 下行 60 = 3C 2
|
||||
//02 //报文起始符 1
|
||||
//05FD //流水号 2
|
||||
//201219013000 //发报时间 6
|
||||
//F1F1 //地址标识符 2
|
||||
//1310020028 //地址 5
|
||||
//49 //遥测站分类码 1
|
||||
//F0F0 //观测时间标识符 2
|
||||
//2012190110 //观测时间 5
|
||||
//282B //取水口流量1标识符 2
|
||||
//0000000000 //取水口流量 5
|
||||
//FF012A //取水口累计流量1标识符 3
|
||||
//0002670300 //取水口累计流量 5
|
||||
//6033 //水表1剩余水量标识符 2
|
||||
//000000000000 //水表1剩余水量 6
|
||||
//4520 //遥测状态及报警息信标识符号 2
|
||||
//00000001 //遥测状态及报警信息 4
|
||||
//3812 //电压标志符 2
|
||||
//0000 //蓄电池电压 2
|
||||
//FF0208 //CSQ标志符 3
|
||||
//12 //信号强度CSQ 1
|
||||
//03 //报文结束符 1
|
||||
//0BB7 //校验 2
|
||||
|
||||
//7E 7E
|
||||
//01
|
||||
//00 00 00 00 01
|
||||
//0C 50
|
||||
//2F
|
||||
//00 08
|
||||
//02
|
||||
//00 02 12 12 07 08 56 50
|
||||
//03
|
||||
//22 8D
|
||||
|
||||
//中心站确认帧
|
||||
|
||||
//7E 7E
|
||||
//00
|
||||
//00 00 00 01 01
|
||||
//0C 51
|
||||
//32
|
||||
//80 08
|
||||
//02
|
||||
//00 00 12 12 07 08 56 50
|
||||
//04 ac a5
|
||||
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
||||
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
||||
//7E 7E 01 13 10 02 00 28 12 34 32 00 3C 02 05 FD 20 12 19 01 30 00 F1 F1 13 10 02 00 28 49 F0 F0 20 12 19 01 10 28 2B 00 00 00 00 00 FF 01 2A 00 02 67 03 00 60 33 00 00 00 00 00 00 45 20 00 00 00 01 38 12 00 00 FF 02 08 12 03 0B B7
|
||||
|
||||
//6.6.4.9 中心站查询遥测站实时数据
|
||||
//报文实例:
|
||||
//(1)取用水类
|
||||
//1)中心站下行报文
|
||||
//中心地址 01 遥测站地址 0000000001 密码 0x0c51 功能码 37 发报时间 12-12-07
|
||||
|
||||
//7E 7E
|
||||
//01
|
||||
//00 00 00 00 01
|
||||
//0C 51
|
||||
//37 ***
|
||||
//00 3C
|
||||
//02
|
||||
//00 01
|
||||
//12 12 09 10 21 07
|
||||
//F1 F1
|
||||
//00 00 00 00 01
|
||||
//50 ***
|
||||
//F0 F0
|
||||
//12 12 09 10 21
|
||||
//28 2B
|
||||
//00 00 23 99 99
|
||||
//FF 01 2A
|
||||
//00 99 99 99 99
|
||||
//60 33
|
||||
//00 00 00 20 03 50
|
||||
//45 20
|
||||
//00 00 00 03
|
||||
//38 12
|
||||
//12 80
|
||||
//FF 02 08
|
||||
//26
|
||||
//03
|
||||
//72 E4
|
||||
|
||||
//6.6.4.17 初始化固态存储数据
|
||||
|
||||
//6.6.4.18 恢复遥测站出厂设置
|
||||
|
||||
//6.6.4.19 修改密码
|
||||
|
||||
//6.6.4.20 设置遥测站时钟
|
||||
|
||||
//6.6.4.23 控制水泵开关命令/水泵状态自报
|
||||
|
||||
//140 页功能码定义
|
||||
|
||||
extern u8 csq_value;
|
||||
extern u8 * hb_getdl_time();
|
||||
extern void hb_get_time(u8 * time);
|
||||
extern u16 get_crc(u8 * val,u16 len);
|
||||
extern void hb_waterdata_read(u16 count,uint8_t * buf,uint16_t len);
|
||||
|
||||
|
||||
bool hb_data_rp_pro(u16 index,u8 * val,u16 * len)
|
||||
{
|
||||
u8 * addr = NULL;
|
||||
u8 * pval = NULL;
|
||||
u16 password = 0;
|
||||
u16 ck = 0;
|
||||
struct hb_struct * p = (struct hb_struct *)val;
|
||||
|
||||
struct waterdata_struct waterdata;
|
||||
u8 data_crc = 0;
|
||||
|
||||
hb_waterdata_read(index,(u8*)&waterdata,sizeof(struct waterdata_struct));
|
||||
|
||||
data_crc = GetSum((uint8_t*)&waterdata.invalid,WATERDATA_LEN);
|
||||
|
||||
if(waterdata.crc != data_crc)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//帧起始符
|
||||
p->head[0] = 0x7E;
|
||||
p->head[1] = 0x7E;
|
||||
|
||||
//中心站地址
|
||||
p->centeraddr = get_center_addr();
|
||||
|
||||
//遥测站地址 40=28
|
||||
addr = get_hb_addr();
|
||||
MemCpy(p->collectaddr,addr,5);
|
||||
|
||||
password = get_password();
|
||||
|
||||
//密码
|
||||
hbp2from16(p->pwd,password);
|
||||
|
||||
//功能码
|
||||
p->fn = 0x32;
|
||||
|
||||
// p->fn = HB_DATA_RP_FUN;
|
||||
|
||||
//报文上下行标志及长度
|
||||
hbp2from16(p->len,60);
|
||||
|
||||
//报文起始符 1
|
||||
p->start = 0x02;
|
||||
|
||||
//流水号 2
|
||||
hbp2from16(p->serial, get_send_serial());
|
||||
pval = (u8*)p + sizeof(struct hb_struct);
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
//发报时间 6
|
||||
hb_get_time(pval);
|
||||
pval = pval + 6;
|
||||
|
||||
//地址标识符 2
|
||||
pval[0] = 0xF1;
|
||||
pval[1] = 0xF1;
|
||||
pval = pval + 2;
|
||||
|
||||
//地址 5
|
||||
MemCpy(pval,addr,5);
|
||||
pval = pval + 5;
|
||||
|
||||
//遥测站分类码 1
|
||||
pval[0] = 0x49;
|
||||
pval = pval + 1;
|
||||
|
||||
//观测时间标识符 2
|
||||
pval[0] = 0xF0;
|
||||
pval[1] = 0xF0;
|
||||
pval = pval + 2;
|
||||
|
||||
//观测时间 5
|
||||
//MemCpy(pval,hb_getdl_time(),5);
|
||||
|
||||
|
||||
pval[0] = waterdata.year;
|
||||
pval[1] = waterdata.mon;
|
||||
pval[2] = waterdata.day;
|
||||
pval[3] = waterdata.hour;
|
||||
/*
|
||||
if(waterdata.hour == 0x00)
|
||||
{
|
||||
if(waterdata.day == 1)
|
||||
{
|
||||
if(waterdata.mon == 1)
|
||||
{
|
||||
waterdata.year = waterdata.year - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
pval[1] = waterdata.mon-1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pval[2] = waterdata.day-1;
|
||||
}
|
||||
pval[3] = 0x23;
|
||||
}
|
||||
else
|
||||
{
|
||||
pval[3] = waterdata.hour-1;
|
||||
}
|
||||
*/
|
||||
|
||||
pval[4] = 0x00;
|
||||
pval[5] = 0;
|
||||
|
||||
pval = pval + 5;
|
||||
|
||||
//取水口流量1标识符 2
|
||||
pval[0] = 0x28;
|
||||
pval[1] = 0x2B;
|
||||
pval = pval + 2;
|
||||
|
||||
//取水口流量 5
|
||||
|
||||
pval[0] = waterdata.current[0];
|
||||
pval[1] = waterdata.current[1];
|
||||
pval[2] = waterdata.current[2];
|
||||
pval[3] = waterdata.current[3];
|
||||
pval[4] = waterdata.current[4];
|
||||
pval = pval + 5;
|
||||
|
||||
//取水口累计流量1标识符 3
|
||||
pval[0] = 0xFF;
|
||||
pval[1] = 0x01;
|
||||
pval[2] = 0x2A;
|
||||
pval = pval + 3;
|
||||
|
||||
|
||||
//取水口累计流量 5
|
||||
pval[0] = waterdata.sum[0];
|
||||
pval[1] = waterdata.sum[1];
|
||||
pval[2] = waterdata.sum[2];
|
||||
pval[3] = waterdata.sum[3];
|
||||
pval[4] = waterdata.sum[4];
|
||||
pval = pval + 5;
|
||||
|
||||
//水表1剩余水量标识符 2
|
||||
pval[0] = 0x60;
|
||||
pval[1] = 0x33;
|
||||
pval = pval + 2;
|
||||
|
||||
//水表1剩余水量 6
|
||||
pval[0] = waterdata.surplus[0];
|
||||
pval[1] = waterdata.surplus[1];
|
||||
pval[2] = waterdata.surplus[2];
|
||||
pval[3] = waterdata.surplus[3];
|
||||
pval[4] = waterdata.surplus[4];
|
||||
pval[5] = waterdata.surplus[5];
|
||||
|
||||
pval = pval + 6;
|
||||
|
||||
//遥测状态及报警息信标识符号 2
|
||||
pval[0] = 0x45;
|
||||
pval[1] = 0x20;
|
||||
pval = pval + 2;
|
||||
|
||||
//遥测状态及报警信息 4
|
||||
pval[0] = waterdata.ala[0];
|
||||
pval[1] = waterdata.ala[1];
|
||||
pval[2] = waterdata.ala[2];
|
||||
pval[3] = waterdata.ala[3];
|
||||
pval = pval + 4;
|
||||
|
||||
//电压标志符 2
|
||||
pval[0] = 0x38;
|
||||
pval[1] = 0x12;
|
||||
pval = pval + 2;
|
||||
|
||||
//蓄电池电压 2
|
||||
//pval[0] = waterdata.vol[0];
|
||||
//pval[1] = waterdata.vol[1];
|
||||
|
||||
pval[0] = 0x12;
|
||||
pval[1] = HexToBCD((RTC_GetCounter()%50));
|
||||
pval = pval + 2;
|
||||
|
||||
//CSQ标志符 3
|
||||
pval[0] = 0xFF;
|
||||
pval[1] = 0x02;
|
||||
pval[2] = 0x08;
|
||||
pval = pval + 3;
|
||||
|
||||
|
||||
//信号强度CSQ 1
|
||||
pval[0] = csq_value;
|
||||
pval = pval + 1;
|
||||
|
||||
//报文结束符 1
|
||||
pval[0] = 0x03;
|
||||
pval = pval + 1;
|
||||
|
||||
ck = get_crc(val,75);
|
||||
|
||||
hbp2from16(pval,ck);
|
||||
*len = 77;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool hb_data_rp_cur_pro(u8 * val,u16 * len,u8 * currentdata,u8 * sumdata,u8 * surplus,u8 * ala,u8 year,u8 month,u8 day,u8 hour)
|
||||
{
|
||||
u8 * addr = NULL;
|
||||
u8 * pval = NULL;
|
||||
u16 password = 0;
|
||||
u16 ck = 0;
|
||||
struct hb_struct * p = (struct hb_struct *)val;
|
||||
|
||||
|
||||
//帧起始符
|
||||
p->head[0] = 0x7E;
|
||||
p->head[1] = 0x7E;
|
||||
|
||||
//中心站地址
|
||||
p->centeraddr = get_center_addr();
|
||||
|
||||
//遥测站地址 40=28
|
||||
addr = get_hb_addr();
|
||||
//addr[4] = addr[4] + 1;
|
||||
MemCpy(p->collectaddr,addr,5);
|
||||
|
||||
password = get_password();
|
||||
|
||||
//密码
|
||||
hbp2from16(p->pwd,password);
|
||||
|
||||
//功能码
|
||||
p->fn = 0x32;
|
||||
|
||||
// p->fn = HB_DATA_RP_FUN;
|
||||
|
||||
//报文上下行标志及长度
|
||||
hbp2from16(p->len,60);
|
||||
|
||||
//报文起始符 1
|
||||
p->start = 0x02;
|
||||
|
||||
//流水号 2
|
||||
hbp2from16(p->serial, get_send_serial());
|
||||
pval = (u8*)p + sizeof(struct hb_struct);
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
//发报时间 6
|
||||
hb_get_time(pval);
|
||||
pval = pval + 6;
|
||||
|
||||
//地址标识符 2
|
||||
pval[0] = 0xF1;
|
||||
pval[1] = 0xF1;
|
||||
pval = pval + 2;
|
||||
|
||||
//地址 5
|
||||
MemCpy(pval,addr,5);
|
||||
pval = pval + 5;
|
||||
|
||||
//遥测站分类码 1
|
||||
pval[0] = 0x49;
|
||||
pval = pval + 1;
|
||||
|
||||
//观测时间标识符 2
|
||||
pval[0] = 0xF0;
|
||||
pval[1] = 0xF0;
|
||||
pval = pval + 2;
|
||||
|
||||
//观测时间 5
|
||||
//MemCpy(pval,hb_getdl_time(),5);
|
||||
|
||||
|
||||
pval[0] = year;
|
||||
pval[1] = month;
|
||||
pval[2] = day;
|
||||
pval[3] = hour;
|
||||
|
||||
pval[4] = 0x00;
|
||||
pval[5] = 0;
|
||||
|
||||
pval = pval + 5;
|
||||
|
||||
//取水口流量1标识符 2
|
||||
pval[0] = 0x28;
|
||||
pval[1] = 0x2B;
|
||||
pval = pval + 2;
|
||||
|
||||
//取水口流量 5
|
||||
|
||||
pval[0] = currentdata[0];
|
||||
pval[1] = currentdata[1];
|
||||
pval[2] = currentdata[2];
|
||||
pval[3] = currentdata[3];
|
||||
pval[4] = currentdata[4];
|
||||
pval = pval + 5;
|
||||
|
||||
//取水口累计流量1标识符 3
|
||||
pval[0] = 0xFF;
|
||||
pval[1] = 0x01;
|
||||
pval[2] = 0x2A;
|
||||
pval = pval + 3;
|
||||
|
||||
|
||||
//取水口累计流量 5
|
||||
pval[0] = sumdata[0];
|
||||
pval[1] = sumdata[1];
|
||||
pval[2] = sumdata[2];
|
||||
pval[3] = sumdata[3];
|
||||
pval[4] = sumdata[4];
|
||||
pval = pval + 5;
|
||||
|
||||
//水表1剩余水量标识符 2
|
||||
pval[0] = 0x60;
|
||||
pval[1] = 0x33;
|
||||
pval = pval + 2;
|
||||
|
||||
//水表1剩余水量 6
|
||||
pval[0] = surplus[0];
|
||||
pval[1] = surplus[1];
|
||||
pval[2] = surplus[2];
|
||||
pval[3] = surplus[3];
|
||||
pval[4] = surplus[4];
|
||||
pval[5] = surplus[5];
|
||||
|
||||
pval = pval + 6;
|
||||
|
||||
//遥测状态及报警息信标识符号 2
|
||||
pval[0] = 0x45;
|
||||
pval[1] = 0x20;
|
||||
pval = pval + 2;
|
||||
|
||||
//遥测状态及报警信息 4
|
||||
pval[0] = ala[0];
|
||||
pval[1] = ala[1];
|
||||
pval[2] = ala[2];
|
||||
pval[3] = ala[3];
|
||||
pval = pval + 4;
|
||||
|
||||
//电压标志符 2
|
||||
pval[0] = 0x38;
|
||||
pval[1] = 0x12;
|
||||
pval = pval + 2;
|
||||
|
||||
//蓄电池电压 2
|
||||
pval[0] = 0x12;
|
||||
pval[1] = HexToBCD((RTC_GetCounter()%50));
|
||||
pval = pval + 2;
|
||||
|
||||
//CSQ标志符 3
|
||||
pval[0] = 0xFF;
|
||||
pval[1] = 0x02;
|
||||
pval[2] = 0x08;
|
||||
pval = pval + 3;
|
||||
|
||||
|
||||
//信号强度CSQ 1
|
||||
pval[0] = csq_value;
|
||||
pval = pval + 1;
|
||||
|
||||
//报文结束符 1
|
||||
pval[0] = 0x03;
|
||||
pval = pval + 1;
|
||||
|
||||
ck = get_crc(val,75);
|
||||
|
||||
hbp2from16(pval,ck);
|
||||
*len = 77;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
318
hb/hbframe.c
Normal file
318
hb/hbframe.c
Normal file
@@ -0,0 +1,318 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
#include "hbframe.h"
|
||||
|
||||
|
||||
|
||||
extern void gprs_recv_judge_timeset();
|
||||
extern u8 * get_uart_recv();
|
||||
extern u8 * get_uart_send();
|
||||
extern u16 MODBUS_CRC16(u8 *updata, u16 len) ;
|
||||
extern u16 get_crc(u8 * val,u16 len);
|
||||
extern u8 * addr_get();
|
||||
extern void rpprocess_recv_ack();
|
||||
|
||||
const u8 tmp[] = {0x7E,0x7E,0x01,0x13,0x10,0x02,0x00,0x28,0x12,0x34,0x32,0x00,0x3C,0x02,0x05,0xFD,0x20,0x12,0x19,0x01,0x30,0x00,0xF1,0xF1,0x13,0x10,0x02,0x00,0x28,0x49,0xF0,0xF0,0x20,0x12,0x19,0x01,0x10,0x28,0x2B,0x00,0x00,0x00,0x00,0x00,0xFF,0x01,0x2A,0x00,0x02,0x67,0x03,0x00,0x60,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x45,0x20,0x00,0x00,0x00,0x01,0x38,0x12,0x00,0x00,0xFF,0x02,0x08,0x12,0x03};//,0x03,0x0B,0xB7
|
||||
|
||||
u8 send_buff[512];
|
||||
u16 send_buff_len = 0;
|
||||
u8 recv_buff[512];
|
||||
u16 recv_buff_len = 0;
|
||||
|
||||
u8 recv_ana_buff1[512];
|
||||
u16 recv_ana_buff_len1 = 0;
|
||||
|
||||
static u8 center_addr = 0x01;
|
||||
static u8 hb_addr[6];
|
||||
static u8 hb_dl_time[6];
|
||||
static u16 password = 0x1234;
|
||||
|
||||
static u16 recvserial = 0;
|
||||
static u16 sendserial = 1;
|
||||
|
||||
//将2个字节转换成 u16数字。
|
||||
unsigned short hbu16fromp2(unsigned char * p2)
|
||||
{
|
||||
return (((p2[0]<<8) &0xFF00) + p2[1]);
|
||||
}
|
||||
|
||||
//将u16数字转换成 2个字节。
|
||||
void hbp2from16(unsigned char * p2,unsigned short val)
|
||||
{
|
||||
p2[1] = (val&0xFF);
|
||||
p2[0] = ((val)>>8)&0xFF;
|
||||
}
|
||||
|
||||
|
||||
u16 get_send_serial()
|
||||
{
|
||||
sendserial ++;
|
||||
return sendserial;
|
||||
}
|
||||
|
||||
u16 get_recv_seral()
|
||||
{
|
||||
return recvserial;
|
||||
}
|
||||
|
||||
void set_recv_seral(u16 serial)
|
||||
{
|
||||
recvserial = serial;
|
||||
}
|
||||
|
||||
void set_dl_time()
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
void test_crc()
|
||||
{
|
||||
// u16 cc = MODBUS_CRC16((u8*)tmp,sizeof(tmp));
|
||||
u16 cc = get_crc((u8*)tmp,sizeof(tmp));
|
||||
// u8 kk = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
得到中心地址
|
||||
*/
|
||||
u8 get_center_addr()
|
||||
{
|
||||
return center_addr;
|
||||
}
|
||||
|
||||
/*
|
||||
得到集中器地址
|
||||
*/
|
||||
u8 * get_hb_addr()
|
||||
{
|
||||
u8 * addr = addr_get();
|
||||
u8 tmp[6];
|
||||
|
||||
u8 tmptmp[2] = {0};
|
||||
|
||||
MemCpy(tmp,addr,6);
|
||||
MemCpy(hb_addr,addr,6);
|
||||
|
||||
hb_addr[0] = tmp[5];
|
||||
hb_addr[1] = tmp[4];
|
||||
hb_addr[2] = tmp[3];
|
||||
hb_addr[3] = tmp[2];
|
||||
hb_addr[4] = tmp[1];
|
||||
|
||||
tmptmp[0] = BCDToHex(hb_addr[3]);
|
||||
tmptmp[1] = BCDToHex(hb_addr[4]);
|
||||
|
||||
u16 aa= tmptmp[0] * 100 + tmptmp[1];
|
||||
|
||||
hb_addr[3] = (aa>>8)&0xFF;
|
||||
hb_addr[4] = (aa)&0xFF;
|
||||
|
||||
return hb_addr;
|
||||
}
|
||||
|
||||
/*
|
||||
得到密码
|
||||
*/
|
||||
u16 get_password()
|
||||
{
|
||||
return password;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void hb_setdl_time()
|
||||
{
|
||||
st_rtc_ext rtc_dat = {0x00};
|
||||
|
||||
|
||||
read_rtc_time(&rtc_dat);
|
||||
|
||||
hb_dl_time[0] = rtc_dat.year;
|
||||
hb_dl_time[1] = rtc_dat.month;
|
||||
hb_dl_time[2] = rtc_dat.day;
|
||||
if(rtc_dat.hour == 0x00)
|
||||
{
|
||||
hb_dl_time[3] = 0x23;
|
||||
}
|
||||
else
|
||||
{
|
||||
hb_dl_time[3] = rtc_dat.hour-1;
|
||||
}
|
||||
hb_dl_time[4] = 0x00;
|
||||
hb_dl_time[5] = 0;
|
||||
}
|
||||
|
||||
void hb_get_time(u8 * time)
|
||||
{
|
||||
st_rtc_ext rtc_dat = {0x00};
|
||||
|
||||
read_rtc_time(&rtc_dat);
|
||||
|
||||
time[0] = rtc_dat.year;
|
||||
time[1] = rtc_dat.month;
|
||||
time[2] = rtc_dat.day;
|
||||
/*
|
||||
if(rtc_dat.hour == 0x00)
|
||||
{
|
||||
time[3] = 0x23;
|
||||
}
|
||||
else
|
||||
{
|
||||
time[3] = rtc_dat.hour;
|
||||
}
|
||||
*/
|
||||
|
||||
time[3] = rtc_dat.hour;
|
||||
time[4] = rtc_dat.min;
|
||||
time[5] = 0;
|
||||
}
|
||||
|
||||
|
||||
u8 * hb_getdl_time()
|
||||
{
|
||||
return hb_dl_time;
|
||||
}
|
||||
|
||||
|
||||
bool hb_frame_recv()
|
||||
{
|
||||
u8 * p = get_uart_recv();
|
||||
u8 * q = get_uart_send();
|
||||
|
||||
// u16 len = 0;
|
||||
u8 fn = p[10];
|
||||
|
||||
// struct hb_struct * hb = (struct hb_struct *)p;
|
||||
|
||||
|
||||
|
||||
memset(send_buff,0,sizeof(send_buff));
|
||||
|
||||
switch(fn)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
case AFN_RP_TIME: //0x32 遥测站定时报
|
||||
gprs_recv_judge_timeset();
|
||||
rpprocess_recv_ack();
|
||||
break;
|
||||
case AFN_RP_HOURTIME: //0x34 遥测站小时报
|
||||
break;
|
||||
case AFN_INIT: //0x47 初始化固态存储数据
|
||||
break;
|
||||
case AFN_RECOVER: //0x48 恢复终端出厂设置
|
||||
break;
|
||||
case AFN_MODIFY_PWD: //0x49 修改密码
|
||||
break;
|
||||
case AFN_SET_RTC: //0x4A 设置遥测站时钟
|
||||
break;
|
||||
case AFN_SET_ICCARD: //0x4B 设置遥测终端 IC 卡状态
|
||||
break;
|
||||
case AFN_CTL_SHUIBANG: //0x4C 控制水泵开关命令/水泵状态信息自报
|
||||
break;
|
||||
case AFN_CTL_FAMEN: //0x4D 控制阀门开关命令/阀门状态信息自报
|
||||
break;
|
||||
case AFN_CTL_ZAMEN: //0x4E 控制闸门开关命令/闸门状态信息自报
|
||||
break;
|
||||
case AFN_CTL_WATER_VALUE: //0x4F 水量定值控制命令
|
||||
break;
|
||||
case AFN_QRY_EVENT: //0x50 中心站查询遥测站事件记录
|
||||
break;
|
||||
case AFN_QRY_RTC: //0x51 中心站查询遥测站时钟
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void set_uart_recv(u8 * val,u16 len)
|
||||
{
|
||||
memmove(recv_buff,val,len);
|
||||
recv_buff_len = len;
|
||||
|
||||
//将接收的序列号也放这里
|
||||
|
||||
//set_recvserial(U16From2(&val[3]));
|
||||
}
|
||||
|
||||
void frame_process(u8 * val,u16 len)
|
||||
{
|
||||
|
||||
memmove(recv_buff,val,len);
|
||||
recv_buff_len = len;
|
||||
|
||||
set_uart_recv(val,len);
|
||||
|
||||
|
||||
|
||||
|
||||
u8 * p = recv_buff;
|
||||
|
||||
u16 i = 0;
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
u16 ck1 = 0;
|
||||
u16 ck2 = 0;
|
||||
if(0x7E == p[0] && 0x7E == p[1])
|
||||
{
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
||||
//7E 7E 01 00 00 00 00 01 0C 50 2F 00 08 02 00 02 12 12 07 08 56 50 03 22 8D
|
||||
|
||||
|
||||
u16 tmplen = (p[11])<<8|p[12];
|
||||
tmplen = tmplen&0xFFF;
|
||||
|
||||
tmplen = tmplen + 15+2;
|
||||
ck1 = get_crc(&p[0],tmplen-2);
|
||||
ck2 = hbu16fromp2(&p[tmplen-2]);
|
||||
|
||||
if(ck1 == ck2)
|
||||
{
|
||||
//仅仅只是替换用
|
||||
memmove(recv_ana_buff1,recv_buff,recv_buff_len);
|
||||
recv_ana_buff_len1 = recv_buff_len;
|
||||
|
||||
memmove(recv_buff,p,tmplen);
|
||||
recv_buff_len = tmplen;
|
||||
|
||||
hb_frame_recv();
|
||||
|
||||
//处理完后,恢复
|
||||
recv_buff_len = recv_ana_buff_len1;
|
||||
memmove(recv_buff,recv_ana_buff1,recv_buff_len);
|
||||
|
||||
gprs_recv_judge_timeset();
|
||||
p = p + tmplen;
|
||||
i = i + tmplen;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[frame] crc err\r\n");
|
||||
}
|
||||
|
||||
}
|
||||
i++;
|
||||
p++;
|
||||
if(i>recv_buff_len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
56
hb/hbframe.h
Normal file
56
hb/hbframe.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef _HBFRAME_H
|
||||
#define _HBFRAME_H
|
||||
|
||||
#include "Basedefine.h"
|
||||
|
||||
|
||||
#define HB_DATA_RP_FUN (0x32)
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
#define AFN_HEART 0x2F //链路维持报
|
||||
#define AFN_RP_TIME 0x32 //遥测站定时报
|
||||
#define AFN_RP_HOURTIME 0x34 //遥测站小时报
|
||||
|
||||
#define AFN_QRY_HOURTIME 0x37 //中心站查询遥测站实时数据
|
||||
|
||||
#define AFN_QRY_VER 0x45 //查询遥测终端软件版本
|
||||
|
||||
#define AFN_INIT 0x47 //初始化固态存储数据
|
||||
#define AFN_RECOVER 0x48 //恢复终端出厂设置
|
||||
#define AFN_MODIFY_PWD 0x49 //修改密码
|
||||
#define AFN_SET_RTC 0x4A //设置遥测站时钟
|
||||
#define AFN_SET_ICCARD 0x4B //设置遥测终端 IC 卡状态
|
||||
#define AFN_CTL_SHUIBANG 0x4C //控制水泵开关命令/水泵状态信息自报
|
||||
#define AFN_CTL_FAMEN 0x4D //控制阀门开关命令/阀门状态信息自报
|
||||
#define AFN_CTL_ZAMEN 0x4E //控制闸门开关命令/闸门状态信息自报
|
||||
#define AFN_CTL_WATER_VALUE 0x4F //水量定值控制命令
|
||||
|
||||
#define AFN_QRY_EVENT 0x50 //中心站查询遥测站事件记录
|
||||
#define AFN_QRY_RTC 0x51 //中心站查询遥测站时钟
|
||||
|
||||
|
||||
|
||||
struct hb_struct
|
||||
{
|
||||
u8 head[2]; //帧起始符 2
|
||||
u8 centeraddr; //中心站地址 1
|
||||
u8 collectaddr[5]; //遥测站地址 40=28 5
|
||||
u8 pwd[2]; //密码 (根据主站下发的变化) 2
|
||||
u8 fn; //功能码
|
||||
u8 len[2]; //报文上下行标志及长度 0000 上行 1000 下行 60 = 3C 2
|
||||
u8 start; //报文起始符
|
||||
u8 serial[2]; //流水号
|
||||
};
|
||||
|
||||
u8 get_center_addr();
|
||||
u8 * get_hb_addr();
|
||||
|
||||
void hbp2from16(unsigned char * p2,unsigned short val);
|
||||
u16 get_send_serial();
|
||||
u16 get_password();
|
||||
|
||||
#pragma pack()
|
||||
|
||||
#endif
|
||||
|
||||
119
hb/hbheartpro .c
Normal file
119
hb/hbheartpro .c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
#include "hbframe.h"
|
||||
|
||||
extern void gprs_recv_judge_timeset();
|
||||
|
||||
//7E7E //帧起始符 2
|
||||
//01 //中心站地址 1
|
||||
//1310020028 //遥测站地址 40=28 5
|
||||
//1234 //密码 (根据主站下发的变化) 2
|
||||
//32 //功能码 1
|
||||
//003C //报文上下行标志及长度 0000 上行 1000 下行 60 = 3C 2
|
||||
//02 //报文起始符 1
|
||||
//05FD //流水号 2
|
||||
//201219013000 //发报时间 6
|
||||
//F1F1 //地址标识符 2
|
||||
//1310020028 //地址 5
|
||||
//49 //遥测站分类码 1
|
||||
//F0F0 //观测时间标识符 2
|
||||
//2012190110 //观测时间 5
|
||||
//282B //取水口流量1标识符 2
|
||||
//0000000000 //取水口流量 5
|
||||
//FF012A //取水口累计流量1标识符 3
|
||||
//0002670300 //取水口累计流量 5
|
||||
//6033 //水表1剩余水量标识符 2
|
||||
//000000000000 //水表1剩余水量 6
|
||||
//4520 //遥测状态及报警息信标识符号 2
|
||||
//00000001 //遥测状态及报警信息 4
|
||||
//3812 //电压标志符 2
|
||||
//0000 //蓄电池电压 2
|
||||
//FF0208 //CSQ标志符 3
|
||||
//12 //信号强度CSQ 1
|
||||
//03 //报文结束符 1
|
||||
//0BB7 //校验 2
|
||||
|
||||
extern u8 csq_value;
|
||||
extern u8 * hb_getdl_time();
|
||||
extern void hb_get_time(u8 * time);
|
||||
extern u16 get_crc(u8 * val,u16 len);
|
||||
|
||||
//7E 7E
|
||||
//01
|
||||
//00 00 00 00 01
|
||||
//0C 50
|
||||
//2F
|
||||
//00 08
|
||||
//02
|
||||
//00 02
|
||||
//12 12 07 08 56 50
|
||||
//03 67 bd
|
||||
|
||||
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
||||
//7E 7E 01 00 00 00 00 01 0C 50 2F 00 08 02 00 02 12 12 07 08 56 50 03 67 bd
|
||||
|
||||
void hb_heart_rp_pro(u8 * val,u16 * len)
|
||||
{
|
||||
u8 * addr = NULL;
|
||||
u8 * pval = NULL;
|
||||
u16 password = 0;
|
||||
u16 ck = 0;
|
||||
struct hb_struct * p = (struct hb_struct *)val;
|
||||
|
||||
//帧起始符
|
||||
p->head[0] = 0x7E;
|
||||
p->head[1] = 0x7E;
|
||||
|
||||
//中心站地址
|
||||
p->centeraddr = get_center_addr();
|
||||
|
||||
//遥测站地址 40=28
|
||||
addr = get_hb_addr();
|
||||
MemCpy(p->collectaddr,addr,5);
|
||||
|
||||
password = get_password();
|
||||
|
||||
//密码
|
||||
hbp2from16(p->pwd,password);
|
||||
|
||||
//功能码
|
||||
p->fn = AFN_HEART;
|
||||
|
||||
//报文上下行标志及长度
|
||||
hbp2from16(p->len,8);
|
||||
|
||||
//报文起始符 1
|
||||
p->start = 0x02;
|
||||
|
||||
//流水号 2
|
||||
hbp2from16(p->serial, get_send_serial());
|
||||
pval = (u8*)p + sizeof(struct hb_struct);
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
|
||||
//发报时间 6
|
||||
hb_get_time(pval);
|
||||
pval = pval + 6;
|
||||
|
||||
|
||||
//报文结束符 1
|
||||
pval[0] = 0x03;
|
||||
pval = pval + 1;
|
||||
|
||||
ck = get_crc(val,23);
|
||||
|
||||
hbp2from16(pval,ck);
|
||||
*len = 25;
|
||||
|
||||
}
|
||||
|
||||
|
||||
131
hb/hbtimeproc.c
Normal file
131
hb/hbtimeproc.c
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "uart.h"
|
||||
#include "addr.h"
|
||||
#include "debug_printf.h"
|
||||
#include "bl24c512.h"
|
||||
#include "WaterMetermanager.h"
|
||||
#include "Led.h"
|
||||
#include "keywd.h"
|
||||
#include "update.h"
|
||||
#include "Flash.h"
|
||||
#include "PHY.h"
|
||||
#include "rtc_ext.h"
|
||||
#include "hbframe.h"
|
||||
|
||||
extern st_rtc_ext ERtctime;
|
||||
extern void dl_485_start(u8 type,u8 *addr, u8 addrlen);
|
||||
extern bool datacount_dlsucess(u8 year,u8 month,u8 day,u8 hour);
|
||||
|
||||
static bool is_dl = false;
|
||||
static u32 dl_count = 0;
|
||||
|
||||
static bool is_rpcur = false;
|
||||
static u32 rpcur_count = 0;
|
||||
|
||||
static bool is_rphistroy = false;
|
||||
static u32 rphistroy_count = 0;
|
||||
|
||||
#define MINUTE_RP1 (0x20)
|
||||
#define MINUTE_RP2 (MINUTE_RP1 + 21)
|
||||
#define MINUTE_RP3 (MINUTE_RP1 + 22)
|
||||
#define MINUTE_RP4 (MINUTE_RP1 + 33)
|
||||
#define MINUTE_RP5 (MINUTE_RP1 + 34)
|
||||
#define MINUTE_RP6 (MINUTE_RP1 + 45)
|
||||
#define MINUTE_RP7 (MINUTE_RP1 + 46)
|
||||
#define MINUTE_RP8 (MINUTE_RP1 + 47)
|
||||
#define MINUTE_RP9 (MINUTE_RP1 + 48)
|
||||
|
||||
|
||||
|
||||
extern u16 datacount_getrpcurcount();
|
||||
extern u16 datacount_getrphistroycount();
|
||||
extern bool datacount_dlsucess(u8 year,u8 month,u8 day,u8 hour);
|
||||
extern bool datacount_getrpcur(u8 year,u8 month,u8 day,u8 hour);
|
||||
extern bool datacount_gethistroy();
|
||||
extern void rp_setcount(u16 count);
|
||||
extern void rp_start();
|
||||
|
||||
void hb_timeproc()
|
||||
{
|
||||
//抄表 抄三次
|
||||
//抄表保存完成后,立即上报!!!
|
||||
if((ERtctime.min == 0x01) || (ERtctime.min == 0x05) || (ERtctime.min == 0x10))
|
||||
{
|
||||
is_dl = true;
|
||||
}
|
||||
|
||||
|
||||
if(is_dl)
|
||||
{
|
||||
dl_count++;
|
||||
if(dl_count>70)
|
||||
{
|
||||
is_dl = false;
|
||||
dl_count = 0;
|
||||
//如果当前抄表失败
|
||||
if(!datacount_dlsucess(ERtctime.year,ERtctime.month,ERtctime.day,ERtctime.hour))
|
||||
{
|
||||
dl_485_start(1,NULL,0);
|
||||
printf("\r\n[DL] start\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\r\n[DL] no num\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//上报当前
|
||||
if((ERtctime.min == 0x15))
|
||||
{
|
||||
is_rpcur = true;
|
||||
}
|
||||
|
||||
if(is_rpcur)
|
||||
{
|
||||
rpcur_count++;
|
||||
if(rpcur_count>70)
|
||||
{
|
||||
is_rpcur = false;
|
||||
rpcur_count = 0;
|
||||
//得到当前上报未成功
|
||||
if(datacount_getrpcur(ERtctime.year,ERtctime.month,ERtctime.day,ERtctime.hour))
|
||||
{
|
||||
rp_setcount(datacount_getrpcurcount());
|
||||
rp_start();
|
||||
printf("\r\n[RP] current\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\r\n[RP] no current\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//上报历史
|
||||
if((ERtctime.min >= 0x20 && ERtctime.min <0x50) && (is_rphistroy == false))
|
||||
{
|
||||
is_rphistroy = true;
|
||||
}
|
||||
|
||||
if(is_rphistroy)
|
||||
{
|
||||
rphistroy_count++;
|
||||
if(rphistroy_count>10)
|
||||
{
|
||||
is_rphistroy = false;
|
||||
rphistroy_count = 0;
|
||||
//遍历上报未成功
|
||||
if(datacount_gethistroy())
|
||||
{
|
||||
rp_setcount(datacount_getrphistroycount());
|
||||
rp_start();
|
||||
printf("\r\n[RP] histroy\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
//printf("\r\n[RP] no histroy\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user