#include #include #include #include #include #include #include #include #include extern int g_msg_fd;extern int g_msg_test_fd;extern int g_sins_gps_fd;#if 1#define MAX_PAYLOAD_LEN 210 //即(1680/8) #define INSTRUCTION_SIZE 5 #define PACKET_LEN_SIZE 2 #define USER_ADDR_SIZE 3 #define CHECKSUM_SIZE 1 #define IPUC (INSTRUCTION_SIZE + PACKET_LEN_SIZE + USER_ADDR_SIZE + CHECKSUM_SIZE) #define TXSQ_INFO_FIRM_SIZE 7 //即(1 个信息类别 + 3 个用户地址 + 2个电文长度 + 1个应答字节) #define TXSQ_FIRM_SIZE (IPUC + TXSQ_INFO_FIRM_SIZE) #define ICJC_INFO_FIRM_SIZE 1 //即帧号,占一个字节 #define ICJC_FIRM_SIZE (IPUC + ICJC_INFO_FIRM_SIZE) #define XTZJ_INFO_FIRM_SIZE 2 //即自检频度,占二个字节 #define XTZJ_FIRM_SIZE (IPUC + XTZJ_INFO_FIRM_SIZE) #define ICXX_INFO_FIRM_SIZE 11 //即(1个帧号+3个通播ID+1个用户特征+2个服务频度+1个通信等级+1个加密标志+2个下属用户总数) #define ICXX_FIRM_SIZE (IPUC + ICXX_INFO_FIRM_SIZE) #define TXXX_INFO_FIRM_SIZE 9 //即(1个信息类别+3个发信方地址+2个发信时间+2个电文长度+1个CRC标志 #define TXXX_FIRM_SIZE (IPUC + TXXX_INFO_FIRM_SIZE) #define TXXX_MAX_SIZE (TXXX_FIRM_SIZE + MAX_PAYLOAD_LEN)//TXXX由固定长度和净长度构成 #define FKXX_INFO_FIRM_SIZE 5//即(1个反馈标志+4个附加信息) #define FKXX_FIRM_SIZE (IPUC + FKXX_INFO_FIRM_SIZE) #define ZJXX_INFO_FRIM_SIZE 10 //即(1个IC卡状态+1个硬件状态+1个电池电量+1个入站状态+6个功率状态) #define ZJXX_FIRM_SIZE (IPUC + ZJXX_INFO_FRIM_SIZE) #define RX_BD_MAX_DATA_SIZE TXXX_MAX_SIZE #define TXSQ_PAYLOAD_CHINESE 0x44 #define TXSQ_PAYLOAD_BCD 0x46 enum { DWXX_BUF = (1 TXXX_BUF = (1 ICXX_BUF = (1 ZJXX_BUF = (1 SJXX_BUF = (1 BBXX_BUF = (1 FKXX_BUF = (1 }; unsigned char bd_buf_bitmap = 0; //unsigned char dwxx_buf[todo]; unsigned char txxx_buf[TXXX_MAX_SIZE]; unsigned char icxx_buf[ICXX_FIRM_SIZE]; unsigned char zjxx_buf[ZJXX_FIRM_SIZE]; //unsigned char sjxx_buf[todo]; //unsigned char bbxx_buf[todo]; unsigned char fkxx_buf[FKXX_FIRM_SIZE]; unsigned int rx_packet_len = 0; unsigned char bd_rx_char; unsigned int bd_buf_pointer = 0; unsigned char bd_shared_rx_buf[RX_BD_MAX_DATA_SIZE]; unsigned char g_src_user_addr[3]; unsigned char g_dst_user_addr[3]; unsigned char g_transfer_format; /* 定位信息 */ struct dwxx_struct { unsigned int todo; }; /* 通信信息 */ struct txxx_info_type_struct { unsigned char packet_comm:2;//打包通信unsigned char transfer_format:1; //数据传输格式unsigned char ack:1; //命令正确应答 unsigned char comm_way:1; //通信方式unsigned char has_key:1; //字典里面有没有一个关键字 unsigned char rest:2; }; struct send_time_struct { unsigned char hour; unsigned char minute; }; struct txxx_info_struct { struct txxx_info_type_struct txxx_info_type; unsigned char src_user_addr[3]; struct send_time_struct send_time; unsigned int payload_len; unsigned char payload[MAX_PAYLOAD_LEN]; unsigned char crc; }; struct txxx_struct { unsigned char instruction[5]; unsigned int packet_len; //解析结构体时以整形数据表示其长度 unsigned char user_addr[3]; struct txxx_info_struct txxx_info; unsigned char checksum; }; struct icxx_info_struct { unsigned char frame_id; //框架unsigned char broadcast_id[3]; //广播unsigned char user_feature; //用户特征unsigned int service_frequency; //服务频率unsigned char comm_level; //通信水准 unsigned char encryption_flag; //加密标志 unsigned int user_num; //用户号}; /* IC信息 */ struct icxx_struct { unsigned char instruction[5]; //指令unsigned int packet_len; //打包拉长 unsigned char user_addr[3]; // 用户地址struct icxx_info_struct icxx_info; unsigned char checksum; //校验和 }; struct zjxx_info_struct { unsigned char ic_status; //身份 unsigned char hw_status; unsigned char battery_quantity;//电池总量 unsigned char in_station_status; //站内身份unsigned char power_status[6]; // 权力身份}; struct zjxx_struct { unsigned char instruction[5]; unsigned int packet_len; unsigned char user_addr[3]; struct zjxx_info_struct zjxx_info; unsigned char checksum; }; struct sjxx_struct { unsigned int todo; }; struct bbxx_struct { unsigned int todo; }; struct fkxx_info_struct { unsigned char fk_flag; unsigned char extra_info[4]; }; struct fkxx_struct { unsigned char instruction[5]; unsigned int packet_len; unsigned char user_addr[3]; struct fkxx_info_struct fkxx_info; unsigned char checksum; }; /* 异或校验和算法 */ static unsigned char xor_checksum (unsigned char *buf, unsigned int len) { unsigned int i; unsigned char checksum = 0; for (i = 0; i { checksum ^= *(buf++); } return checksum; } void create_txsq(unsigned char *src_user_addr, unsigned char *dst_user_addr, unsigned char transfer_format, unsigned char *payload, unsigned int payload_len, unsigned char *send_txsq_data) { /* 1、通信申请指令初始化,不采用memcpy等库函数,提高指令执行效率,只有涉及到大量数据赋值拷贝时才考虑用库函数 */ send_txsq_data[0] = $; send_txsq_data[1] = T; send_txsq_data[2] = X; send_txsq_data[3] = S; send_txsq_data[4] = Q; /* 2、包长度,先传高位,再传低位 */ send_txsq_data[5] = (TXSQ_FIRM_SIZE + payload_len) / 256; send_txsq_data[6] = (TXSQ_FIRM_SIZE + payload_len) % 256; /* 3、源用户地址 */ send_txsq_data[7] = *src_user_addr; send_txsq_data[8] = *(src_user_addr + 1); send_txsq_data[9] = *(src_user_addr + 2); /* 4.1、信息-信息类别 */ if (transfer_format == 0) //汉字 { send_txsq_data[10] = TXSQ_PAYLOAD_CHINESE;//0b01000100; } else //代码/混发 { send_txsq_data[10] = TXSQ_PAYLOAD_BCD;//0b01000110; } /* 4.2、信息-目的用户地址 */ send_txsq_data[11] = *dst_user_addr; send_txsq_data[12] = *(dst_user_addr + 1); send_txsq_data[13] = *(dst_user_addr + 2); /* 4.3、信息-电文净荷长度-单位是bit */ send_txsq_data[14] = (payload_len * 8) / 256; send_txsq_data[15] = (payload_len * 8) % 256; /* 4.4、信息-是否应答 */ send_txsq_data[16] = 0; /* 4.5、信息-电文内容 */ memcpy(send_txsq_data[17] , payload, payload_len); /* 5、校验和 */ send_txsq_data[TXSQ_FIRM_SIZE + payload_len -1] = xor_checksum(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len -1)); printf(&"\r\n xor_checksum = 0x%x\r\n&", xor_checksum(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len -1))); } /* 1、结构体不宜管理可变长度的数据协议,如通讯申请协议 2、发送长度为6个字节(&"我爱你&"),发送方式为中文,协议内容: txsq:24 54 58 53 51 00 18 02 ad f7 44 02 ad f7 00 30 00 ce d2 b0 ae c4 e3 63 txxx:24 54 58 58 58 00 1a 02 ad f7 40 02 ad f7 00 00 00 30 ce d2 b0 ae c4 e3 00 67 */ void send_txsq(unsigned char cmd, unsigned char *src_user_addr, unsigned char *dst_user_addr, unsigned char transfer_format, unsigned char *send_txsq_payload, unsigned int send_txsq_payload_len) { unsigned int i; unsigned char l_transfer_format; unsigned int payload_len; unsigned long src_user_addr_long; unsigned long dst_user_addr_long; unsigned char l_src_user_addr[3]; unsigned char l_dst_user_addr[3]; unsigned char payload[MAX_PAYLOAD_LEN]; unsigned char send_txsq_data[TXSQ_FIRM_SIZE + MAX_PAYLOAD_LEN]; if (cmd == 1) { printf(&"\r\n 内容编码(0:汉字,1代码):&"); scanf(&"%d&",l_transfer_format); printf(&"\r\n 发送内容:&"); scanf(&"%s&", payload); payload_len = strlen((char const *)payload); // payload_len = payload_len -1;//鍖楁枟鎶ユ枃妯″潡婧愬湴鍧?l_src_user_addr[0] = 0x05; l_src_user_addr[1] =0x95; l_src_user_addr[2] =0x79; //鍖楁枟鎶ユ枃妯″潡鐩殑鍦板潃l_dst_user_addr[0] =0x02; l_dst_user_addr[1] =0x7b;l_dst_user_addr[2] = 0x14;// l_dst_user_addr[0] =0x05; // l_dst_user_addr[1] =0x95;// l_dst_user_addr[2] = 0x79;for (i = 0; i { g_src_user_addr[i] = l_src_user_addr[i]; g_dst_user_addr[i] = l_src_user_addr[i]; } g_transfer_format = l_transfer_format; // 由前面所有参数组合成 send_txsq_data,这是最终实际发给卫星的数据包create_txsq(l_src_user_addr, l_dst_user_addr, l_transfer_format, payload, payload_len, send_txsq_data); write(g_msg_fd,send_txsq_data,(TXSQ_FIRM_SIZE + payload_len));// tx_uart3_string(send_txsq_data, (TXSQ_FIRM_SIZE + payload_len)); } else { create_txsq(src_user_addr, dst_user_addr, transfer_format, send_txsq_payload, send_txsq_payload_len, send_txsq_data); // tx_uart3_string(send_txsq_data, (TXSQ_FIRM_SIZE + send_txsq_payload_len)); write(g_msg_fd,send_txsq_data,(TXSQ_FIRM_SIZE + send_txsq_payload_len));} } void parse_txxx(struct txxx_struct *txxx) { unsigned int i; unsigned int payload_len; unsigned char send_data[104];//用途有2 /* 1、指令内容 */ for (i = 0; i { (*txxx).instruction[i] = txxx_buf[i]; } /* 2、接收包长 */ (*txxx).packet_len = txxx_buf[5] * 256 + txxx_buf[6]; /* 3、目的用户地址 */ for (i = 0; i { (*txxx).user_addr[i] = txxx_buf[i + 7]; } /* 4.1、信息-信息类别 */ memcpy(((*txxx).txxx_info.txxx_info_type), (txxx_buf + 10), 1); /* 4.2、信息-发送方地址 */ for (i = 0; i { (*txxx).txxx_info.src_user_addr[i] = txxx_buf[i + 11]; } /* 4.3、信息-发送时间 */ (*txxx).txxx_info.send_time.hour = txxx_buf[14]; (*txxx).txxx_info.send_time.minute = txxx_buf[15]; /* 4.4、信息-电文长度 */ (*txxx).txxx_info.payload_len = txxx_buf[16] * 256 + txxx_buf[17]; payload_len = (*txxx).txxx_info.payload_len / 8; /* 4.5、信息-电文内容 */ memcpy((*txxx).txxx_info.payload, (txxx_buf + 18), payload_len); /* 4.6、信息-CRC */ (*txxx).txxx_info.crc = txxx_buf[18 + payload_len]; /* 5、校验和 */ (*txxx).checksum = txxx_buf[18 + payload_len + 1]; #if 0if ((txxx_buf[18] == z) (txxx_buf[19] == t)/* (txxx_buf[18] == z) (txxx_buf[19] == t) (txxx_buf[20] == c) (txxx_buf[20] == x) */) { #if 1 create_send_data_1(send_data); send_txsq(0, (*txxx).user_addr[0], (*txxx).txxx_info.src_user_addr[0], g_transfer_format, send_data, (104-37)); #else for (i = 0; i { send_data[i] = 0x5a; } send_data[0] = M; send_data[1] = (104-37); send_txsq(0, (*txxx).user_addr[0], (*txxx).txxx_info.src_user_addr[0], g_transfer_format, send_data, (104-37)); #endif }else if ( (txxx_buf[18] == M) (txxx_buf[19] == (104-37)) ) { create_send_data_2(send_data, txxx_buf[18], payload_len); SendData(104, 1, GSMNEW_ENUM_measureCurSend, send_data); } #endif} void read_bd_rx_info(void) { struct txxx_struct txxx; struct icxx_struct icxx; struct zjxx_struct zjxx; struct fkxx_struct fkxx; #if 0if (bd_buf_bitmap FKXX_BUF) { parse_fkxx(fkxx); print_fkxx(fkxx); bd_buf_bitmap = ~FKXX_BUF; } if (bd_buf_bitmap ICXX_BUF) { parse_icxx(icxx); print_icxx(icxx); bd_buf_bitmap = ~ICXX_BUF; } if (bd_buf_bitmap ZJXX_BUF) { parse_zjxx(zjxx); print_zjxx(zjxx); bd_buf_bitmap = ~ZJXX_BUF; } #endifif (bd_buf_bitmap TXXX_BUF) { parse_txxx(txxx); //print_txxx(txxx); bd_buf_bitmap = ~TXXX_BUF; } } int copy_packet_from_shared_buf() //根据1、2字节判断数据包信息类型{ if ((bd_shared_rx_buf[1] == D) (bd_shared_rx_buf[2] == W)) //收到定位信息$DWXX { bd_buf_bitmap |= DWXX_BUF; //memcpy(dwxx_buf, bd_shared_rx_buf, sizeof(dwxx_buf)); return 0;} else if ((bd_shared_rx_buf[1] == T) (bd_shared_rx_buf[2] == X)) //收到通信信息$TXXX { bd_buf_bitmap |= TXXX_BUF; memcpy(txxx_buf, bd_shared_rx_buf, sizeof(txxx_buf)); return 1;} else if ((bd_shared_rx_buf[1] == I) (bd_shared_rx_buf[2] == C)) //收到IC信息$ICXX { bd_buf_bitmap |= ICXX_BUF; memcpy(icxx_buf, bd_shared_rx_buf, sizeof(icxx_buf)); return 2;} else if ((bd_shared_rx_buf[1] == Z) (bd_shared_rx_buf[2] == J)) //收到自检信息$ZJXX { bd_buf_bitmap |= ZJXX_BUF; memcpy(zjxx_buf, bd_shared_rx_buf, sizeof(zjxx_buf)); return 3;} else if ((bd_shared_rx_buf[1] == S) (bd_shared_rx_buf[2] == J)) //收到时间信息$SJXX { bd_buf_bitmap |= SJXX_BUF; //memcpy(sjxx_buf, bd_shared_rx_buf, sizeof(sjxx_buf)); return 4;} else if ((bd_shared_rx_buf[1] == B) (bd_shared_rx_buf[2] == B)) //收到版本信息$BBXX { bd_buf_bitmap |= BBXX_BUF; //memcpy(bbxx_buf, bd_shared_rx_buf, sizeof(bbxx_buf)); return 5;} else if ((bd_shared_rx_buf[1] == F) (bd_shared_rx_buf[2] == K)) //收到反馈信息$FKXX { bd_buf_bitmap |= FKXX_BUF; memcpy(fkxx_buf, bd_shared_rx_buf, sizeof(fkxx_buf)); return 6;} return -1;} void parse_zjxx(struct zjxx_struct *zjxx){int i;for (i = 0; i { (*zjxx).instruction[i] = zjxx_buf[i]; } (*zjxx).packet_len = zjxx_buf[5] * 256 + zjxx_buf[6]; for (i = 0; i { (*zjxx).user_addr[i] = zjxx_buf[i + 7]; } }void parse_icxx(struct icxx_struct *icxx){int i;for (i = 0; i { (*icxx).instruction[i] = icxx_buf[i]; } (*icxx).packet_len = icxx_buf[5] * 256 + icxx_buf[6]; for (i = 0; i { (*icxx).user_addr[i] = icxx_buf[i + 7]; } }void parse_fkxx(struct fkxx_struct *fkxx) { unsigned int i; for (i = 0; i { (*fkxx).instruction[i] = fkxx_buf[i]; } (*fkxx).packet_len = fkxx_buf[5] * 256 + fkxx_buf[6]; for (i = 0; i { (*fkxx).user_addr[i] = fkxx_buf[i + 7]; } (*fkxx).fkxx_info.fk_flag = fkxx_buf[10]; for (i = 0; i { (*fkxx).fkxx_info.extra_info[i] = fkxx_buf[11 + i]; } (*fkxx).checksum = fkxx_buf[15]; } #endifint open_msgport(){int fd;fd = open( &"/dev/ttyUSB2&", O_RDWR);if (-1 == fd){perror(&"Cant Open ttyUSB0 Serial Port&");return(-1);}else {printf(&"open ttyUSB1 .....\n&");} #if 1if(fcntl(fd, F_SETFL, 0){printf(&"fcntl failed!\n&");}else{printf(&"fcntl=%d\n&",fcntl(fd, F_SETFL,0));}#endifreturn fd;}int open_msgport_test(){int fd;fd = open( &"/dev/ttyUSB2&", O_RDWR);if (-1 == fd){perror(&"Cant Open ttyUSB2 Serial Port&");return(-1);}else {printf(&"open ttyUSB2 .....\n&");} #if 1if(fcntl(fd, F_SETFL, 0){printf(&"fcntl failed!\n&");}else{printf(&"fcntl=%d\n&",fcntl(fd, F_SETFL,0));}#endifreturn fd;}void send_icjc() // {int ret;unsigned char send_iczj_data[ICJC_FIRM_SIZE]; send_iczj_data[0] = $; send_iczj_data[1] = I; send_iczj_data[2] = C; send_iczj_data[3] = J; send_iczj_data[4] = C; send_iczj_data[5] = 0; send_iczj_data[6] = 12; send_iczj_data[7] = 0; send_iczj_data[8] = 0 ; send_iczj_data[9] = 0; send_iczj_data[10] = 0; send_iczj_data[11] = xor_checksum(send_iczj_data,11);printf(&"%s\n&",send_iczj_data);ret = write(g_msg_fd,send_iczj_data,12);printf(&"ret = %d\n&",ret);}void bd_msg_send(){if (g_msg_fd printf(&"msg port error,return\n&");} send_txsq(1,NULL,NULL,0,NULL,0);// send_icjc();return;}void thread_msg_recv(){struct txxx_struct mytxxx;struct fkxx_struct myfkxx;struct zjxx_struct myzjxx;struct icxx_struct myicxx;int ret,i;if (g_msg_fd printf(&"msg port error,return\n&");}while(1) {ret = read(g_msg_fd,bd_shared_rx_buf,RX_BD_MAX_DATA_SIZE);//报文的串口输出,读取的数据存放在bd_shared_rx_buf中printf(&"msg read ret = %d\n&",ret);bd_shared_rx_buf[ret] = \0;#if 1printf(&"data:&");for(i = 0;i printf(&"%c &",bd_shared_rx_buf[i]);}printf(&"\r\n&");printf(&"111111111\n&");#endif// printf(&"data: %s\n&",bd_shared_rx_buf);tcflush(g_msg_fd, TCIOFLUSH ); //刷新串口bufferret = copy_packet_from_shared_buf();//初步解析bd_shared_rx_bufif (ret printf(&"recv msg error\n&");}switch(ret) {case 0:break;case 1://当前主要使用第1类信息,即通信信息parse_txxx(mytxxx);//数据解析到mytxxx中mytxxx.txxx_info.payload[mytxxx.txxx_info.payload_len] = \0; printf(&"recv msg: %s\n&",mytxxx.txxx_info.payload);printf(&"user addr: &");for (i = 0;iprintf(&"0x%x &",mytxxx.user_addr[i]);}printf(&"\r\n&");printf(&"src user addr:&");for (i=0;iprintf(&"0x%x &",mytxxx.txxx_info.src_user_addr[i]);}printf(&"\r\n&"); //*********************此处添加实际应用代码******************************break;case 2:parse_icxx(myicxx);printf(&" user addr:&");for (i=0;iprintf(&"0x%x &",myicxx.user_addr[i]);}printf(&"\r\n&"); break; case 3:parse_zjxx(myzjxx);printf(&" user addr:&");for (i=0;iprintf(&"0x%x &",myzjxx.user_addr[i]);}printf(&"\r\n&"); break; case 4:break; case 5:break;case 6: //发送失败时的反馈信息处理parse_fkxx(myfkxx);printf(&" user addr:&");for (i=0;iprintf(&"0x%x &",myfkxx.user_addr[i]);}printf(&"\r\n&"); printf(&"fsxx flag is %x\n&",myfkxx.fkxx_info.fk_flag );if (myfkxx.fkxx_info.extra_info[0] == Tmyfkxx.fkxx_info.extra_info[1] == Xmyfkxx.fkxx_info.extra_info[2] == Smyfkxx.fkxx_info.extra_info[3] == Q) {if (myfkxx.fkxx_info.fk_flag == 0x0) {printf(&"TXSQ CMD send ok\n&");} else {printf(&"TXSQ CMD send error\n&");}}break;default :printf(&"msg resolve error\n&");break;}sleep(1);}}#if 0void thread_msg_recv_test(){struct txxx_struct mytxxx;struct fkxx_struct myfkxx;int ret,i;if (g_sins_gps_fd printf(&"msg port error,return\n&");}while(1) {read(g_sins_gps_fd,bd_shared_rx_buf,RX_BD_MAX_DATA_SIZE);tcflush(g_sins_gps_fd, TCIOFLUSH ); ret = copy_packet_from_shared_buf();if (ret printf(&"recv msg error\n&");}switch(ret) {case 0:parse_txxx(mytxxx);mytxxx.txxx_info.payload[mytxxx.txxx_info.payload_len] = \0; printf(&"recv msg: %s\n&",mytxxx.txxx_info.payload);printf(&"user addr: &");for (i = 0;iprintf(&"0x%x &",mytxxx.user_addr[i]);}printf(&"\r\n&");printf(&"src user addr:&");for (i=0;iprintf(&"0x%x &",mytxxx.txxx_info.src_user_addr[i]);}printf(&"\r\n&"); break;case 1:parse_fkxx(myfkxx);if (myfkxx.fkxx_info.extra_info[0] == Tmyfkxx.fkxx_info.extra_info[1] == Xmyfkxx.fkxx_info.extra_info[2] == Smyfkxx.fkxx_info.extra_info[3] == Q) {if (myfkxx.fkxx_info.fk_flag == 0x0) {printf(&"TXSQ CMD send ok\n&");} else {printf(&"TXSQ CMD send error\n&");}}break;}sleep(1);}}#endif& 转自:http://ass.3daixie.com/2018061951098595.html