private void button1_Click(object sender, EventArgs e) { this.Hide(); Server_Client sc = new Server_Client(); sc.StartPosition = FormStartPosition.CenterScreen; sc.Show(); }
private void ReceiveMessage() { while (true) { byte[] result = new byte[1024 * 1024]; int ReceiveLength = 0; ReceiveLength = ClientSocket.Receive(result); if (result[0] == 0)//表示接收聊天消息 { try { richTextBox3.Text += "接收现场端聊天消息:"; string str = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1); richTextBox3.Text += str; if (checkBox1.Checked) { richTextBox3.Text += DateTime.Now; //显示时间 } if (checkBox3.Checked) { richTextBox1.Text += "\r\n"; //自动换行 } } catch (Exception ex) { MessageBox.Show("接收消息失败!" + ex.ToString()); ClientSocket.Shutdown(SocketShutdown.Both); ClientSocket.Close(); break; } } if (result[0] == 6)//调试成功 { MessageBox.Show("设备参数调试成功!"); } if (result[0] == 7) { MessageBox.Show("请继续调试!!!"); } if (result[0] == 1)//表示接收设备状态信息 { try { //richTextBox1.Text += "接收现场设备数据信息:"; string str = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1); for (int i = 0; i < str.Length; i++)//解密处理 { int num = str[i] - '0'; if (i != 0 || i != str.Length) { num -= 1; } Information += num.ToString(); } Server_Client sc = new Server_Client(); sc.Information = Information; //if (checkBox1.Checked) textBox5.Text += DateTime.Now;//显示时间 textBox5.Text += DateTime.Now + ":" + Information + "\r\n"; //if (checkBox3.Checked) textBox5.Text += "\r\n";//自动换行 } catch (Exception ex) { MessageBox.Show("现场数据接收异常!" + ex.ToString()); ClientSocket.Close(); break; } } } }
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) //串口接收 { if (radioButton1.Checked) //接受字符模式ASCII { string str = serialPort1.ReadExisting(); //字符串方式读 int num = str[0] - '0'; if (num == 6) { MessageBox.Show("设备调试成功!"); return; } if (num == 7) { MessageBox.Show("请继续调试!!!"); return; } richTextBox2.Text += "ASCII接收:"; if (checkBox1.Checked) { richTextBox2.Text += DateTime.Now + ":"; //显示时间 } richTextBox2.Text += str; if (checkBox3.Checked) { richTextBox2.Text += "\r\n"; } Information = str; Server_Client sc = new Server_Client(); sc.Information = Information; } if (radioButton2.Checked)//HEX接收,只接收两位HEX,接收后转为8位二进制方式 { byte data; richTextBox2.Text += "HEX接收:"; data = (byte)serialPort1.ReadByte();//int转换为byte string str = Convert.ToString(data, 16).ToUpper(); if (checkBox1.Checked) { richTextBox2.Text += DateTime.Now + ":"; //显示时间 } richTextBox2.Text += "0x" + (str.Length == 1 ? "0" + str : str + " "); if (checkBox3.Checked) { richTextBox2.Text += "\r\n"; } //16进制转8位2进制,不够补0 string str2 = null; int[] a = new int[10]; for (int i = 0; i <= str.Length - 1; i++) { int cnt = 0; int num = str[i] - '0'; if (num >= 10) { num -= 7; } while (num != 0) { a[++cnt] = num % 2; num /= 2; } for (int j = 1; j <= (4 - cnt); j++) { str2 += "0"; } for (int j = cnt; j >= 1; j--) { str2 += a[j].ToString(); } } Information = str2; Server_Client sc = new Server_Client(); sc.Information = Information; } }