打包函数基本正常工作

tcp函数基本正常工作
This commit is contained in:
CSSC-WORK\murmur 2023-06-16 11:18:52 +08:00
parent 16d3e235db
commit 5ba77d20e5
11 changed files with 378 additions and 107 deletions

5
applications/TODO.md Normal file
View File

@ -0,0 +1,5 @@
- 完成TT发送数据
- 解决打包文件错误问题
- 定时发送数据
- TT接收数据
- 完成开窗功能

View File

@ -21,6 +21,7 @@
#include <ttTR.h>
static struct rt_event sw_check;//软件条件
static struct rt_event hw_check;//硬件条件
struct rt_event tcp_chk;//tcp条件
//#define FILE_IS_OK 1
//#define TIMER_IS_OUT 1<<1
//#define TT_IS_OK 1<<2
@ -34,7 +35,9 @@ static void initEvent(void)
/* 事 件 控 制 块 */
rt_err_t result = rt_event_init(&sw_check, "SHcheck", RT_IPC_FLAG_PRIO);
result = rt_event_init(&hw_check, "HWcheck", RT_IPC_FLAG_PRIO);
result = rt_event_init(&hw_check, "HWcheck", RT_IPC_FLAG_PRIO) | result;
result = rt_event_init(&tcp_chk, "tcpTT", RT_IPC_FLAG_PRIO) | result;
if (result != RT_EOK)
{
LOG_E("init event failed.\n");
@ -132,7 +135,7 @@ static void getFileData_thread_entry(void)
{
//get file to be send
char *f="2023_05_22_11_27_12_245.txt";
char *f="2023_06_16_10_28_00_123.bin";
// char *f="1023_05_19_15_29_59_254.txt";
// pack file
@ -143,20 +146,23 @@ static void getFileData_thread_entry(void)
#ifdef __PCK
// LOG_D("%p--%p",d,s);
// LOG_I("pack %s ...",f);
static MSG cfg;
rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间
// static MSG cfg;
// rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间
len = pack_File(f, 0, d, s);
rt_kprintf("len is %d\n", len);
if (len)
{
for (size_t var = 0; var < len; var++) {
LOG_HEX("d",27,d[var],s[var]);
// sendMsg(d[var], s[var]);
}
sendMsgs(d, s, len);
LOG_I("Done.");
}
if (len && (tcpInit() == RT_EOK))
{
// rt_event_send(&tcp_chk, TT_TCP_OK);
for (size_t var = 0; var < len; var++)
{
sendMsg(d[var], s[var]);
LOG_D("i = %d",var);
LOG_HEX("d", 27, d[var], s[var]);
}
tcpClose();
LOG_I("Done.");
}
#else

47
applications/cfg.ini Normal file
View File

@ -0,0 +1,47 @@
[config]
# V1.4
#发送间隔M以零点为基准时刻
sendInterval=60
# 用于保存串口数据的单文件最大值Byte
maxSizePerFile=4096
# 最长等待数据时间,超时后将已收数据存盘
MaxTimeoutToSave=20
# 最小文件大小,超过此大小则进入发送流程
minSizeToSend=4096
# 最小等待时间S超时后进入待机模式
minSecToSleep=30
# TT最长等待激活时间M超时后重启
maxTTWaitTime=4
# TT最大重试次数
maxTTRetryCnt=3
# TT最低信号值
minTTsinal=5
# TT状态检测周期数
minTTPeriCnt=5
compressType=1
encrytType=1
# 开窗时间UTC+8,持续时间H。支持多组组与组之间采用【;】为分隔符
openWindowTime=9,1;15;1
[stats]
# 继电器开关次数影响继电器寿命10W次
swCnt=12
# 第一次上电就满足硬件条件的次数,
oneShotCnt=0
# 重启次数
bootCnt=13
# 发送总次数,由软件条件触发
sendCnt=0
# TT初始化失败次数由MaxRetryCnt次失败后触发
errCnt=0
# 最短激活时间
minActiveTime=0
# 最长激活时间
maxActiveTime=0
# 平均激活时间,暂不考虑实现
meanActiveTime=0
[tosend]
# 文件名=待发包。值为0表示全发。
2023_05_22_11_27_12_245.txt=12
1023_05_19_15_29_59_254.txt=1
123_00_00_00_00_00_00.txt=0

View File

@ -25,9 +25,9 @@ void pfdemo(void)
// #define cmb_println(...) rt_kprintf(__VA_ARGS__);rt_kprintf("\r\n");
// char *f = "1023_05_19_15_29_59_254.txt";
// char *f = "2023_05_19_15_29_59_255.txt";
char *f = "2023_05_22_11_27_12_245.txt";
static rt_uint8_t d[10][200] = { };
static rt_uint8_t s[10] = { };
char *f = "2023_06_16_10_28_00_123.bin";
static rt_uint8_t d[50][200] = { };
static rt_uint8_t s[50] = { };
rt_uint8_t len = 0;
LOG_D("%p--%p",d,s);
LOG_I("pack %s ...",f);
@ -35,7 +35,7 @@ void pfdemo(void)
rt_kprintf("len is %d\n", len);
if (len)
{
for (size_t var = 0; var < len; ++var) {
for (size_t var = 0; var < len; var++) {
LOG_HEX("d",27,d[var],s[var]);
}
// LOG_HEX("pkdata:",27,d[0],s[0]);

View File

@ -8,19 +8,27 @@
#include <dfs_file.h>
#include <usrcfg.h>
#ifdef _CFGALL
int sendInterval =60;
int maxTTWaitTime = 4;
int maxTTRetryCnt = 3;
int minTTPeriCnt=5;
int minTTsinal=5;
/**
* HEX显示文件内容
*/
void hex()
typedef struct
{
int sendInterval;
int maxTTWaitTime;
int maxTTRetryCnt;
int minTTPeriCnt;
int minTTsinal;
} SYS_CFG;
//#define _CFGALL
#ifdef _CFGALL
SYS_CFG scfg={
.sendInterval =60,
.maxTTWaitTime = 4,
.maxTTRetryCnt = 3,
.minTTPeriCnt=5,
.minTTsinal=5,
};
}
/**
*
@ -30,17 +38,119 @@ void updatecfg(void)
//因为不知原因,采用事件集独立更新配置出错
//独立响应单个参数更新事件,程序上更复杂也没特别必要
//现采用事件通知、统一全部重新加载
while(1)
{
maxTTWaitTime = get_cfg("maxTTWaitTime");
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
static int minTTPeriCnt=5;
static int minTTsinal=5;
}
while(1)
{
maxTTWaitTime = get_cfg("maxTTWaitTime");
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
minTTPeriCnt = get_cfg("minTTPeriCnt");
minTTsinal = get_cfg("minTTsinal");
}
}
#endif
/**
* HEX显示文件内容
*/
void hexFile_thread_entry(void* parameter)
{
int fd_in = -1;
int block_size = 0;
char *fin = (char *)parameter;
fd_in = open(fin, O_RDONLY, 0);
if (fd_in < 0)
{
rt_kprintf("[hex] open the input file : %s error!\n", fin);
return RT_ERROR;
}
size_t file_size = lseek(fd_in, 0, SEEK_END);
lseek(fd_in, 0, SEEK_SET);
rt_uint8_t *buffer = RT_NULL;
#define READ_BUFFER_SIZE 512
buffer = (rt_uint8_t *) malloc(READ_BUFFER_SIZE);
rt_kprintf("------Start------\n");
for (size_t i = 0; i < file_size; i += READ_BUFFER_SIZE)
{
if ((file_size - i) < READ_BUFFER_SIZE)
{
block_size = file_size - i;
}
else
{
block_size = READ_BUFFER_SIZE;
}
memset(buffer, 0x00, READ_BUFFER_SIZE);
read(fd_in, buffer, block_size);
LOG_HEX("hex_file:",27,buffer,block_size);
}
rt_free(buffer);
close(fd_in);
rt_kprintf("------Done.------\n");
}
void hexFile(int argc, char **argv)
{
if (argc == 2)
{
static char f[30];
rt_strcpy(f,argv[1]);
/* 创建线程 */
rt_thread_t thread = rt_thread_create("hex_file", hexFile_thread_entry, (void *) f, 1024 * 1, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'hex_file' create failure.");
return RT_ERROR;
}
}
}
MSH_CMD_EXPORT(hexFile,HEX方式显示文件内容);
/**
*
* @param str buffer
* @return
*/
int time2Str(char *str)
{
struct tm *tm, tm_tmp;
time_t now = time(RT_NULL);
time_t t = (time_t)0;
if (gettimeofday(&now, RT_NULL) >= 0)
{
// t = now.tv_sec;
}
tm = localtime_r(&t, &tm_tmp);
// static time_t now;
// static struct tm *tm, tm_tmp;
//
// now = time(RT_NULL);
// tm = gmtime_r(&now, &tm_tmp);
/* show the time format MM-DD HH:MM:SS */
size_t len = rt_snprintf(str, 20, "%04d_%02d_%02d_%02d_%02d_%02d_%03d", tm->tm_year, tm->tm_mon,
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
str[len]='\0';
return len;
}
void sDemo()
{
// extern struct rt_event sw_check;//软件条件

View File

@ -17,15 +17,16 @@
#define LOG_TAG "ttTR"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#include <usrcfg.h>
#define BUFSZ 1024
#define BUFSZ 200
#define RETRYCNT 10
#define URL "10.10.10.72"
#define URL TTIP
#define PORT 8005
int sock=0;
static int sock=0;
static void tcpClose(void)
void tcpClose(void)
{
/* 关闭这个连接 */
if (sock) {
@ -34,7 +35,7 @@ static void tcpClose(void)
}
static int tcpInit(void)
int tcpInit(void)
{
int ret;
@ -95,7 +96,31 @@ static int tcpSend(const rt_uint8_t *send_data, size_t len)
return ret;
}
static int tcpRec()
/**
* TCP数据
* @param recv_data 200
* @return -1
*/
int tcpRec(unsigned char *recv_data)
{
int ret=RT_ERROR;
/* 从sock连接中接收最大BUFSZ字节数据 */
int bytes_received = recv(sock, recv_data, BUFSZ, 0);
if (bytes_received) {
return bytes_received;
}
else {
LOG_E("received error.");
return -RT_ERROR;
}
}
/**
* TT的ack信号
* @return
*/
static int tcpAck()
{
/* 分配用于存放接收数据的缓冲 */
char *recv_data;
@ -111,7 +136,7 @@ static int tcpRec()
while (cnt--)
{
/* 从sock连接中接收最大BUFSZ - 1字节数据 */
int bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
int bytes_received = recv(sock, recv_data, BUFSZ, 0);
if (bytes_received) {
LOG_HEX("rec data",16,recv_data,bytes_received);
//check response
@ -147,10 +172,10 @@ static int tcpRec()
int sendMsg(const rt_uint8_t *msg, size_t len)
{
if (tcpInit() != RT_EOK)
{
return RT_ERROR;
}
// if (tcpInit() != RT_EOK)
// {
// return RT_ERROR;
// }
if (tcpSend(msg, len) != RT_EOK)
{
@ -158,13 +183,13 @@ int sendMsg(const rt_uint8_t *msg, size_t len)
return RT_ERROR;
}
if (tcpRec() != RT_EOK)
{
tcpClose();
return RT_ERROR;
};
// if (tcpRec() != RT_EOK)
// {
// tcpClose();
// return RT_ERROR;
// };
tcpClose();
// tcpClose();
return RT_EOK;
}
@ -178,27 +203,31 @@ int sendMsg(const rt_uint8_t *msg, size_t len)
*/
int sendMsgs(const rt_uint8_t (*msg)[200], char *s, size_t row)
{
int ret = RT_EOK;
if (tcpInit() != RT_EOK)
{
return RT_ERROR;
ret = RT_ERROR;
return ret;
}
for (size_t var = 0; var < row; var++)
{
if (tcpSend(msg[var], s[row]) != RT_EOK)
{
tcpClose();
return RT_ERROR;
ret = RT_ERROR;
break;
}
if (tcpRec() != RT_EOK)
#ifdef CHK_ACK
if (tcpAck() != RT_EOK)
{
tcpClose();
return RT_ERROR;
ret = RT_ERROR;
break;
};
#endif
}
tcpClose();
return RT_EOK;
return ret;
}

View File

@ -10,7 +10,10 @@
#ifndef APPLICATIONS_TTTR_H_
#define APPLICATIONS_TTTR_H_
int tcpInit(void);
int tcpRec(unsigned char *recv_data);
int sendMsg(const rt_uint8_t *msg, size_t len);
int sendMsgs(const rt_uint8_t (*msg)[200], char *s, size_t row);
void tcpClose(void);
#endif /* APPLICATIONS_TTTR_H_ */

View File

@ -35,6 +35,10 @@
#include <board.h>
#include <ttTR.h>
#include <cfg.h>
extern struct rt_event tcp_chk;//tcp条件
/**
* @brief
*
@ -290,8 +294,8 @@ LOG_HEX("buf",16,buf,100);
*/
rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[200], rt_uint8_t *arrLen)
{
extern MSG cfg;
// rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间
static MSG cfg;
rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间
rt_uint8_t rst = packInit(&cfg, fin, flag);
if (rst != RT_EOK)
{
@ -318,6 +322,7 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[
LOG_I("file size is %d bytes.", file_size);
lseek(fd_in, 0, SEEK_SET);
rt_uint8_t *buffer = RT_NULL;
// extern rt_uint8_t *buffer;
buffer = rt_malloc(file_size);
if (buffer == RT_NULL)
{
@ -377,11 +382,11 @@ static int pf_thread_entry(void* parameter)
char *f = (char *) parameter;
// static char d[5][200];
// static char s[5];
rt_uint8_t *buffer = RT_NULL;
static MSG cfg;
// rt_uint8_t *buffer = RT_NULL;
// static MSG cfg;
static rt_uint8_t len=0;
// len = pack_File(f, 0, d, s);
#define DEMO
len = pack_File(f, 0, d, s);
//#define DEMO
#ifdef DEMO
{
//
@ -459,15 +464,16 @@ static int pf_thread_entry(void* parameter)
#endif
rt_kprintf("len is %d\n", len);
if (len)
if (len && (tcpInit() == RT_EOK))
{
// rt_event_send(&tcp_chk, TT_TCP_OK);
for (size_t var = 0; var < len; var++)
{
sendMsg(d[var], s[var]);
LOG_D("i = %d",var);
LOG_HEX("d", 27, d[var], s[var]);
}
// sendMsgs(d, s, len);
tcpClose();
LOG_I("Done.");
}
return RT_EOK;
@ -512,7 +518,7 @@ MSH_CMD_EXPORT(pmsg, 打包文件。);
void pwTT_thread_entry(void *parameter)
{
// LOG_I("--%s--",parameter);
if (strcmp(parameter,"NULL")==0)
if (strcmp(parameter,"NULL")==0)//无参数
{
// LOG_I("null");
int f = rt_pin_read(TT_EN);
@ -521,11 +527,13 @@ void pwTT_thread_entry(void *parameter)
else
{
rt_bool_t flag = (rt_bool_t) atoi(parameter);
// LOG_I("flag is %d",flag);
rt_pin_write(TT_EN, flag);
if ((rt_bool_t)rt_pin_read(TT_EN) != flag) {//目标状态与当前状态一致才响应
rt_pin_write(TT_EN, flag);
}
}
if (rt_pin_read(TT_EN))
if (rt_pin_read(TT_EN))//检查设置后状态
{
//#ifdef NO_RELAY
LOG_I("set TT %s", "OFF");
@ -565,3 +573,36 @@ static int swTT(int argc, char **argv)
}
MSH_CMD_EXPORT(swTT, TT电源);
void recTT_thread_entry()
{
if (rt_event_recv(&tcp_chk, TT_TCP_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, RT_NULL) == RT_EOK)
{
while (1)
{
rt_uint8_t recv_data[200];
size_t size = tcpRec(recv_data);
LOG_HEX("tcpTT", 16, recv_data, size);
}
}
}
//INIT_COMPONENT_EXPORT(recTT_thread_entry);
static void recTT(void)
{
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 12, 27, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'recTT' create failure.");
return;
}
}

View File

@ -40,6 +40,21 @@ static rt_device_t serial;
/* 消息队列控制块 */
static struct rt_messagequeue rx_mq;
/* 定时器的控制块 */
static rt_timer_t timer1;
/* 定时器1超时函数 */
static void timeout1(void *parameter)
{
// rt_kprintf("periodic timer is timeout %d\n", cnt);
// rt_timer_stop(timer1);
LOG_W("超时未收到新数据。");
}
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
@ -58,55 +73,63 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
}
struct rx_msg msg;
extern struct rt_event update_cfg;
//static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
extern int time2Str(char *str);
static void serial_thread_entry(void *parameter)
{
// struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
rt_uint32_t rx_length=0;//单次收到的数据长度
static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
static currLen=0;//已收数据长度
static char f[30];
time2Str(f);
while (1)
{
// rt_memset(&cfg, 0, sizeof(cfg));
//暂定逻辑
//先创建文件
//接收数据并存盘
//
rt_memset(&msg, 0, sizeof(msg));
/* 从消息队列中读取消息*/
// rt_uint32_t e;
// rt_err_t result = rt_event_recv(&update_cfg, CFGCHANGEED|MAXSIZEPERFILE, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, &e);
// if (result == RT_EOK)
// {
// LOG_I("updatecfg");
// }
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
/* 从串口读取数据*/
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
rx_buffer[rx_length] = '\0';
/* 通过串口设备 serial 输出读取到的消息 */
rt_device_write(serial, 0, rx_buffer, rx_length);
int fd=0;
rx_length = rt_device_read(msg.dev, 0, rx_buffer+rx_length, msg.size);
currLen += rx_length;
/* 启动定时器1 */
if (timer1 != RT_NULL)
rt_timer_start(timer1);
fd =open("/rxdata/demo.bin",O_WRONLY | O_CREAT|O_APPEND);
if(fd <0)
{
rt_kprintf("open file failed!\r\n");
}
else
{
int count =write(fd,rx_buffer,rx_length);
close(fd);
fd =0;
}
LOG_HEX("rx:",16,rx_buffer,rx_length);//print what received.
#ifdef TR_ACK
/* 回传收到的消息 */
rt_device_write(serial, 0, rx_buffer+rx_length, rx_length);
#endif
if (currLen > 1024) //大于1k则存盘
{
/* 打印数据 */
rt_kprintf("%s\n",rx_buffer);
int fd =open(f,O_WRONLY | O_CREAT|O_APPEND);
if(fd <0)
{
LOG_E("open file %s failed!", f);
}
else
{
write(fd,rx_buffer,rx_length);
close(fd);
currLen = 0;
rx_length=0;
}
}
}
else {
// LOG_D("no data reced.");
}
rt_thread_mdelay(100);
}
}
@ -116,7 +139,7 @@ static int uart_dma_sample(int argc, char *argv[])
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[256];
char str[] = "hello RT-Thread!\r\n";
// char str[] = "hello RT-Thread!\r\n";
if (argc == 2)
{
@ -147,7 +170,13 @@ static int uart_dma_sample(int argc, char *argv[])
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
// rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 创建定时器1 周期定时器 */
timer1 = rt_timer_create("rxtimer", timeout1,
RT_NULL, 10,
RT_TIMER_FLAG_PERIODIC);
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024*1, 25+1, 10);

View File

@ -16,7 +16,8 @@
#define FILE_IS_OK 1
#define TIMER_IS_OUT 1<<1
#define TT_IS_OK 1<<2
#define TT_TCP_OK 1<<3
#define TTIP "10.10.10.72"
typedef struct
{

View File

@ -49,7 +49,7 @@
#define RT_USING_DEVICE
#define RT_USING_CONSOLE
#define RT_CONSOLEBUF_SIZE 256
#define RT_CONSOLE_DEVICE_NAME "uart3"
#define RT_CONSOLE_DEVICE_NAME "uart1"
/* end of Kernel Device Object */
#define RT_VER_NUM 0x40101
/* end of RT-Thread Kernel */