From e8a8606bf3663c8304b36d10b81a675a4fd2f2c2 Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Thu, 20 Jul 2023 15:29:37 +0800 Subject: [PATCH] =?UTF-8?q?func.c=20=E6=9B=B4=E6=96=B0upSend=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=B1=8F=E8=94=BD=E6=89=93=E5=8C=85=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20tools.c=20=E6=9B=B4=E6=96=B0time2Str=E5=87=BD?= =?UTF-8?q?=E6=95=B0=20=E6=B7=BB=E5=8A=A0isInByte=E5=87=BD=E6=95=B0=20ttms?= =?UTF-8?q?g.c=20=E5=B1=8F=E8=94=BD=E5=A4=9A=E5=A4=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=B0=83=E7=94=A8tcpInit()=EF=BC=8C=E6=9B=B4=E6=96=B0=E5=8C=B9?= =?UTF-8?q?=E9=85=8Dack=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/_main.c | 53 ++++++++++++++++++++++++++---- applications/func/func.c | 35 ++++++++++++++++---- applications/main.c | 6 +++- applications/tools.c | 31 ++++++++++++++++-- applications/ttTR.c | 66 ++++++++++++++++++++------------------ applications/ttmsg/ttmsg.c | 36 ++++++++++++--------- 6 files changed, 162 insertions(+), 65 deletions(-) diff --git a/applications/_main.c b/applications/_main.c index 4b7e2d5..b583cb2 100644 --- a/applications/_main.c +++ b/applications/_main.c @@ -78,12 +78,12 @@ static void getFileData_thread_entry(void) static rt_uint8_t d[10][200] = { }; static rt_uint8_t s[10] = { }; - while (tcpInit() != RT_EOK) - { - LOG_W("TT server is not ready."); - rt_thread_mdelay(100); - } - LOG_D("TT server is ready."); +// while (tcpInit() != RT_EOK) +// { +// LOG_W("TT server is not ready."); +// rt_thread_mdelay(100); +// } +// LOG_D("TT server is ready."); while (1) { @@ -131,6 +131,7 @@ static void gg(void) } } + static void try() { static rt_err_t rst = RT_ERROR; @@ -209,7 +210,7 @@ static void try() } } -MSH_CMD_EXPORT(gg, file); + void smsg() { @@ -227,4 +228,42 @@ void smsg() } } //INIT_COMPONENT_EXPORT(smsg); + + +//tcp连接保活 +//实际场景不存在中间断掉的可能 +void iniCnt() +{ + rt_hw_stm32_eth_init(); //激活网口 + rt_thread_mdelay(5000); + + while (1 && !isTCPok()) //判断TCP连接是否正常,异常自动重连 + { +// while (!isTCPok()) + { + LOG_W("TT server is not ready."); + tcpInit(); + LOG_D("s=%d",isTCPok()); + rt_thread_mdelay(5000); + } + { + LOG_D("TT server is ready."); + tcpRecMQ(); + recTT(); + rt_thread_mdelay(1000); + } + + } +} +void deiniCnt() +{ + rt_hw_stm32_eth_deinit(); //qu激活网口 +} + +#define FUNC_DEMO +#ifdef FUNC_DEMO //测试时导出命令到控制台 MSH_CMD_EXPORT(smsg, smsg); +MSH_CMD_EXPORT(gg, file); +MSH_CMD_EXPORT(iniCnt,初始化tcp连接); +MSH_CMD_EXPORT(deiniCnt,初始化tcp连接); +#endif diff --git a/applications/func/func.c b/applications/func/func.c index 1239e31..d3880ce 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -92,6 +92,11 @@ return 0; */ RT_WEAK int upSend(uint8_t *din, size_t len) { + //此函数有打包操作,需线程操作 + LOG_D("upsend."); + return 0; + + uint8_t dout[200]; //打包数据 static MSG cfg; @@ -100,7 +105,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len) time2Str(fin); packInit(&cfg, fin, 0); //写入配置 size_t rst = packMsg(&cfg, din, len, dout); - LOG_HEX("upSend", 16, din, rst); + LOG_HEX("upSend", 16, dout, rst); return 0; } @@ -109,6 +114,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len) */ void selfTest() { + LOG_D("FUNC = selftest"); rt_uint8_t rst[100]={0x5A, 0xA5, 0x32, 0x3E, 0x0A, 0x41}; int p = 6; rt_uint8_t sysSta=1,xh=0,jh=0,commSpeed=0; @@ -498,9 +504,10 @@ void ttRunCMD(uint8_t *din, size_t len) } -RT_WEAK xpParse(uint8_t * din, size_t len) +RT_WEAK int xpParse(uint8_t * din, size_t len) { LOG_D("直接调用小彭的函数进行处理。"); + return 0; } //原计划将指令粗解析放在上位机,考虑到上位机到位时间晚,现放到MCU端 @@ -541,7 +548,7 @@ void parse3SData(uint8_t *din, size_t count) // uint8_t dout[200]; // size_t len=0; - if (din[3] == ADDR_TT)//仅给TT的消息 + if (din[2] == ADDR_TT)//仅给TT的消息 { ttRunCMD(din,count); } @@ -592,7 +599,16 @@ void parseTTData(uint8_t *din, size_t len) */ uint8_t head[]={0x88,0xAA,0xBB,0x88, 0x00,0x01, 0x00,0x22, 0x70,0x21, 0x00,0xaa, 0x00, 0x27,0x22,0x22,0x22 }; //fstart[4] fnum[2]  bak[2]  ftype[2]  fdlen[2]  fcrc[1] ftccid[4] - uint8_t rst = memcmp(din,head,sizeof(head)); +// uint8_t rst[10]; +// size_t n=isInByte(din, len, head, 10, rst); +// uint8_t ndin[200]; +// for (size_t i = 0; i < n; i++) +// { +// int n=rst[i+1] +// memcpy(ndin,din,rst[i+1]-rst[i]); +// } + + uint8_t rst = memcmp(din,head,10);//只比较到ftype if (rst) { LOG_W("帧头不匹配"); @@ -638,7 +654,12 @@ void parseTTData(uint8_t *din, size_t len) } - +/** + * 缓存任务数据 + * @param din 单次收到的任务数据 + * @param len 任务数据长度 + * @return 0--正常,-1--异常 + */ int cacheData(uint8_t *din, size_t len) { static int cnt = 0; @@ -680,7 +701,7 @@ int cacheData(uint8_t *din, size_t len) if (fd < 0) { LOG_E("open file %s failed!", f); - return RT_ERROR; + return -RT_ERROR; } else { @@ -689,7 +710,7 @@ int cacheData(uint8_t *din, size_t len) { LOG_E("write to file %s failed!", f); close(fd); - return RT_ERROR; + return -RT_ERROR; } cnt += len; close(fd); diff --git a/applications/main.c b/applications/main.c index 5ceedd3..226d7c6 100644 --- a/applications/main.c +++ b/applications/main.c @@ -127,7 +127,11 @@ MSH_CMD_EXPORT(rt_hw_stm32_eth_init, 初始化网络。); void show_version(void) { - rt_kprintf("SW Version: %s\n","1.41"); + char str[30]; + uint8_t t[10]; + size_t len=time2Byte(t); + bytes2str(t, len, 10, "", str); + rt_kprintf("SW Version: %s build%s\n","1.43",bytes2str(t, len, 10, "", str)); } MSH_CMD_EXPORT(show_version,显示版本号); diff --git a/applications/tools.c b/applications/tools.c index dbd583c..98c2834 100644 --- a/applications/tools.c +++ b/applications/tools.c @@ -143,14 +143,14 @@ char *bytes2str(uint8_t *din, size_t count, int radix, char *sep, char *str); * @param str 字符串buffer * @return 字符串长度 */ -int time2Str(char *str) +char *time2Str(char *str) { uint8_t t[10]; size_t len=time2Byte(t); bytes2str(t, len, 10, "_", str); len = strlen(str); str[len]='\0'; - return len; + return str; } @@ -334,7 +334,31 @@ uint8_t bccCRC(uint8_t *din, size_t count) } - +/** + * @brief 在指定数组中搜索目标数组 + * + * @param din 待搜索数组 + * @param len 待搜索数组长度 + * @param s 目标数组 + * @param slen 搜索长度 + * @param dout 搜索结果 + * @return int 匹配个数 + */ +size_t isInByte(uint8_t *din, size_t len, uint8_t *s, size_t slen, uint8_t *dout) +{ + size_t cnt = 0; + uint8_t tmp[len]; + for (size_t i = 0; i < len - slen+1; i++) + { + if (memcmp(din + i, s, slen) == 0) + { + tmp[cnt++] = i; + i = i+slen-1; + } + } + memcpy(dout,tmp,cnt); + return cnt; +} @@ -389,6 +413,7 @@ void sysInit() cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO); rt_sem_release(cfgUpdate);//上电更新值 // rt_hw_stm32_eth_init();//激活网口 +// LOG_D("sysInit"); } INIT_APP_EXPORT(sysInit); diff --git a/applications/ttTR.c b/applications/ttTR.c index 1e1953b..ce0ee77 100644 --- a/applications/ttTR.c +++ b/applications/ttTR.c @@ -27,7 +27,7 @@ #define PORT 8005 static int sock=0; - +static int flag=0; void tcpClose(void) { /* 关闭这个连接 */ @@ -38,48 +38,49 @@ void tcpClose(void) } int isTCPok(void) { - return sock; + return flag; } int tcpInit(void) { int ret; + const char *url = URL; + int port = PORT; - const char *url = URL; - int port = PORT; + /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ + struct hostent *host; + host = gethostbyname(url); - /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ - struct hostent *host; - host = gethostbyname(url); + /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + /* 创建socket失败 */ + LOG_E("Socket error"); - /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) - { - /* 创建socket失败 */ - LOG_E("Socket error"); + return RT_ERROR; + } - return RT_ERROR; - } + /* 初始化预连接的服务端地址 */ + struct sockaddr_in server_addr; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + server_addr.sin_addr = *((struct in_addr *) host->h_addr); + rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); - /* 初始化预连接的服务端地址 */ - struct sockaddr_in server_addr; - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr = *((struct in_addr *)host->h_addr); - rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); + /* 连接到服务端 */ + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) + { + /* 连接失败 */ + LOG_E("Connect fail!\n"); + tcpClose(); - /* 连接到服务端 */ - if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) - { - /* 连接失败 */ - LOG_E("Connect fail!\n"); - tcpClose(); - - return RT_ERROR; - } - else { - tcpRecMQ(); + return RT_ERROR; + } + else + { +// tcpRecMQ(); + flag=1; return RT_EOK; } } @@ -166,12 +167,13 @@ void tcpRecMQ_thread_entry(void) // /* 释放接收缓冲 */ // break; // } +// LOG_D("%d Bytes received.",msg.size); if (msg.size >0 ) { // LOG_D("%d Bytes received.",msg.size); // LOG_HEX("tcpTT", 16, msg.data, msg.size); int result = rt_mq_send(&TTrx_mq, &msg, sizeof(msg)); - if ( result == -RT_EFULL) + if (result == -RT_EFULL) { /* 消息队列满 */ LOG_W("message queue full."); diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c index f340365..8a08e96 100644 --- a/applications/ttmsg/ttmsg.c +++ b/applications/ttmsg/ttmsg.c @@ -526,12 +526,12 @@ MSH_CMD_EXPORT(swTT, 切换TT电源。不带参切换电源状态,支持参数 extern struct rt_messagequeue TTrx_mq; void recTT_thread_entry() { - while(tcpInit() != RT_EOK) - { - LOG_W("TT server is not ready."); - rt_thread_mdelay(100); - } - LOG_D("TT server is ready."); +// while(tcpInit() != RT_EOK) +// { +// LOG_W("TT server is not ready."); +// rt_thread_mdelay(100); +// } +// LOG_D("TT server is ready."); static TTRx_MSG msg; while (1) { @@ -539,23 +539,29 @@ void recTT_thread_entry() rt_memset(&msg, 0, sizeof(msg)); if (rt_mq_recv(&TTrx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK) { -// LOG_D("%d Bytes received...",msg.size); - if (!msg.size) {//收到数据长度为0表示tcp断开 +// + if (!msg.size) + { //收到数据长度为0表示tcp断开 break; } - LOG_HEX("tcpTT", 16, msg.data, msg.size); + LOG_D("%d Bytes received from TT",msg.size); + LOG_HEX("TTrec", 16, msg.data, msg.size); //此处处理接收到数据 - rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88 };//前四字节 - if (rt_memcmp(msg.data, rec_good, 4)==0) - { - LOG_I("ack is good."); - } + rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41,0x43,0x4B }; //前四字节=帧头、后三字节=ACK + if (rt_memcmp(msg.data, rec_good, 4) == 0 && rt_memcmp(msg.data+msg.size-3, rec_good+4, 3) == 0) + { + LOG_I("ack is good."); + } + else { + LOG_D("try to parse data."); + parseTTData(msg.data,msg.size); + } } } } //INIT_COMPONENT_EXPORT(recTT_thread_entry); -static void recTT(void) +void recTT(void) { /* 创建线程 */ rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 2, 27, 10);