RS_485.c 更新parse算法,添加温度信息回传

alarmer.c 添加RTC启动也检查待传文件,并更新重试次数为0xFFFF
修复setManualWindow 存入-1实际写入255的bug
log2file.c 添加日期文件夹
This commit is contained in:
CSSC-WORK\murmur 2023-09-03 16:54:23 +08:00
parent 736bf46d2f
commit 841dbd5dec
5 changed files with 87 additions and 28 deletions

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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();
}
}
}

View File

@ -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]
*
* 15MOB(most oldest bit)
* 1610
@ -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);
}
/**
*

View File

@ -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()