diff --git a/applications/RS_485.c b/applications/RS_485.c index 0e7b8ad..5e3a620 100644 --- a/applications/RS_485.c +++ b/applications/RS_485.c @@ -55,8 +55,8 @@ static rt_err_t uart485_input(rt_device_t dev, rt_size_t size) static rt_size_t cachecnt=0; cachecnt += size; - if (cachecnt < MIN_FRAME_LEN) { - LOG_W("not enough data, cached and waiting...");//处理半包 + if (cachecnt < 63) { +// LOG_W("not enough data[%d byte(s)], cached and waiting...",size);//处理半包 return -1; } msg.dev = dev; @@ -84,6 +84,13 @@ typedef struct{ uint8_t temp2[2]; uint8_t temp3[2]; uint8_t vol1[2]; + uint8_t vol2[2]; + uint8_t vol3[2]; + uint8_t vol4[2]; + uint8_t vol5[2]; + uint8_t vol6[2]; + uint8_t vol7[2]; + uint8_t vol8[2]; }BATT_INFO; static BATT_INFO batt; @@ -100,16 +107,20 @@ void parseBattInfo(uint8_t *din, size_t len) { // LOG_HEX("batt",16,din,len); uint8_t head[]={0x01,0x03,0x3a}; - if (rt_memcmp(din, head, sizeof(head)) != 0) { - LOG_W("answer from battery is error."); + uint8_t index[10]; + size_t rst = isInByte(din, len, head, sizeof(head), index); + if (!rst) { + LOG_W("response from battery is not correct."); + batt.level[1]=0x65;//设一个异常值便于观测101% + return; } - rt_memcpy(&batt, din+sizeof(head), sizeof(batt)); + rt_memcpy(&batt, din+index[0]+sizeof(head), sizeof(batt));//简化,只取第一组数据 // LOG_D("batt=%X,%X,%d\%",batt.level[0],batt.level[1],b2v(batt.level)); } static void dumpBattInfo() { - rt_kprintf("标称电压 %d\n",b2v(batt.norVol)); + rt_kprintf("实际电压 %d\n",b2v(batt.norVol)); rt_kprintf("电芯数量 %d\n",b2v(batt.cnt)); rt_kprintf("电量 %d\n",b2v(batt.level)); rt_kprintf("容量 %d\n",b2v(batt.Ah)); @@ -119,6 +130,13 @@ static void dumpBattInfo() rt_kprintf("温度2 %d\n",b2v(batt.temp2)); rt_kprintf("温度3 %d\n",b2v(batt.temp3)); rt_kprintf("电芯 1电压 %d\n",b2v(batt.vol1)); + rt_kprintf("电芯 2电压 %d\n",b2v(batt.vol2)); + rt_kprintf("电芯 3电压 %d\n",b2v(batt.vol3)); + rt_kprintf("电芯 4电压 %d\n",b2v(batt.vol4)); + rt_kprintf("电芯 5电压 %d\n",b2v(batt.vol5)); + rt_kprintf("电芯 6电压 %d\n",b2v(batt.vol6)); + rt_kprintf("电芯 7电压 %d\n",b2v(batt.vol7)); + rt_kprintf("电芯 8电压 %d\n",b2v(batt.vol8)); } /** * 获取电池电量 @@ -128,6 +146,20 @@ uint8_t getPowerLevel(void) { return b2v(batt.level); } +/** + * 获取电池信息,目前仅返回电量和板卡温度 + * @param dout 返回结果数组 + * @return 数组长度 + */ +uint8_t getBattInfo(uint8_t *dout) +{ + size_t p=0; + memcpy(dout+p,batt.level,2); + p += 2; + memcpy(dout+p,batt.temp3,2); + p += 2; + return p; +} static void serial485_thread_entry(void *parameter) { @@ -180,7 +212,7 @@ static int uart485_dma_sample(int argc, char *argv[]) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; - static char msg_pool[256]; + static char msg_pool[1024]; // char str[] = {0x01,0x03,0x00,0x00,0x00,0x1d,0x85,0xc3}; if (argc == 2) @@ -224,7 +256,7 @@ static int uart485_dma_sample(int argc, char *argv[]) /* 发送字符串 */ /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("RS485", serial485_thread_entry, RT_NULL, 1024*5, 30, 10); + rt_thread_t thread = rt_thread_create("RS485", serial485_thread_entry, RT_NULL, 1024*5, 24, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { diff --git a/applications/alarmer.c b/applications/alarmer.c index 4ad3430..35c6623 100644 --- a/applications/alarmer.c +++ b/applications/alarmer.c @@ -65,6 +65,7 @@ MSH_CMD_EXPORT(alarm_sample,alarm sample); void poTT_callback(rt_alarm_t alarm, time_t timestamp) { LOG_I("power UP TT by RTC."); + timerIsReady(); setWindowMode(); initTT(); } @@ -73,6 +74,7 @@ void pdTT_callback(rt_alarm_t alarm, time_t timestamp) LOG_I("power DOWN TT by RTC."); clearWindowMode(); deInitTT(); + //预置开窗会更新多次尝试逻辑,未激活时会始终尝试 } void poTT2_callback(rt_alarm_t alarm, time_t timestamp) { diff --git a/applications/core.c b/applications/core.c index f4e29a4..ab722d5 100644 --- a/applications/core.c +++ b/applications/core.c @@ -263,7 +263,7 @@ void sysInit(void) void timerIsReady(void) { rt_event_send(&chkSta, TIMER_IS_OUT); - stopTM(); + stopTM();//停止而不是重置,避免超时时间小于未激活重试时间的情况下意外关机。 } void fileIsReady(void) { @@ -532,9 +532,13 @@ static void chkAndSendFile_thread_entry() initTT(); } - for (size_t var = 0; var < scfg.maxTTRetryCnt; var++) //轮询尝试 + size_t maxCnt = 0xffff; + if (!isWindowMode && !isManualWindow()) { + maxCnt = scfg.maxTTRetryCnt; + } + for (size_t var = 0; var < maxCnt; var++) //轮询尝试 { - LOG_I("第%d/%d次尝试。", var + 1,scfg.maxTTRetryCnt); + LOG_I("第%d/%d次尝试。", var + 1,maxCnt); int rst = rt_event_recv(&chkSta, TT_IS_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(scfg.maxTTWaitTime*60*1000), RT_NULL); // rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000)); @@ -546,7 +550,7 @@ static void chkAndSendFile_thread_entry() } else if (rst == -RT_ETIMEOUT)//超时则重试 { - if ((var+1) < scfg.maxTTRetryCnt) + if ((var+1) < maxCnt) { LOG_W("第%d次尝试中TT准备失败,重试。", var + 1); deInitTT(); @@ -555,6 +559,7 @@ static void chkAndSendFile_thread_entry() } else { LOG_E("TT准备失败"); + deInitTT(); } } } diff --git a/applications/func/func.c b/applications/func/func.c index e3b4ce0..0bc314e 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -380,7 +380,7 @@ void d_sw(void) * 存储手动开窗状态,便于意外重启后恢复状态 * @param isManualWindow 负数代表无,0代表手动开窗不自动关,整数代表自动关窗超时时间 */ -void setManualWindow(uint8_t isManualWindow) +void setManualWindow(int isManualWindow) { set_cfg("isMaWin", isManualWindow); } @@ -637,7 +637,7 @@ void reportSysCfg() //单次最多可上报15条位置信息 /** - * 按15条经纬度打包位置信息。由[是否越界,电池电量]+[时戳]+[15组数据]组成。 + * 按15条经纬度打包位置信息。由[是否越界]+[时戳]+[15组数据]组成。 * 是否越界为预留,不在此判断。 * 15组数据采集顺序为MOB(most oldest bit),越新的数据离时戳越近 * 由于加密后数据最大会增加16字节,后位置数据条数改为10 @@ -651,12 +651,19 @@ static int packLocMsg(uint8_t *dout) { 0x5A, 0xA5, ADDR_ANJI, ADDR_TT, _CFG_LOCATION_ALERT >> 8, _CFG_LOCATION_ALERT & 0xFF, 0, 0 }; - alertMsg[8] = getPowerLevel(); //首字节最高bit为1时为在围栏外 - int len = time2Byte(alertMsg+9);//添加时间戳 - len += getAndCheckLoc(alertMsg + 9 + len, _LOC_CNT) + 1; //p指向第2个数据 - if (len == 0x08) {//无有效位置数据 + alertMsg[8] = 0; //首字节00为定时发送,未检测围栏;其它为在围栏外 + int len = time2Byte(alertMsg+9) + 1;//添加时间戳,len为数据长度 + size_t rst = getAndCheckLoc(alertMsg + 8 + len, _LOC_CNT); + if (!rst) {//无有效位置数据 return 0; } + len += rst; + + //add battery info + uint8_t temp[60]; + rst = getBattInfo(temp); + memcpy(alertMsg+8+len,temp,rst); + len += rst; alertMsg[7] = len; //update len of raw data alertMsg[8 + len] = bccCRC(alertMsg + 2, 8 + len - 2); //update CRC @@ -672,8 +679,8 @@ static void d_packLocMsg(void) uint8_t tmp[200]; size_t len = packLocMsg(tmp); // len = cryptSingleMsg(tmp, len, tmp); -// LOG_HEX("c",16,tmp,len); - upSend(tmp, len); + LOG_HEX("c",16,tmp,len); +// upSend(tmp, len); } /** * 获取信号信息,以时戳开头 diff --git a/applications/log2file.c b/applications/log2file.c index c69e38f..59f97a3 100644 --- a/applications/log2file.c +++ b/applications/log2file.c @@ -35,12 +35,12 @@ typedef enum }ulog_file_be_name; #define ROOT_PATH ROOT_PATH_DEBUG //设置保存路径 -#define FILE_SIZE 10*1024 //设置单个文件大小 +#define FILE_SIZE 100*1024 //设置单个文件大小 #define BUFF_SIZE 1*1024 //设备缓存区大小 static struct ulog_backend sys_log_backend; static struct ulog_file_be sys_log_file; -char logfilename[30];// = "23_08_27"; + extern char *getTimestmp(char *str); static struct _log_file table[] = @@ -77,21 +77,34 @@ static rt_bool_t sys_log_file_backend_filter(struct ulog_backend *backend, rt_ui * @retval None. * @note None. */ -static char tmstr[30];//定义时间前缀 void sys_log_file_backend_init(void) { static struct ulog_file_be *file_be = &sys_log_file; uint8_t id = sys_id; file_be->parent = sys_log_backend; + char logfilename[30];// = "2023-08-27/2023-08-27#11-"; + char tmstr[50];//定义时间前缀 - getTimestmp(tmstr); - strncpy(logfilename,tmstr,14); + getTimestmp(tmstr);// 2023-08-27 11-44-42-65 + tmstr[10]='#';//2023-08-27#11- tmstr[14]='\0'; - tmstr[10]='#'; + + //以天为独立文件夹 + strncpy(logfilename,tmstr,10); + char dir[30]=ROOT_PATH; + strcat(dir,"/"); + strcat(dir,logfilename); + mkdir(dir, 0); + strcat(logfilename,"/"); + + //总路径不能太长 + strcat(logfilename,tmstr+11); + + ulog_file_backend_init( file_be, - strcat(tmstr,table[id].name), + strcat(logfilename,table[id].name), table[id].dir_path, table[id].max_num, table[id].max_size, @@ -101,7 +114,7 @@ void sys_log_file_backend_init(void) //新增代码 ulog_backend_filter_t filter = sys_log_file_backend_filter; - ulog_backend_set_filter(&file_be->parent,filter); +// ulog_backend_set_filter(&file_be->parent,filter); } void updateLogFileBackend()