private void Bt_Start_Click(object sender, EventArgs e) { RefreshCmbChooseEquip(); /* * foreach(KeyValuePair<string,DTSEquip> kvp in Equips) * { * for(int i=0;i<kvp.Value.channelInfo.Count;i++) * { * kvp.Value.channelInfo[i].SlaveNum = (ushort)(kvp.Value.SlaveNum + kvp.Value.channelInfo[i].ChannelNum); * } * } */ ServerIP = cmb_ServerIP.SelectedItem.ToString(); if (Bt_Start.Text == "启 动") { ServerStart.Create().Start(); Bt_Start.Text = "停 止"; } else { ServerStart.Create().ServerStop(); Bt_Start.Text = "启 动"; } }
private void Main_FormClosed(object sender, FormClosedEventArgs e) { if (Bt_Start.Text == "停 止") { ServerStart.Create().ServerStop(); } }
private void Bt_Start_Click(object sender, EventArgs e) { RefreshCmbChooseEquip(); if (Bt_Start.Text == "启 动") { ServerStart.Create().Start(); Bt_Start.Text = "停 止"; } else { ServerStart.Create().ServerStop(); Bt_Start.Text = "启 动"; } }
/// <summary> /// 接收客户端发送的消息 /// </summary> /// <param name="newSocket"></param> void Receive(Socket newSocket) { int oldadd = 0; int newadd = 0; RegistData rd = null; ChannelTempInfo ci = new ChannelTempInfo(); int SerialNum = -1; //流水号 List <byte> response = new List <byte>(); while (newSocket != null && newSocket.Connected) { try { byte[] requetData = new byte[12]; //Map报文头+字节数 requetData = SocketRead(newSocket, requetData.Length); ushort slavenum = requetData[6]; //站号 var address = requetData[8] * 256 + requetData[9]; //寄存器地址 var registercout = requetData[10] * 256 + requetData[11]; //寄存器个数 byte[] responseDataHead = new byte[9]; Buffer.BlockCopy(requetData, 0, responseDataHead, 0, 8); responseDataHead[5] = (byte)(3 + registercout * 2); responseDataHead[8] = (byte)(registercout * 2); switch (requetData[7]) //功能码 { //读保持寄存器 case 3: { if (ServerStart.Create().DTS.Keys.Contains(this.equipnum)) { int equipslavenum = ServerStart.Create().DTS[this.equipnum].SlaveNum; int channelnum = Math.Abs(slavenum - equipslavenum); if (BitConverter.ToUInt16(requetData, 0) != SerialNum) { response.Clear(); object data = ServerStart.Create().DTS[this.equipnum].dataMange.PopData(this.equipnum, slavenum); if (channelnum == 0) { //设备基本信息 } else { if (data != null) { byte[] responseData1 = EquipChannelTempInfo.ProtocolConvert((ChannelTempInfo)data); response.AddRange(responseData1); /* byte[] holdregisterdata = new byte[9 + registercout * 2]; * Buffer.BlockCopy(responseDataHead, 0, holdregisterdata, 0, 8); * Buffer.BlockCopy(response.ToArray(), address * 2, holdregisterdata, 9, registercout * 2); * newSocket.Send(holdregisterdata); */ if (this.equipnum == Main.oldEquipNum) { ci = (ChannelTempInfo)data; Main.channelCurvers[ci.ChannelNum.ToString()].DrawSendData(ci); Main.ObjRefreshZed(ci.ChannelNum.ToString()); // Thread.Sleep(1000); } } } SerialNum = BitConverter.ToUInt16(requetData, 0); } if (response.Count > 0) { byte[] holdregisterdata = new byte[9 + registercout * 2]; Buffer.BlockCopy(responseDataHead, 0, holdregisterdata, 0, 9); Buffer.BlockCopy(response.ToArray(), address * 2, holdregisterdata, 9, registercout * 2); newSocket.Send(holdregisterdata); System.Text.StringBuilder builder = new System.Text.StringBuilder(); for (int i = 0; i < requetData.Length; i++) { builder.Append(string.Format("{0:X2} ", requetData[i])); } Debug.WriteLine("接收:" + builder.ToString()); builder.Clear(); for (int i = 0; i < holdregisterdata.Length; i++) { builder.Append(string.Format("{0:X2} ", holdregisterdata[i])); } Debug.WriteLine("发送:" + builder.ToString()); } } } break; //读输入寄存器 case 4: { /* * //当前位置到最后的长度 * responseData1[4] = (byte)((3 + registercout * 2) / 256); * responseData1[5] = (byte)((3 + registercout * 2) % 256); * byte[] responseData2 = new byte[1 + registercout * 2]; * responseData2[0] = (byte)(registercout * 2); * for (int i = 1; i < responseData2.Length; i++) * responseData2[i] = 0xff; * if (address >= 0 && address < 65536) * { * newadd = address; * rd = ServerStart.Create().PosRegistdata(equipnum, slavenum.ToString()); * if (rd != null) * { * byte[] byteArray = rd.inputRegistData; * * if (address * 2 < byteArray.Length) * { * if ((address + registercout) * 2 <= byteArray.Length) * { * Buffer.BlockCopy(byteArray, address * 2, responseData2, 1, registercout * 2); * } * else * { * int len = ((address + registercout) * 2) - byteArray.Length; * byte[] b = new byte[len]; * for (int i = 0; i < len; i++) * b[i] = 0xff; * * byte[] temp = byteArray.Concat(b).ToArray(); * Buffer.BlockCopy(temp, address * 2, responseData2, 1, registercout * 2); * } * var responseData = responseData1.Concat(responseData2).ToArray(); * newSocket.Send(responseData); * * oldadd = address; * * } * } * } */ } break; } } catch (Exception ex) { connectsuccessflag = false; if (newSocket?.Connected ?? false) { newSocket?.Shutdown(SocketShutdown.Both); } newSocket?.Close(); } } }
/// <summary> /// 接收客户端发送的消息 /// </summary> /// <param name="newSocket"></param> void Receive(Socket newSocket) { int oldadd = 0; int newadd = 0; RegistData rd = null; while (newSocket != null && newSocket.Connected) { try { byte[] requetData1 = new byte[8]; //Map报文头+功能码 requetData1 = SocketRead(newSocket, requetData1.Length); byte[] requetData2 = new byte[requetData1[5] - 2]; //数据 requetData2 = SocketRead(newSocket, requetData2.Length); var requetData = requetData1.Concat(requetData2).ToArray(); //完整的数据请求报文 byte[] responseData1 = new byte[8]; Buffer.BlockCopy(requetData, 0, responseData1, 0, responseData1.Length); string stationNumberKey = string.Format("{0}", requetData[6]); //站号 var address = requetData[8] * 256 + requetData[9]; //寄存器地址 var registerLenght = requetData[10] * 256 + requetData[11]; //寄存器的长度 switch (requetData[7]) //功能码 { //读保持寄存器 case 3: { /* * //当前位置到最后的长度 * responseData1[4] = (byte)((3 + registerLenght * 2) / 256); * responseData1[5] = (byte)((3 + registerLenght * 2) % 256); * byte[] responseData2 = new byte[1 + registerLenght * 2]; * responseData2[0] = (byte)(registerLenght * 2); * for (int i = 1; i < responseData2.Length; i++) * responseData2[i] = 0xff; * if (address >= 0 && address < 6000) * { * RegistData rd = RegistDatas.Create().PosRegistdata(stationNumberKey); * if (rd != null) * { * byte[] byteArray = rd.holdRegistData; * Buffer.BlockCopy(byteArray, address * 2, responseData2, 1, registerLenght * 2); * * var responseData = responseData1.Concat(responseData2).ToArray(); * newSocket.Send(responseData); * } * } */ } break; //读输入寄存器 case 4: { //当前位置到最后的长度 responseData1[4] = (byte)((3 + registerLenght * 2) / 256); responseData1[5] = (byte)((3 + registerLenght * 2) % 256); byte[] responseData2 = new byte[1 + registerLenght * 2]; responseData2[0] = (byte)(registerLenght * 2); for (int i = 1; i < responseData2.Length; i++) { responseData2[i] = 0xff; } if (address >= 0 && address < 65536) { newadd = address; rd = ServerStart.Create().PosRegistdata(equipnum, stationNumberKey); if (rd != null) { byte[] byteArray = rd.inputRegistData; if (address * 2 < byteArray.Length) { if ((address + registerLenght) * 2 <= byteArray.Length) { Buffer.BlockCopy(byteArray, address * 2, responseData2, 1, registerLenght * 2); } else { int len = ((address + registerLenght) * 2) - byteArray.Length; byte[] b = new byte[len]; for (int i = 0; i < len; i++) { b[i] = 0xff; } byte[] temp = byteArray.Concat(b).ToArray(); Buffer.BlockCopy(temp, address * 2, responseData2, 1, registerLenght * 2); } var responseData = responseData1.Concat(responseData2).ToArray(); newSocket.Send(responseData); oldadd = address; } } } } break; } } catch (Exception ex) { connectsuccessflag = false; if (newSocket?.Connected ?? false) { newSocket?.Shutdown(SocketShutdown.Both); } newSocket?.Close(); } } }