/// <summary> /// 接收到数据的委托方法,包括更新Form1各实例的属性,来改变外观,以及返回报文的 /// </summary> /// <param name="cm"></param> private void connect_dataReceived(SktMsgDt cm) { //用MESSAGE类的未拆分DATA属性来添加日志内容 string logdata = Tools.byteToHexStr(cm.data); string sourcestr = ((System.Net.IPEndPoint)cm.socket.RemoteEndPoint).Address.ToString() + ":" + ((System.Net.IPEndPoint)cm.socket.RemoteEndPoint).Port.ToString(); string str = "\r\n\r\n" + "<<<<" + sourcestr + " " + DateTime.Now.ToString() + "\r\n" + logdata; if (textBox50.InvokeRequired) { addLogRichTextBoxCallback callback = new addLogRichTextBoxCallback(addLog); Invoke(callback, new object[] { str }); } else { addLog(str); } //用MESSAGE类的IP和PORT属性改变客户端IP的combox选项外观 //string clientstr = ((System.Net.IPEndPoint)cm.socket.RemoteEndPoint).Address.ToString() + ":" + ((IPEndPoint)cm.socket.RemoteEndPoint).Port.ToString(); for (int i = 0; i < ClientIPCombobox.Items.Count; i++) { if (ClientIPCombobox.Items[i].ToString() == sourcestr) { OpenMainFormControl(delegate() { ClientIPCombobox.SelectedIndex = i; }); } } //拆分MESSAGE类的DATA //DATA //68 //21 00 //30 04 //01 //00 //23 06 15 10 12 12 34 //00 00 00 00 00 00 02 //07 00 //23 06 15 10 12 12 34 //A5 CO //16 byte[] revdata = Encoding.ASCII.GetBytes(cm.message); //string revstr = Tools.byteToHexStr(revdata).Replace(" ","");//和下面一样 string revstr1 = logdata.Replace(" ", ""); //和上面一样 string crccode1 = revstr1.Substring(revstr1.Length - 6, 4); //校验码 string crccode2 = crccode1.Substring(2, 2) + crccode1.Substring(0, 2); string cmdcode = revstr1.Substring(6, 4); //命令码 meterID = revstr1.Substring(14, 14); //表ID messageID = revstr1.Substring(28, 14); //报文ID //用revdata来获得校验码和形成校验码的前面数据,并对照 //byte[] revcrccodebyte = new byte[2]; //revcrccodebyte[0] = revdata[revdata.Length - 3]; //revcrccodebyte[1] = revdata[revdata.Length - 2]; //string revcrccodestr = Tools.byteToHexStr(revcrccodebyte).Replace(" ", ""); string calcrccode = Check.getCrcStr(revstr1.Replace(" ", "")); string calcrccode1 = calcrccode.Substring(2, 2) + calcrccode.Substring(0, 2); if (crccode1 == calcrccode) { if (FormMessageTextBox.InvokeRequired) { string formstr = Environment.NewLine + "\n原校验码为: " + crccode2 + "\r\n计算校验码为: " + calcrccode1 + "\r\n校验码相同\r\n"; changeFormMessageText callback = new changeFormMessageText(changeFormMessage); Invoke(callback, new object[] { formstr }); } Console.WriteLine("\r\n校验码校验通过"); } else { if (FormMessageTextBox.InvokeRequired) { string formstr = "\r\n原校验码为: " + crccode2 + "\r\n计算校验码为: " + calcrccode1 + "\r\n校验码不相同\r\n"; changeFormMessageText callback = new changeFormMessageText(changeFormMessage); Invoke(callback, new object[] { formstr }); } Console.WriteLine("\r\n校验码校验没通过"); } //改变MessageToAccept的外观和MessageToSend的外观 for (int j = 0; j < cmdlist.Count; j++) { if (cmdlist[j]["cmdc2s"].Split('|')[2] == cmdcode)//找到对应的命令码 { OpenMainFormControl(delegate() { comboBox1.SelectedIndex = j; /* * if (cmdcode == "3002" || cmdcode == "3003" || cmdcode == "3004" || cmdcode == "3043" || cmdcode == "3062") * { * s2ctextboxlist[5].Text = meterID; * s2ctextboxlist[6].Text = messageID; * s2ctextboxlist[9].Text = meterID; * joinStr(); * sendData(); * } */ }); } } string messagelength, datalength, identifingcode, responsecode; string readmetertime, readmeterdate, meterphase, tapstate; string occurdate, occurtime, exceptiontype; string askforresponsecode; messagelength = revstr1.Substring(4, 2) + revstr1.Substring(2, 2); askforresponsecode = revstr1.Substring(12, 2); datalength = revstr1.Substring(42, 4); responsecode = revstr1.Substring(46, 4); string crccode = crccode1.Substring(2, 2) + crccode1.Substring(0, 2); try { switch (cmdcode) { case "3001": //identifingcode = revstr1.Substring(64, 4); OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = responsecode; c2stextboxlist[9].Text = meterID; c2stextboxlist[10].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[8].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3002": OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = meterID; c2stextboxlist[9].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[9].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3003": OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = meterID; c2stextboxlist[9].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[9].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3004": OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = meterID; c2stextboxlist[9].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[9].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3042": readmeterdate = revstr1.Substring(64, 4) + "-" + revstr1.Substring(68, 2) + "-" + revstr1.Substring(70, 2); readmetertime = revstr1.Substring(72, 2) + ":" + revstr1.Substring(74, 2) + ":" + revstr1.Substring(76, 2);; string meterphase1 = revstr1.Substring(78, 12); //68 00 15 30 43 01 01 01 12 40 00 00 00 00 00 52 06 51 12 23 20 00 15 01 12 40 00 00 00 00 20 15 10 31 16 29 00 //2C 01 73 00 00 00 01 //46 E5 //16 int meterphase2_1 = Convert.ToInt32(Tools.GetHexData(meterphase1).Substring(0, 8), 16); int meterphase2_2 = Convert.ToInt32(Tools.GetHexData(meterphase1).Substring(8, 4), 16); string meterphase3 = Convert.ToString(meterphase2_1) + '.' + Convert.ToString(meterphase2_2); meterphase = meterphase3; tapstate = revstr1.Substring(90, 2); OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = responsecode; c2stextboxlist[9].Text = meterID; c2stextboxlist[10].Text = readmeterdate; c2stextboxlist[11].Text = readmetertime; c2stextboxlist[12].Text = meterphase; c2stextboxlist[13].Text = tapstate; c2stextboxlist[14].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[8].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3043": readmeterdate = revstr1.Substring(60, 4) + "-" + revstr1.Substring(64, 2) + "-" + revstr1.Substring(66, 2); readmetertime = revstr1.Substring(68, 2) + ":" + revstr1.Substring(70, 2) + ":" + revstr1.Substring(72, 2); meterphase1 = revstr1.Substring(74, 12); meterphase2_1 = Convert.ToInt32(Tools.GetHexData(meterphase1).Substring(0, 8), 16); meterphase2_2 = Convert.ToInt32(Tools.GetHexData(meterphase1).Substring(8, 4), 16); meterphase3 = Convert.ToString(meterphase2_1) + '.' + Convert.ToString(meterphase2_2); meterphase = meterphase3; tapstate = revstr1.Substring(86, 2); identifingcode = revstr1.Substring(88, 4); OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = meterID; c2stextboxlist[9].Text = readmeterdate; c2stextboxlist[10].Text = readmetertime; c2stextboxlist[11].Text = meterphase; c2stextboxlist[12].Text = tapstate; c2stextboxlist[13].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[9].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3051": OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = responsecode; c2stextboxlist[9].Text = meterID; c2stextboxlist[10].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[8].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3052": OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = responsecode; c2stextboxlist[9].Text = meterID; c2stextboxlist[10].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[8].Text = meterID; if (askforresponsecode == "00") { joinStr(); Console.WriteLine("\n已响应物联网表"); sendData(); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; case "3062": occurdate = revstr1.Substring(60, 8); occurtime = revstr1.Substring(68, 6); exceptiontype = revstr1.Substring(74, 4); //identifingcode = revstr1.Substring(78, 4); OpenMainFormControl(delegate() { c2stextboxlist[1].Text = messagelength; c2stextboxlist[5].Text = meterID; c2stextboxlist[6].Text = messageID; //c2stextboxlist[7].Text = datalength; c2stextboxlist[8].Text = meterID; c2stextboxlist[9].Text = occurdate; c2stextboxlist[10].Text = occurtime; c2stextboxlist[11].Text = exceptiontype; c2stextboxlist[12].Text = crccode; s2ctextboxlist[5].Text = meterID; s2ctextboxlist[6].Text = messageID; s2ctextboxlist[9].Text = meterID; if (askforresponsecode == "00") { joinStr(); sendData(); Console.WriteLine("\n已响应物联网表"); } else if (askforresponsecode == "01") { } else { Console.WriteLine("响应码有误"); } }); break; } } catch (Exception ex) { Console.WriteLine("输入数据错误产生了异常,一般是手动测试时的数据长度有误"); MessageBox.Show("数据异常" + ex); } }
/// <summary> /// 利用clientsocket获取信息,创建并更新SktMsgDt的实例(其实就是产生一个SktMsgDt的实例并返回给主控域) /// </summary> /// <param name="ClientSocket"></param> public void MessageReceived(object ClientSocket) { Socket myClientSocket = (Socket)ClientSocket; while (true) { if (serverSocket == null) { break; } if (ClientSocket == null) { break; } try { //通过clientSocket接收数据 int receiveNumber = myClientSocket.Receive(datareceived); //收到空数组表示客户端已断开 if (receiveNumber == 0) { myClientSocket.Shutdown(SocketShutdown.Both); clientsocketList.Remove(myClientSocket); if (clientChanged != null) { clientChanged(clientsocketList); } myClientSocket.Close(); break; } // if (receiveNumber <= 0) { continue; } Console.WriteLine("接收客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(datareceived, 0, receiveNumber)); //获得data的原始数据 byte[] data = new byte[receiveNumber]; for (int i = 0; i < data.Length; i++) { data[i] = datareceived[i]; } cm = new SktMsgDt(myClientSocket, Encoding.ASCII.GetString(data, 0, receiveNumber), data); if (dataReceived != null) { dataReceived(cm); } } catch (Exception ex) { if (myClientSocket == null) { return; } Console.WriteLine(ex.Message); //myClientSocket.Shutdown(SocketShutdown.Both); clientsocketList.Remove(myClientSocket); if (clientChanged != null) { clientChanged(clientsocketList); } myClientSocket.Close(); break; } } }