123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- #include "tt808.h"
- #include "usart.h"
- #include <stdint.h>
- #include <stdlib.h>
- //BYTE uint8_t
- //WORD uint16_t
- //DWORD uint32_t
- TT808 tt808;
- TCPCenterD tcpd;
- TCPClientD tcpc;
- uint16_t TCPCenterdatelength=0;//数据中心下行长度
- void newpacket(uint8_t *head,uint8_t *body,uint8_t outarr[]);
- void packageData(uint8_t flag, uint8_t *head, uint8_t *body, size_t bodylength, uint8_t *escaped_data, size_t *new_length);
- void tt808test(uint8_t* Bodyxxt, size_t bodylength, uint8_t XXLSH);
- // 发送一包内容为0x30 0x7e 0x08 0x7d 0x55的数据包,则经过封装如下:0x7e 0x30 0x7d 0x02 0x08 0x7d 0x01 0x55 0x7e
- /**消息结构
- * 标识位 消息头 消息体 校验码 标识位
- * 0x7e 11BYTE MORE 1BYTE 0x7e
- *
- */
- /**消息头
- *
- * 消息ID(WORD) 消息体属性(WORD) 终端ID(BCD[6]) 消息流水号(WORD)
- * (0 1) (2 3) (4 5 6 7 8 9) (10 11)
- */
- /**消息体属性
- *
- * 消息体长度 数据加密方式 分包 保留
- * (0 1 2 3 4 5 6 7 8 9) (10 11 12) 13 (14 15)
- */
- // 校验码指从消息头开始,同后一字节异或,直到校验码前一个字节,占用一个字节。
- void printBinary(uint16_t num) {
- for (int i = 15; i >= 0; i--) {
- if (num & (1u << i)) {
- printf("1");
- } else {
- printf("0");
- }
- }
- printf("\n");
- }
- typedef struct Node {
- uint8_t data;
- struct Node* next;
- } Node;
- Node* createNode(uint8_t value) {
- Node* newNode = (Node*)malloc(sizeof(Node));
- newNode->data = value;
- newNode->next = NULL;
- return newNode;
- }
- void insertAfter(Node* prevNode, uint8_t value) {
- Node* newNode = createNode(value);
- newNode->next = prevNode->next;
- prevNode->next = newNode;
- }
- uint8_t escaped_data[100]; // 定义足够大的数组存储转义后的数据
- void escape_data(uint8_t* input, int length, uint8_t* escaped_data, size_t* new_length) {
- *new_length = 0;
- Node* head = createNode(input[0]); // 创建链表头节点
- Node* current = head;
- escaped_data[(*new_length)++] = input[0]; // 将第一个数据直接存入
- for (int i = 1; i < length; i++) {
- if (input[i] == 0x7E && (i != length - 1)) {
- escaped_data[(*new_length)++] = 0x7D;
- escaped_data[(*new_length)++] = 0x02;
- }
- else if (input[i] == 0x7D) {
- escaped_data[(*new_length)++] = 0x7D;
- escaped_data[(*new_length)++] = 0x01;
- }
- else {
- escaped_data[(*new_length)++] = input[i];
- }
- }
-
- // 释放链表内存
- current = head;
- Node* temp;
- while (current != NULL) {
- temp = current;
- current = current->next;
- free(temp);
- }
- }
- //uint8_t escaped_data[100]; // 假设足够大来存储转义后的数据
- size_t new_length = 0;
- // 数据封装函数
- void packageData(uint8_t flag, uint8_t *head, uint8_t *body, size_t bodylength, uint8_t *escaped_data, size_t *new_length)
- {
- // Calculate total length
- uint8_t len = 1 + 12 + bodylength + 1 + 1; // 1 byte for flag + 10 bytes for header + body length + 1 byte for checksum + 1 byte for flag
- uint8_t checksum = 0; // Initialize checksum
-
- // Calculate checksum from message header
- for (int i = 0; i < 12; i++) {
- checksum ^= head[i];
- }
-
- for (int i = 0; i < bodylength; i++) {
- checksum ^= body[i];
- }
-
- // Start building the packet
- uint8_t packet[len];
- int index = 0;
- packet[index++] = flag; // Set the flag
- // Copy message header to packet
- for (int i = 0; i < 12; i++) {
- packet[index++] = head[i];
- }
- // Copy message body to packet
- for (size_t i = 0; i < bodylength; i++) {
- packet[index++] = body[i];
- }
- packet[index++] = checksum; // Set the checksum
- packet[index++] = flag; // Set the flag at the end
-
- int packetlength = (sizeof(packet)/sizeof(packet[0]));
- // 执行转义处理
- escape_data(packet, packetlength, escaped_data, new_length);
- // 输出转义后的数据
- // for (int i = 0; i < *new_length; i++) {
- // printf("%02X ", escaped_data[i]);
- // }
- // printf("\n");
-
- // char TCPvalue[128];
- // char *ptr = TCPvalue;
- // ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",sizeof(escaped_data));
- // for (int i = 0; i < sizeof(escaped_data); i++) {
- // ptr += sprintf(ptr, "%02X", escaped_data[i]);
- // }
- // sprintf(ptr, "\"\r\n");//TCPvalue
- // if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 3))//发送11BYTE MAX1460
- // //if(sendCmd_4G("AT+MIPSEND=1,11,\"12345678900\"\r\n", "+MIPSEND:", 1, 3))//发送11BYTE MAX1460
- // {
- // printf("#发送完成\r\n");
- // }
- // memset(cmd_replyArray,0,sizeof(cmd_replyArray));
- // memcpy(cmd_replyArray,escaped_data,*new_length);
- }
-
- void reCenter(void);
- uint16_t xxid =0 ;
- uint8_t BusinessData[256] = {0}; // 定义业务数据数组
- uint8_t TCPCenterCrc=0;
- /**
- * @breaf 目前监测 U1485标志位 0x7E 0x2A 0x23 0x7E
- */
- bool U4re =false;
- bool newmac=false;
- void TCPCenterDownward(void)//信息处理
- {
- //MIPurc1
-
- /*测试 由485执行*/ //7E 1235 0000 647475303031 0001 252A 1532 64747531 02 00000000000000000402 101000000003 03 01108026 081010164318 23 1234 7E
- /*
- xxid = U1recvBuff[1]<<8 | U1recvBuff[2];
-
- if (U1reFlag && U1recvBuff[0] == TT808FLAG && U1recvBuff[14] == TCPHeadBYTE && U1recvBuff[49] == TCPTAILBYTE && U1recvBuff[52] == TT808FLAG && (xxid==XXIDzdy ||xxid==XXIDxfMac1|| xxid==XXIDxfMac2||xxid==XXIDxfMac3||xxid==XXIDxfMac4)) // 业务头尾 下发MAC1
- {
- U1reFlag = 0;
- TCPCenterdatelength = U1recvBuff[13]+1;//长度+1(7E)
-
- //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST
-
-
-
- // 将业务数据存入BusinessData数组中
- if(U1recvLength-15==TCPCenterdatelength)//消息包长度
- {
- //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST 问询53B
- //crc测试暂时不用
- //TCPCenterCrc = xorBytes(&U1recvBuff[1],TCPCenterdatelength-1);
- tcpd.crc = U1recvBuff[TCPCenterdatelength+12];//50
- tcpd.Tcrc = U1recvBuff[TCPCenterdatelength+13];//51
- // if(tcpd.crc == TCPCenterCrc)
- // {
-
- tcpd.Xxid = (U1recvBuff[1] << 8) | U1recvBuff[2];
- tcpd.Xxtsx = (U1recvBuff[3] << 8) | U1recvBuff[4];
- for(int i = 0; i < 6; i++){
- tcpd.CidNum[i] = U1recvBuff[5 + i];
- }
- tcpd.Xxlsh = (U1recvBuff[11] << 8) | U1recvBuff[12];
-
-
-
-
- tcpd.workcmd = U1recvBuff[15];
- tcpd.Cmd = U1recvBuff[16];
- tcpd.LockID = (U1recvBuff[17] << 24) | (U1recvBuff[18] << 16) | (U1recvBuff[19] << 8) | U1recvBuff[20];
- tcpd.Gate = U1recvBuff[21];
- for(int i = 0; i < 8; i++){
- tcpd.Bill[i] = U1recvBuff[22 + i];
- }
- tcpd.LineCode = (U1recvBuff[30] << 8) | U1recvBuff[31];
- for(int i = 0; i < 6; i++){
- tcpd.Key[i] = U1recvBuff[32 + i];//KEY暂存mac号
- }
-
- tcpd.VaildTime = U1recvBuff[38];
- tcpd.standby = (U1recvBuff[39] << 24) | (U1recvBuff[40] << 16) | (U1recvBuff[41] << 8) | U1recvBuff[42];
- for(int i = 0; i < 6; i++){
- tcpd.Time[i] = U1recvBuff[43 + i];
- }
-
- U4re=true;
- newmac=true;
- // }
- }
- }
- */
- /*参考
- xxid = MIPurc1[1]<<8 | MIPurc1[2];
-
- if (u4RecvFlag && MIPurc1[0] == TT808FLAG && MIPurc1[14] == TCPHeadBYTE && MIPurc1[49] == TCPTAILBYTE && MIPurc1[52] == TT808FLAG && (xxid==XXIDzdy ||xxid==XXIDxfMac1|| xxid==XXIDxfMac2||xxid==XXIDxfMac3||xxid==XXIDxfMac4)) // 业务头尾
- {
- u4RecvFlag = 0;
- TCPCenterdatelength = MIPurc1[13]+1;//长度+1(7E)
-
- //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST
-
-
-
- // 将业务数据存入BusinessData数组中
- if(u4length-15==TCPCenterdatelength)//消息包长度
- {
- //HAL_UART_Transmit_DMA(&huart1, U1recvBuff, 53); //TEST 问询53B
- //crc测试暂时不用
- //TCPCenterCrc = xorBytes(&U1recvBuff[1],TCPCenterdatelength-1);
- tcpd.crc = MIPurc1[TCPCenterdatelength+12];//50
- tcpd.Tcrc = MIPurc1[TCPCenterdatelength+13];//51
- // if(tcpd.crc == TCPCenterCrc)
- // {
-
- tcpd.Xxid = (MIPurc1[1] << 8) | MIPurc1[2];
- tcpd.Xxtsx = (MIPurc1[3] << 8) | MIPurc1[4];
- for(int i = 0; i < 6; i++){
- tcpd.CidNum[i] = MIPurc1[5 + i];
- }
- tcpd.Xxlsh = (MIPurc1[11] << 8) | MIPurc1[12];
-
-
-
-
- tcpd.workcmd = MIPurc1[15];
- tcpd.Cmd = MIPurc1[16];//30 问询 32远程施封 38远程解封
- tcpd.LockID = (MIPurc1[17] << 24) | (MIPurc1[18] << 16) | (MIPurc1[19] << 8) | MIPurc1[20];
- tcpd.Gate = MIPurc1[21];
- for(int i = 0; i < 8; i++){
- tcpd.Bill[i] = MIPurc1[22 + i];
- }
- tcpd.LineCode = (MIPurc1[30] << 8) | MIPurc1[31];
- for(int i = 0; i < 6; i++){
- tcpd.Key[i] = MIPurc1[32 + i];
- }
-
- tcpd.VaildTime = MIPurc1[38];
-
- tcpd.standby = (MIPurc1[39] << 24) | (MIPurc1[40] << 16) | (MIPurc1[41] << 8) | MIPurc1[42];
- for(int i = 0; i < 6; i++){
- tcpd.Time[i] = MIPurc1[43 + i];
- }
-
- tcpc.Gate = 0x00;
- U4re=true;
- // }
- }
- }
- */
- }
- bool genghaunmac =true;
- extern Menu_table menu;
- void newMacchange(void)
- {
- //conn AT+CONN=101000000010\r\n
- //connected +CONNECTED:0,101000000010
- //dis +DISCONN:0,101000000010
- //snnum 0010
-
- uint16_t mac = (tcpd.Key[4] << 8) | tcpd.Key[5];
- char SNconn[50];
- char *ptr = SNconn;
- ptr += sprintf(ptr, "AT+CONN=");
- for (int i = 0; i < 6; i++) {
- ptr += sprintf(ptr, "%02X", tcpd.Key[i]);
- }
- sprintf(ptr, "\r\n");//TCPvalue
-
- char SNF[50];
- char *ptr1 = SNF;
- ptr1 += sprintf(ptr1, "+CONNECTED:0,");
- for (int i = 0; i < 6; i++) {
- ptr1 += sprintf(ptr1, "%02X", tcpd.Key[i]);
- }
- //sprintf(ptr, "\r\n");//TCPvalue
-
-
- char SNname[2];
- char *ptr2 = SNname;
- for (int i = 4; i < 6; i++) {
- ptr2 += sprintf(ptr2, "%02X", tcpd.Key[i]);
- }
- //sprintf(ptr, "\r\n");//TCPvalue
-
- char SND[50];
- char *ptr3 = SND;
- ptr3 += sprintf(ptr3, "+DISCONN:0,");
- for (int i = 0; i < 6; i++) {
- ptr3 += sprintf(ptr3, "%02X", tcpd.Key[i]);
- }
-
- if(xxid==0x1234)
- {
-
- //scanMac();
- }
- else if(xxid==0x1235)//M1
- {
- //xxid=0;
-
-
- menu.SN1name=NULL;
- menu.SN1=NULL;
- menu.SN1F=NULL;
- menu.SN1D=NULL;
- erase_flash(Sn1StatusAddr);
- erase_flash(Sn1Addr);
- erase_flash(serialNum1Addr);
-
-
- menu.SN1 = SNconn;//AT+CONN=*********\r\n
- menu.SN1F = SNF;//+CONNECTED:0,101000000010
- menu.SN1name = SNname;
- menu.SN1D = SND;
- Write_Information(Sn1Addr, mac);
- //refresh=true;
- tcpc.Gate = 0x01;
- xxid=0;
-
- menu.home&=0x0<<3;
- //scanMac();
- genghaunmac=true;
-
- }
- else if(xxid==0x1236)//M2
- {
-
- menu.SN2name=NULL;
- menu.SN2=NULL;
- menu.SN2F=NULL;
- menu.SN2D=NULL;
- erase_flash(Sn2StatusAddr);
- erase_flash(Sn2Addr);
- erase_flash(serialNum2Addr);
-
- menu.SN2 = SNconn;//AT+CONN=*********\r\n
- menu.SN2F = SNF;//+CONNECTED:0,101000000010
- menu.SN2name = SNname;
- menu.SN2D = SND;
- Write_Information(Sn2Addr, mac);
- //refresh=true;
- tcpc.Gate = 0x01;
- xxid=0;
-
- menu.home&=0x0<<2;
- genghaunmac=true;
- //scanMac();
- }
- else if(xxid==0x1237)//M3
- {
- menu.SN3name=NULL;
- menu.SN3=NULL;
- menu.SN3F=NULL;
- menu.SN3D=NULL;
- erase_flash(Sn3StatusAddr);
- erase_flash(Sn3Addr);
- erase_flash(serialNum3Addr);
-
-
- menu.SN3 = SNconn;//AT+CONN=*********\r\n
- menu.SN3F = SNF;//+CONNECTED:0,101000000010
- menu.SN3name = SNname;
- menu.SN3D = SND;
- Write_Information(Sn3Addr, mac);
- //refresh=true;
- tcpc.Gate = 0x01;
- xxid=0;
-
- menu.home&=0x0<<1;
- genghaunmac=true;
- //scanMac();
- }
- else if(xxid==0x1238)//M4
- {
- menu.SN4name=NULL;
- menu.SN4=NULL;
- menu.SN4F=NULL;
- menu.SN4D=NULL;
- erase_flash(Sn4StatusAddr);
- erase_flash(Sn4Addr);
- erase_flash(serialNum4Addr);
-
- menu.SN4 = SNconn;//AT+CONN=*********\r\n
- menu.SN4F = SNF;//+CONNECTED:0,101000000010
- menu.SN4name = SNname;
- menu.SN4D = SND;
- Write_Information(Sn4Addr, mac);
- //refresh=true;
- tcpc.Gate = 0x01;
- xxid=0;
-
- menu.home &= 0x0;
- genghaunmac=true;
- //scanMac();
- }
-
-
- }
- uint8_t hext[256]={0};
- uint8_t cmd_replyArray[] = {0};
- uint32_t DTUID=0x64747531;
- uint16_t length;
- void wsj(void);
- extern Time time;
- void tcphuifu(void);
- int tcpdataLength = 0;
- void wjwd(void);
- void replyCenter(void)
- {
- U4re=false;
- if(tcpd.Cmd==0x30 || tcpd.Cmd==0x32 || tcpd.Cmd==0x38)
- {
- wsj();//闻讯时间
- HAL_Delay(100);
- //wjwd();
- //HAL_Delay(100);
-
- tcpc.workcmd = tcpd.workcmd;//终端回复
- tcpc.Cmd = tcpd.Cmd;
- //64 74 75 31 dtu1
- tcpc.LockID = ((DTUID << 24)&0xff) | ((DTUID << 16)&0xff) | ((DTUID << 8)&0xff) | DTUID;
- //tcpc.Gate = 0x00;//待传入
-
- /* 载货清单 */
- tcpc.Bill[0] = (JD_dw>>24)&0xff;//待传入
- tcpc.Bill[1] = (JD_dw>>16)&0xff;//待传入
- tcpc.Bill[2] = (JD_dw>>8)&0xff;//待传入
- tcpc.Bill[3] = (JD_dw)&0xff;//待传入
- tcpc.Bill[4] = (WD_dw>>24)&0xff;//待传入
- tcpc.Bill[5] = (WD_dw>>16)&0xff;//待传入
- tcpc.Bill[6] = (WD_dw>>8)&0xff;//待传入
- tcpc.Bill[7] = (WD_dw)&0xff;//待传入
- /* 电压 */
- tcpc.Voltage = 0x00;//待传入
-
- if(tcpc.Cmd==0x32)//施封
- {
- if(tcpd.VaildTime==0x01)//锁1施封
- {
-
- }
- if(tcpd.VaildTime==0x02)//锁2施封
- {
-
- }
- if(tcpd.VaildTime==0x03)//锁3施封
- {
-
- }
- if(tcpd.VaildTime==0x04)//锁4施封
- {
-
- }
- }
-
- if(tcpc.Cmd==0x38)//解封
- {
- if(tcpd.VaildTime==0x01)//锁1解封
- {
-
- }
- if(tcpd.VaildTime==0x02)//锁2解封
- {
-
- }
- if(tcpd.VaildTime==0x03)//锁3解封
- {
-
- }
- if(tcpd.VaildTime==0x04)//锁4解封
- {
-
- }
- }
- /* 锁状态 */
- //tcpc.LockStatus = 0xAA;//待传入
- tcpc.LockStatus = tt808.L1Lockstatus;
- /* 电机状态 */
- //tcpc.MotoStatus = 0x00;//待传入
- tcpc.MotoStatus = tt808.L2Lockstatus;
- /* 线路编号 */
- tcpc.LineCode = ((uint16_t)tt808.L3Lockstatus << 8) | tt808.L4Lockstatus;//待传入
-
- // /* 操作标识码 */
- tcpc.OperationIdNum[0] = 0x00;//待传入
- tcpc.OperationIdNum[1] = 0x00;//待传入
- tcpc.OperationIdNum[2] = 0x00;//待传入
- tcpc.OperationIdNum[3] = 0x00;//待传入
- tcpc.OperationIdNum[4] = 0x00;//待传入
- tcpc.OperationIdNum[5] = 0x00;//待传入
- tcpc.OperationIdNum[6] = 0x00;//待传入
- tcpc.OperationIdNum[7] = 0x00;//待传入
- /* 设备标识码 */
- tcpc.cmdSRC = 0x0F;//dtu设备
-
- /* 下发命令时间 */
- tcpc.Time[0] = time.year;
- tcpc.Time[1] = time.month;
- tcpc.Time[2] = time.day;
- tcpc.Time[3] = time.hour;
- tcpc.Time[4] = time.minute;
- tcpc.Time[5] = time.second;
-
-
- //***
- cmd_replyArray[1]=TCPHeadBYTE;
- cmd_replyArray[2]=tcpc.workcmd;
- cmd_replyArray[3]=tcpc.Cmd;
- cmd_replyArray[4]=(tcpc.LockID>>24)&0xff;
- cmd_replyArray[5]=(tcpc.LockID>>16)&0xff;
- cmd_replyArray[6]=(tcpc.LockID>>8)&0xff;
- cmd_replyArray[7]=(tcpc.LockID)&0xff;
- cmd_replyArray[8]=tcpc.Gate;
- memcpy(&cmd_replyArray[9],tcpc.Bill,8);
- cmd_replyArray[17]=tcpc.Voltage;
- cmd_replyArray[18]=tcpc.LockStatus;
- cmd_replyArray[19]=tcpc.MotoStatus;
- cmd_replyArray[20]=(tcpc.LineCode>>8)&0xff;
- cmd_replyArray[21]=(tcpc.LineCode)&0xff;
- memcpy(&cmd_replyArray[22],tcpc.OperationIdNum,8);
- cmd_replyArray[30]=tcpc.cmdSRC;
- memcpy(&cmd_replyArray[31],tcpc.Time,6);
-
- cmd_replyArray[0]=sizeof(cmd_replyArray)-1;
- cmd_replyArray[37]=TCPTAILBYTE;
- cmd_replyArray[38]=xorBytes(&cmd_replyArray[1],37);//第2字节开始校验
-
- tcpdataLength = sizeof(cmd_replyArray) / sizeof(cmd_replyArray[0]);
-
-
-
- tcphuifu();
- }
-
-
- }
- uint8_t MIPurc[256];
- uint8_t MIPurc1[256];
- uint8_t MIPurcXXTCSJ[256];//平台下行透传数据
- bool MIPurcReFlag = false;
- uint16_t hccd = 0;
- char high[]={0};
- bool chuliSJ = false;
- char hex2asc(const char *hex)
- {
- char asc = 0;
- char a = (hex[0]>='A' && hex[0]<='F')?(hex[0]^32):hex[0];
- char b = (hex[1]>='A' && hex[1]<='F')?(hex[1]^32):hex[1];
- if ((a>='0' && a<='9' || a>='a' && a<='f') && (b>='0' && b<='9' || b>='a' && b<='f')){
- asc = ((('a'<=a && a<='f')?(a-'a')+10:a-'0')<<4)|(('a'<=b && b<='f')?(b-'a')+10:b-'0');
- }
- return asc;
- }
- char *bin2hex(char *hex, const unsigned char *bin, int size)
- {
- size_t i;
- for(i=0; i<size; i++)
- {
- sprintf(hex+i*2, "%02x", bin[i]);
- }
- hex[i+i] = 0;
- return hex;
- }
- int hex2bin(unsigned char *bin, const char *hex)
- {
- const char *h = hex;
- unsigned char *b = bin;
- int rlt = 0;
- while(*h){
- *b++ = hex2asc(h);
- h = h + 2;
- rlt++;
- }
- return rlt;//长度
- }
- int rlt;
- char year_str[3];
- char month_str[3];
- char day_str[3];
- char hour_str[3];
- char minute_str[3];
- char second_str[3];
- void wsj(void)
- {
- if (sendCmd_4G("AT+CCLK?\r\n", "OK", 1, 1))
- {
-
- memcpy(year_str, &g_ML307A_8buf_Down[8], 2); // 复制两个字符
- memcpy(month_str, &g_ML307A_8buf_Down[11], 2);
- memcpy(day_str, &g_ML307A_8buf_Down[14], 2);
- memcpy(hour_str, &g_ML307A_8buf_Down[17], 2);
- memcpy(minute_str, &g_ML307A_8buf_Down[20], 2);
- memcpy(second_str, &g_ML307A_8buf_Down[23], 2);
-
- year_str[2] = '\0'; // 添加空字符结束符
- month_str[2] = '\0';
- day_str[2] = '\0';
- hour_str[2] = '\0';
- minute_str[2] = '\0';
- second_str[2] = '\0';
-
- time.year = (uint8_t)atoi(year_str); // 使用atoi函数将字符转换为数字并赋值给time.year
- time.month = (uint8_t)atoi(month_str);
- time.day = (uint8_t)atoi(day_str);
- time.hour = (uint8_t)atoi(hour_str)+8;
- time.minute = (uint8_t)atoi(minute_str);
- time.second = (uint8_t)atoi(second_str);
- }
- }
- uint8_t tcpoutarr[]={0};
- uint8_t header[12];
- void tcphuifu(void)
- {
- if(!tcpconn)
- {
- if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
- {
- printf("#断开连接\r\n");
- }
- if(sendCmd_4G("AT+MIPOPEN=1,\"TCP\",\"118.31.22.26\",8901,60,0\r\n", "+MIPOPEN: 1,0", 1, 3))//TCP连接
- {
- printf("#TCP连接成功\r\n");
- if(sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 3))//默认输入输出为ASCII,0 发16,1 接16,1
- {
- printf("#TCP设置完成\r\n");
- tcpconn=true;
- }
- }
- }
- module4G_F=false;
- uint16_t XXTSXdata = tcpdataLength;
- uint8_t checksum = 0; // Initialize checksum
-
- //XXTSXdata |= ( tcpdataLength | (TCPJMFSRSA<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
- XXTSXdata |= ( tcpdataLength | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
-
- if(xxid == XXIDzdy)
- {
- //xxid=0;
- header[0] = ((XXIDzdy>>8)&0xff);
- header[1] = (XXIDzdy&0xff);
- }
-
- if(xxid == XXIDxfMac1)
- {
- //xxid=0;
- header[0] = ((XXIDxfMac1>>8)&0xff);
- header[1] = (XXIDxfMac1&0xff);
- }
- if(xxid == XXIDxfMac2)
- {
- //xxid=0;
- header[0] = ((XXIDxfMac2>>8)&0xff);
- header[1] = (XXIDxfMac2&0xff);
- }
- if(xxid == XXIDxfMac3)
- {
- //xxid=0;
- header[0] = ((XXIDxfMac3>>8)&0xff);
- header[1] = (XXIDxfMac3&0xff);
- }
- if(xxid == XXIDxfMac4)
- {
- //xxid=0;
- header[0] = ((XXIDxfMac4>>8)&0xff);
- header[1] = (XXIDxfMac4&0xff);
- }
-
-
-
- header[2] = ((XXTSXdata>>8)&0xff);
- header[3] = (XXTSXdata&0xff);
- header[4] = 0x64;
- header[5] = 0x74;
- header[6] = 0x75;
- header[7] = 0x30;
- header[8] = 0x30;
- header[9] = 0x31;
- header[10] = ((tt808.LSH>>8)&0xff);
- header[11] = (tt808.LSH&0xff);
-
-
-
- for (int i = 0; i < 12; i++) {
- checksum ^= header[i];
- }
-
- for (int i = 0; i < tcpdataLength; i++) {
- checksum ^= cmd_replyArray[i];
- }
- ;
- uint8_t len = 1 + 12 + tcpdataLength + 1 + 1;
- uint8_t tcppacket[len];
- int index = 0;
- tcppacket[index++] = TT808FLAG; // Set the flag
-
- for (int i = 0; i < 12; i++) {
- tcppacket[index++] = header[i];
- }
- // Copy message body to packet
- for (size_t i = 0; i < tcpdataLength; i++) {
- tcppacket[index++] = cmd_replyArray[i];
- }
-
- uint16_t tcpoutLength = len;
-
- tcppacket[index++] = checksum; // Set the checksum
- tcppacket[index++] = TT808FLAG; // Set the flag at the end
-
-
- char TCPvalue[256];
- char *ptr = TCPvalue;
- ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",tcpoutLength);
- for (int i = 0; i < tcpoutLength; i++) {
- ptr += sprintf(ptr, "%02X", tcppacket[i]);
- }
- sprintf(ptr, "\"\r\n");//TCPvalue
- if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
- {
- printf("#发送完成\r\n");
- }
-
- module4G_F=true;
- tt808.LSH+=1;
- memset(MIPurc1,0,rlt);
- }
- // 7E 0002 0005 0000000000000000 042A002309 03 7E
- uint8_t xxtnewlength=0;
- uint8_t xxtfz[100];
- uint8_t tcppacket[256];
- void chuli(uint16_t XXID)
- {
- uint16_t XXTSXdata = xxtnewlength;
- XXTSXdata |= ( tcpdataLength | (TCPJMFSNULL<<10)); // 将长度左移3位后和加密方式进行按位或操作(001 RSA ,000 无加密)
-
- header[0] = ((XXID>>8)&0xff);
- header[1] = (XXID&0xff);
- header[2] = ((XXTSXdata>>8)&0xff);
- header[3] = (XXTSXdata&0xff);
- header[4] = 0x00;
- header[5] = 0x00;
- header[6] = 0x16;
- header[7] = 0x00;
- header[8] = 0x00;
- header[9] = 0x08;
- header[10] = ((tt808.LSH>>8)&0xff);;
- header[11] = (tt808.LSH&0xff);;
-
-
-
-
- int index = 0;
- tcppacket[index++] = TT808FLAG; // Set the flag
-
- for (int i = 0; i < 12; i++) {
- tcppacket[index++] = header[i];
- }
- // Copy message body to packet
- for (size_t i = 0; i < xxtnewlength; i++) {
- tcppacket[index++] = xxtfz[i];
- }
- tcppacket[index++] = xorBytes(&tcppacket[1],12+xxtnewlength);; // Set the checksum
- tcppacket[index++] = TT808FLAG; // Set the flag at the end
-
-
- // for (int i = 0; i < index; i++) {
- // printf("%02X ", tcppacket[i]); // 以十六进制格式打印每个元素
- // }
-
-
-
- char TCPvalue[256];
- char *ptr = TCPvalue;
- ptr += sprintf(ptr, "AT+MIPSEND=1,%d,\"",index);
- for (int i = 0; i < index; i++) {
- ptr += sprintf(ptr, "%02X", tcppacket[i]);
- }
- sprintf(ptr, "\"\r\n");//TCPvalue
- if(sendCmd_4G(TCPvalue, "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
- {
- printf("#发送完成\r\n");
- }
- tt808.LSH+=1;
- }
- /*需要传入消息体长度sizeof(xxt)/sizeof(xxt[0]);*/
- void tt808FsFunc(uint8_t xxt[], uint8_t xxtlength)
- {
- xxtnewlength = xxtFZ(xxt, xxtlength, xxtfz);
- //chuli(XXIDzdxt);
- chuli(XXIDzdjq);
-
- }
- uint8_t xxtFZ(uint8_t xxt[], uint8_t xxtlength, uint8_t outXxt[])
- {
- int index = 0;
- uint8_t newlength = xxtlength+3;
-
- uint8_t newXxt[newlength];
- newXxt[index++]=newlength;
- newXxt[index++]=0x2A;
- for (int i = 0; i < xxtlength; i++) {
- newXxt[index++] = xxt[i]; // 将消息头拷贝到报文中
- }
- newXxt[index++]=0x23;
- newXxt[index++]=xorBytes(&newXxt[1],newlength-1);
- newlength = index;
-
- for(int i =0;i<newlength;i++)
- {
- outXxt[i]=newXxt[i];
- }
-
-
-
- //test
- // for (int i = 0; i < newlength; i++) {
- // printf("%02X ", newXxt[i]); // 以十六进制格式打印每个元素
- // }
- return newlength;
- }
|