private void Fun_RecvFromCRT(ref Data.CRT_STRUCT myCRT, ref ClientAPP.TCP_STRUCT Server_CRT) { Trace.WriteLine("Entering" + myCRT.CRTName + "Fun_RecvFromCRT!!"); while (Server_CRT.IsConnected) { try { byte[] RecvBufCRTa = new byte[1024]; int RecvNum = Server_CRT.sck.Receive(RecvBufCRTa); if (RecvNum > 0) { int[] RecvBufInt = Program.BytesToInt(RecvBufCRTa); myCRT.TCMsgStatus = RecvBufInt[7]; } else { Trace.WriteLine("收到数据少于等于0!"); break; } } catch (Exception e) { Console.WriteLine(e.Message); Trace.WriteLine("Exception leave!!"); break; } } }
private void Fun_Transfer2CRT(ref Data.CRT_STRUCT myCRT, ref ClientAPP.TCP_STRUCT Server_CRT, ref Queue <byte[]> DataQueue_save) { Delegate la = new UpdateText(UpdateTextMethod); while (Server_CRT.IsConnected) { if (myCRT.DataQueue_CRT.Count() > 0) { byte[] SendByte = myCRT.DataQueue_CRT.Dequeue(); Server_CRT.sck.Send(SendByte); Data.dtYK.Rows[3]["数量"] = (int)Data.dtYK.Rows[3]["数量"] + 1; //增加存储,DataQueue_save为引用的对应SaveFile里面的Queue DataQueue_save.Enqueue(SendByte); myCRT.Transfer2CRTa_TempStr = ""; for (int m = 24; m < SendByte.Length - 8; m++) { myCRT.Transfer2CRTa_TempStr += SendByte[m].ToString("x2"); } Trace.WriteLine("Fun_Transfer2CRT:" + myCRT.Transfer2CRTa_TempStr); } } }
private void DealWithZK2CRT_PB(byte[] data, ref Data.CRT_STRUCT myCRT) { //To do here byte[] MsgHead = new byte[17]; byte[] MsgBody = new byte[516]; Array.Copy(data, 29, MsgHead, 0, 17); Array.Copy(data, 46, MsgBody, 0, 516); //并未对于明密态做出处理 //Attention if (Data.MingMiTag) { MsgHead[4] = 0xF0; } else { MsgHead[4] = 0x0F; ushort CRC = 0xffff; ushort genpoly = 0x1021; for (int i = 2; i < 512; i = i + 1) { CRC = Function.CRChware(MsgBody[i], genpoly, CRC); } MsgBody[512] = (byte)((CRC & 0xFF00) >> 2); MsgBody[513] = (byte)(CRC & 0x00FF); } byte[] FinalSendToCRT = Function.Make_toCortex_frame(MsgBody); myCRT.DataQueue_CRT.Enqueue(FinalSendToCRT); //遥控注数应答 byte[] Return_data = new byte[534];//原数据包为533(4+12+1+516信息体),+1Byte retrun code MsgHead.CopyTo(Return_data, 0); MsgBody.CopyTo(Return_data, 4); Return_data[533] = 0x30; byte[] Return_Send = Function.Make_tozk_frame(Data.Data_Flag_Real, Data.InfoFlag_KACK, Return_data); Data.DataQueue_ZK_ACK.Enqueue(Return_Send); byte[] Return_XHL = new byte[Return_Send.Length]; Array.Copy(Return_Send, Return_XHL, Return_Send.Length); if (Data.WaitXHL_Return2ZK.WaitOne(500)) { Data.WaitXHL_Return2ZK.Reset(); Return_XHL[Return_XHL.Length - 1] = Data.ReturnCode; } else { Return_XHL[Return_XHL.Length - 1] = 0x37;//超时 } Data.DataQueue_ZK_ACK.Enqueue(Return_XHL); }
private void DealWithZK2CRT_K(byte[] data, ref Data.CRT_STRUCT myCRT) { //Data.DealKSSA.DataQueue_CRT.Enqueue(FinalSendToCRT); byte[] MsgHead = new byte[4]; byte[] MsgBody = new byte[12]; Array.Copy(data, 29, MsgHead, 0, 4); Array.Copy(data, 33, MsgBody, 0, 12); byte[] KCmdByte = new byte[9]; Function.Get_KcmdText(MsgBody[11], ref KCmdByte); byte[] ToBeSend = new byte[71]; //将K令数据组成遥控帧序列后发送 ToBeSend = Function.Make_toCRT_frame(KCmdByte, true); //明指令 //YKZL = Function.Make_toCRT_frame(temp2, false); //密指令 byte[] FinalSendToCRT = Function.Make_toCortex_frame(ToBeSend); myCRT.DataQueue_CRT.Enqueue(FinalSendToCRT); //遥控指令应答 byte[] Return_data = new byte[17]; MsgHead.CopyTo(Return_data, 0); MsgBody.CopyTo(Return_data, 4); Return_data[16] = 0x30; byte[] Return_Send = Function.Make_tozk_frame(Data.Data_Flag_Real, Data.InfoFlag_CACK, Return_data); Data.DataQueue_ZK_ACK.Enqueue(Return_Send); byte[] Return_XHL = new byte[Return_Send.Length]; Array.Copy(Return_Send, Return_XHL, Return_Send.Length); if (Data.WaitXHL_Return2ZK.WaitOne(500)) { Data.WaitXHL_Return2ZK.Reset(); Return_XHL[16] = Data.ReturnCode; } else { Return_XHL[16] = 0x37;//超时 } Data.DataQueue_ZK_ACK.Enqueue(Return_XHL); }
private void Fun_RecvFromCRT_Return(ref Data.CRT_STRUCT myCRT, ref ClientAPP.TCP_STRUCT Server_CRT) { Trace.WriteLine("++++++++++Entering" + myCRT.CRTName + "Fun_RecvFromCRT_Return!!"); //int[] SendReq; //byte[] SendReqBytes; ////send request //SendReq = new int[16]; //SendReq[0] = 1234567890; //Start of msg //SendReq[1] = 64; //Size of msg in bytes //SendReq[2] = 0; //Size of msg //SendReq[3] = 0; //0:channelA 1:channelB //SendReq[4] = 0; //Real time telemetry //SendReq[5] = 0; //Permanent flow 一次请求 //SendReq[15] = -1234567890; //SendReqBytes = Program.IntToBytes(SendReq); //int len = Server_CRT.sck.Send(SendReqBytes); while (Server_CRT.IsConnected) { try { byte[] RecvBufCRTa = new byte[200]; int RecvNum = Server_CRT.sck.Receive(RecvBufCRTa); if (RecvNum > 0) { //TempRecvBuf 本次收到的数据 byte[] TempRecvBuf = new byte[RecvNum]; Array.Copy(RecvBufCRTa, TempRecvBuf, RecvNum); SaveFile.Lock_Dat3.EnterWriteLock(); SaveFile.DataQueue_out3.Enqueue(TempRecvBuf); SaveFile.Lock_Dat3.ExitWriteLock(); String tempstr = ""; for (int i = 0; i < TempRecvBuf.Length; i++) { tempstr += TempRecvBuf[i].ToString("x2"); } Trace.WriteLine(tempstr); Data.dtYC.Rows[0]["数量"] = (int)Data.dtYC.Rows[0]["数量"] + 1; //收到总数 //YCBuf 本次收到的实际遥测数据 byte[] YCBuf = new byte[RecvNum - 68]; Array.Copy(RecvBufCRTa, 64, YCBuf, 0, RecvNum - 68); SaveFile.Lock_Dat4.EnterWriteLock(); SaveFile.DataQueue_out4.Enqueue(YCBuf); SaveFile.Lock_Dat4.ExitWriteLock(); String tempstr2 = ""; for (int i = 0; i < YCBuf.Length; i++) { tempstr2 += YCBuf[i].ToString("x2"); } Trace.WriteLine(tempstr2); //ushort CRC = 0xffff; //ushort genpoly = 0x1021; //for (int i = 0; i < YCBuf.Length-2; i = i + 1) //{ // CRC = Function.CRChware(YCBuf[i], genpoly, CRC); //} //// MyLog.Info("Calc 通道1 CRC = " + CRC.ToString("x4")); //Trace.WriteLine("Calc 通道1 CRC = " + CRC.ToString("x4")); } else { Trace.WriteLine("收到数据少于等于0!"); break; } } catch (Exception e) { Trace.WriteLine(e.Message); break; } } Trace.WriteLine("----------Leaving" + myCRT.CRTName + "Fun_RecvFromCRT_Return!!"); }
void DealCRT_Off(ref Data.CRT_STRUCT myCRT) { myCRT.LedOff(); MyLog.Info("无法连接--" + myCRT.CRTName); }
void DealCRT_On(ref Data.CRT_STRUCT myCRT) { myCRT.LedOn(); MyLog.Info("连接成功--" + myCRT.CRTName); }