/// <summary> /// 定时器方法,用于循环发送数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SendTimer_Tick(object sender, EventArgs e) { //将接收到的字符串数据转换为字节数据数据 byte[] UserData; byte[] sourData; string[] data = sendTextBox.Text.Split(new char[] { ' ' }); data = data.Where(s => !string.IsNullOrEmpty(s)).ToArray(); UserData = new byte[data.Length]; UserData = Array.ConvertAll(data, s => Convert.ToByte(s, 16)); sourData = UserData; if (mulCheckBox.Checked) { if (framegroup.Count < 1) { MessageBox.Show("节点地址不存在"); return; } int num = framegroup.Count; for (int i = 0; i < num; i++) { byte[] nodemac = Character.StringToBytes(framegroup[i]); frameNum6968++; if (frameNum6968 % 256 == 0) { frameNum6968++; } UserData = PacketProcess.SendCommand_68_serialtest(sourData, (byte)sourData.Length, framegroup[i], frameNum6968, false); Queue.Push(UserData); } return; } //打包数据帧 if (dataBagCheckBox.Checked) { frameNum6968++; if (frameNum6968 % 256 == 0) { frameNum6968++; } UserData = PacketProcess.SendCommand_68_serialtest(sourData, (byte)sourData.Length, nodeTextBox.Text, frameNum6968, frameNumCheckBox.Checked); } //发送数据 Queue.Push(UserData); //获取时间 DateTime dt = System.DateTime.Now; string date = dt.ToString("HH:mm:ss fff"); Console.WriteLine(date); //写入文件 if (this.TxFileCheckBox.Checked) { TxDataFile.SetValue("send", "Number", sendNum.ToString()); TxDataFile.SetValue("send", "data" + sendNum.ToString(), Character.BytesToString(UserData, 0, UserData.Length - 1, true) + " + " + date); sendNum++; } }
/// <summary> /// 发送按键方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void sendButton_Click(object sender, EventArgs e) { //发送不能为空 if (this.sendTextBox.Text == "") { return; } try { if (this.sendButton.Text == "发送") { if (this.TxLoopCheckBox.Checked) { this.SendTimer.Interval = Convert.ToInt32(this.timeTextBox.Text); this.SendTimer.Start(); this.sendButton.Text = "停止发送"; } if (string.IsNullOrEmpty(sendTextBox.Text)) { MessageBox.Show("请填写要发送的数据!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //将接收到的字符串数据转换为字节数据数据 byte[] UserData; byte[] linkData; string[] data = sendTextBox.Text.Split(new char[] { ' ' }); data = data.Where(s => !string.IsNullOrEmpty(s)).ToArray(); UserData = new byte[data.Length]; UserData = Array.ConvertAll(data, s => Convert.ToByte(s, 16)); linkData = UserData; if (mulCheckBox.Checked) { if (framegroup.Count < 1) { MessageBox.Show("节点地址不存在"); return; } int num = framegroup.Count; for (int i = 0; i < num; i++) { byte[] nodemac = Character.StringToBytes(framegroup[i]); frameNum6968++; if (frameNum6968 % 256 == 0) { frameNum6968++; } UserData = PacketProcess.SendCommand_68_serialtest(linkData, (byte)linkData.Length, framegroup[i], frameNum6968, false); Queue.Push(UserData); } return; } //打包数据帧 if (dataBagCheckBox.Checked) { byte[] nodeMac = Character.StringToBytes(nodeTextBox.Text); if (nodeMac.Length != 2) { MessageBox.Show("节点地址不正确"); return; } frameNum6968++; if (frameNum6968 % 256 == 0) { frameNum6968++; } //固定帧序号 if (frameNumCheckBox.Checked) { //frame.FrameCount = (ushort)6; } UserData = PacketProcess.SendCommand_68_serialtest(UserData, (byte)UserData.Length, nodeTextBox.Text, frameNum6968, frameNumCheckBox.Checked); } //发送数据 if (frameLinkCheckBox.Checked) { int num = Convert.ToInt32(frameLinkNumericUpDown.Value); byte[] array = UserData; UserData = new byte[array.Length * num]; for (int i = 0; i < num; i++) { if (dataBagCheckBox.Checked) { frameNum6968++; if (frameNum6968 % 256 == 0) { frameNum6968++; } array = PacketProcess.SendCommand_68_serialtest(linkData, (byte)linkData.Length, nodeTextBox.Text, frameNum6968, frameNumCheckBox.Checked); } array.CopyTo(UserData, array.Length * i); } Queue.Push(UserData); } else { Queue.Push(UserData); } //获取时间 DateTime dt = System.DateTime.Now; string date = dt.ToString("HH:mm:ss fff"); Console.WriteLine(date); //string leng = Character.BytesToString(sendByte, 0, sendByte.Length - 1, true); //写入文件 if (this.TxFileCheckBox.Checked) { TxDataFile.SetValue("send", "Number", sendNum.ToString()); TxDataFile.SetValue("send", "data" + sendNum.ToString(), Character.BytesToString(UserData, 0, UserData.Length - 1, true) + " + " + date); sendNum++; } } else if (this.sendButton.Text == "停止发送") { this.SendTimer.Stop(); this.sendButton.Text = "发送"; } } catch (Exception ex) { MessageBox.Show(" send error" + ex.ToString()); } }