private void RecvFromClientZK1(object ClientSocket) { Trace.WriteLine("RecvFromClientZK1!!"); Socket myClientSocket = (Socket)ClientSocket; while (ServerOn && myClientSocket.Connected) { try { byte[] RecvBufZK1 = new byte[4096]; int RecvNum = myClientSocket.Receive(RecvBufZK1); if (RecvNum > 0) { Data.dtYK.Rows[0]["数量"] = (int)Data.dtYK.Rows[0]["数量"] + 1; String tempstr = ""; byte[] RecvBufToFile = new byte[RecvNum]; for (int i = 0; i < RecvNum; i++) { RecvBufToFile[i] = RecvBufZK1[i]; tempstr += RecvBufZK1[i].ToString("x2"); } Trace.WriteLine(tempstr); // SaveFile.DataQueue_in1.Enqueue(tempstr); SaveFile.DataQueue_out1.Enqueue(RecvBufToFile); if (RecvNum >= 45 && RecvNum < 300) { MyLog.Info("收到遥测数据量:" + RecvNum.ToString()); Data.dtYK.Rows[1]["数量"] = (int)Data.dtYK.Rows[1]["数量"] + 1; deal_zk_data(RecvBufZK1, RecvNum); } else { Data.dtYK.Rows[2]["数量"] = (int)Data.dtYK.Rows[2]["数量"] + 1; } } else { Trace.WriteLine("收到数据少于等于0!"); break; } } catch (Exception e) { Trace.WriteLine("RecvFromClientZK1 Exception:" + e.Message); if (myClientSocket.Connected) { myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); } ClientAPP.ClientZK1.IsConnected = false; break; } } if (myClientSocket.Connected) { Trace.WriteLine("服务器主动关闭socket!"); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); } ClientAPP.ClientZK1.IsConnected = false; Trace.WriteLine("leave RecvFromClientZK1!!"); Data.ServerConnectEvent.Set(); }
private void RecvFromClientGT(object ClientSocket) { Trace.WriteLine("RecvFromClientZK1_YC!!"); Socket myClientSocket = (Socket)ClientSocket; while (ServerOn_YC && myClientSocket.Connected) { try { byte[] RecvBufZK1 = new byte[2048]; int RecvNum = myClientSocket.Receive(RecvBufZK1); if (RecvNum > 0) { String tempstr = ""; byte[] RecvBufToFile = new byte[RecvNum]; for (int i = 0; i < RecvNum; i++) { RecvBufToFile[i] = RecvBufZK1[i]; tempstr += RecvBufZK1[i].ToString("x2"); } Trace.WriteLine(tempstr); //存储总控发来的 // SaveFile.DataQueue_1.Enqueue(RecvBufZK1); if (RecvNum > 29) { MyLog.Info("收到遥测数据量:" + RecvNum.ToString()); //Trace.WriteLine("网络收到数据量" + RecvNum.ToString()); deal_zk_data(RecvBufZK1, RecvNum); } } else { Trace.WriteLine("收到数据少于等于0!"); break; } } catch (Exception e) { Trace.WriteLine("RecvFromClientZK_YC Exception:" + e.Message); if (myClientSocket.Connected) { myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); } ClientAPP.ClientZK1_YC.IsConnected = false; break; } } if (myClientSocket.Connected) { Trace.WriteLine("服务器主动关闭socket!"); myClientSocket.Shutdown(SocketShutdown.Both); myClientSocket.Close(); } ClientAPP.ClientZK1_YC.IsConnected = false; Trace.WriteLine("leave Server YC!!"); Data.ServerConnectEvent2.Set(); }
void DealCRT_Off(ref Data.CRT_STRUCT myCRT) { myCRT.LedOff(); MyLog.Info("无法连接--" + myCRT.CRTName); }
/// <summary> /// 收到总控发来的网络数据包,解析并推入对应的队列中 /// </summary> /// <param name="data"></param> public void deal_zk_data(byte[] data, int RecvNum) { byte[] DealData = new byte[RecvNum];//收到的实际数组,data后面可能包含0? Trace.WriteLine("收到数据量" + RecvNum.ToString()); Array.Copy(data, DealData, RecvNum); string printstr = null; for (int i = 0; i < RecvNum; i++) { printstr += DealData[i].ToString("x2"); } Trace.WriteLine("1111-------" + printstr); byte[] infoflag = new byte[4]; infoflag[0] = byte.Parse(DealData[18].ToString()); infoflag[1] = byte.Parse(DealData[19].ToString()); infoflag[2] = byte.Parse(DealData[20].ToString()); infoflag[3] = byte.Parse(DealData[21].ToString()); string type_str = Encoding.ASCII.GetString(infoflag); Trace.WriteLine("type_str is :" + type_str); MyLog.Info("收到总控--:" + type_str + "数据量:" + RecvNum.ToString() + "内容:" + printstr); switch (type_str) { case "UCLK": //校时信息 //settime //######## byte[] timedata = new byte[23]; Array.Copy(data, 29, timedata, 0, 23); Function.Set_Time(timedata); break; case "MASG": //主备当班 // Encoding.ASCII.GetString(RecvBufZK2, 0, RecvNum) //解析判断 MyLog.Info(Encoding.ASCII.GetString(DealData, 29, RecvNum - 29)); Console.WriteLine("接受客户端{0}", Encoding.ASCII.GetString(DealData, 29, RecvNum - 29)); //返回应答信息码 //service.send(data) break; case "CCUK": case "CCUA": //总控-->遥控-->USB应答机a(遥控K令及密钥/算法注入) DealWithZK2CRT_K(DealData, ref Data.DealCRTa); break; case "DCUA": //总控-->遥控-->USB应答机a(遥控注数) DealWithZK2CRT_PB(DealData, ref Data.DealCRTa); break; case "DCUZ": case "CCUG": //收到对地测控上行遥控数据 int len = DealData.Length - 45; byte[] MsgHead = new byte[16]; byte[] MsgBody = new byte[len]; Array.Copy(DealData, 29, MsgHead, 0, 16); Array.Copy(DealData, 45, MsgBody, 0, len); //遥控注数应答 byte[] Return_data = new byte[len + 17]; //16head+len(Body),+1Byte retrun code MsgHead.CopyTo(Return_data, 0); MsgBody.CopyTo(Return_data, 16); Return_data[len + 16] = 0x30; byte[] CmdType = new byte[4]; CmdType[0] = byte.Parse(MsgBody[0].ToString()); CmdType[1] = byte.Parse(MsgBody[1].ToString()); CmdType[2] = byte.Parse(MsgBody[2].ToString()); CmdType[3] = byte.Parse(MsgBody[3].ToString()); string tempCmdTypeStr = Encoding.ASCII.GetString(CmdType); if (tempCmdTypeStr != "0003") { Return_data[len + 16] = 0x31; } byte[] Return_Send = Function.Make_tozk_frame(Data.Data_Flag_Real, Data.InfoFlag_KACK, Return_data); Data.DataQueue_ZK_ACK.Enqueue(Return_Send); break; default: break; } }
void DealCRT_On(ref Data.CRT_STRUCT myCRT) { myCRT.LedOn(); MyLog.Info("连接成功--" + myCRT.CRTName); }