private void recvDataDecode(byte[] data) { try { TransmitFormat_t recvData = (TransmitFormat_t)BytesToStruct(data, typeof(TransmitFormat_t)); //if (recvData.head != TRANSMIT_HEAD) return; switch (recvData.code) { case CONNECT_RESOPNCE_CODE: { msgRetransmit = null; heartBeatCnt = 0; printMessage("连接成功!\r\n", "green"); button1.Text = "断开连接"; label4.Text = "已连接"; heartBeat.Start(); Constate = CONNECTED; break; } case READ_CONFIG_RESPONCE_CODE: { msgRetransmit = null; heartBeatCnt = 0; ConfigFormat_t configData = (ConfigFormat_t)BytesToStruct(recvData.data, typeof(ConfigFormat_t)); comboBox1.Text = configData.channel.ToString(); comboBox3.Text = configData.power.ToString(); comboBox2.Text = configData.speed.ToString(); printMessage("配置读取成功!\r\n", "green"); break; } case WRITE_CONFIG_RESPONCE_CODE: { msgRetransmit = null; heartBeatCnt = 0; printMessage("配置写入成功!\r\n", "green"); break; } case HEART_BEAT_RESPONCE: heartBeatCnt = 0; break; case TRANS_DATA_CODE: { heartBeatCnt = 0; break; } default: //printMessage(System.Text.Encoding.Default.GetString(data) + "\r\n", "olive"); printMessage(BitConverter.ToString(data, 0).Replace(" ", string.Empty).ToLower() + "\r\n", "olive"); break; } } catch { printMessage(BitConverter.ToString(data, 0).Replace(" ", string.Empty).ToLower() + "\r\n", "olive"); } }
private void rebootDevice() { TransmitFormat_t connectdata = new TransmitFormat_t(); connectdata.head = TRANSMIT_HEAD; connectdata.code = REBOOT_CODE; connectdata.datalen = 0; transmitSendData(StructToBytes(connectdata), Marshal.SizeOf(connectdata)); msgRetransmit = new delegateCall(rebootDevice); retransmit.Start(); }
private void readConfigRequest() { TransmitFormat_t connectdata = new TransmitFormat_t(); connectdata.head = TRANSMIT_HEAD; connectdata.code = READ_CONFIG_REQUEST_CODE; connectdata.datalen = 0; transmitSendData(StructToBytes(connectdata), Marshal.SizeOf(connectdata)); msgRetransmit = new delegateCall(readConfigRequest); retransmit.Start(); }
private void writeConfigRequest( ) { try { TransmitFormat_t connectdata = new TransmitFormat_t(); ConfigFormat_t configData = new ConfigFormat_t(); configData.channel = int.Parse(comboBox1.Text); configData.power = int.Parse(comboBox3.Text); configData.speed = int.Parse(comboBox2.Text); connectdata.head = TRANSMIT_HEAD; connectdata.code = WRITE_CONFIG_REQUEST_CODE; connectdata.datalen = Marshal.SizeOf(configData); connectdata.data = StructToBytes(configData); transmitSendData(StructToBytes(connectdata), Marshal.SizeOf(connectdata)); msgRetransmit = new delegateCall(writeConfigRequest); retransmit.Start(); } catch { MessageBox.Show("配置填写错误,请检查!"); } }
private void retransmitTime(object source, System.Timers.ElapsedEventArgs e) { if (msgRetransmit != null) { msgRetransmit(); } else { retransmit.Stop(); retranCount = 0; } if (msgRetransmit_old == msgRetransmit && ++retranCount > 3) { msgRetransmit_old = null; msgRetransmit = null; transmitFaildCallback(); printMessage("操作失败!\r\n", "red"); } else if (msgRetransmit_old != msgRetransmit) { msgRetransmit_old = msgRetransmit; retranCount = 0; } }
private void button1_Click(object sender, EventArgs e) { if (Constate == DISCONNECT) { if (ConMode == CONNECT_MODE_COM) { comboBox4.Enabled = false; comboBox5.Enabled = false; comboBox6.Enabled = false; comboBox7.Enabled = false; comboBox8.Enabled = false; printMessage("connect device by serial.\r\n", "blue"); ComDevice = new SerialPort(); ComDevice.PortName = comboBox6.Text; ComDevice.BaudRate = Convert.ToInt32(comboBox5.Text); ComDevice.DataBits = Convert.ToInt32(comboBox4.Text); ComDevice.StopBits = (StopBits)Convert.ToInt32(comboBox7.Text); ComDevice.DataReceived += new SerialDataReceivedEventHandler(serialReceivePro); switch (comboBox8.Text) { case "无": ComDevice.Parity = Parity.None; break; case "奇校验": ComDevice.Parity = Parity.Odd; break; case "偶校验": ComDevice.Parity = Parity.Even; break; default: ComDevice.Parity = Parity.None; break; } try { ComDevice.Open(); } catch { comboBox4.Enabled = true; comboBox5.Enabled = true; comboBox6.Enabled = true; comboBox7.Enabled = true; comboBox8.Enabled = true; printMessage("打开串口失败!\r\n", "red"); MessageBox.Show("打开串口失败!"); ComDevice.Close(); return; } } else { printMessage("connect device by ip net.\r\n", "blue"); try { textBox1.ReadOnly = true; textBox2.ReadOnly = true; textBox3.ReadOnly = true; textBox4.ReadOnly = true; textBox26.ReadOnly = true; string dstIP = String.Format("{0}.{1}.{2}.{3}", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text); int remotePort = int.Parse(textBox26.Text); IPAddress remoteIP = IPAddress.Parse(dstIP); remotePoint = new IPEndPoint(remoteIP, remotePort); udp_client = new UdpClient(); udpRcvThread = new Thread(udpReceivePro); udpRcvThread.Start(); } catch { textBox1.ReadOnly = false; textBox2.ReadOnly = false; textBox3.ReadOnly = false; textBox4.ReadOnly = false; textBox26.ReadOnly = false; textBox1.Focus(); printMessage("地址无效,请输入正确的IP地址!\r\n", "red"); MessageBox.Show("地址无效,请输入正确的IP地址!"); return; } } radioButton1.Enabled = false; radioButton2.Enabled = false; radioButton3.Enabled = false; radioButton4.Enabled = false; Constate = CONNECTING; label4.Text = "正在连接"; button1.Text = "取消"; printMessage("连接中,请稍等。。。\r\n", "green"); if (typedev == DEVICE_BASE) { Constate = CONNECTED; printMessage("连接成功!\r\n", "green"); button1.Text = "断开连接"; label4.Text = "已连接"; } else { connectDevice(); } } else { button1.Text = "连接"; label4.Text = "未连接"; msgRetransmit = null; if (Constate == CONNECTED && typedev != DEVICE_BASE) { disconnect(); } if (ConMode == CONNECT_MODE_COM) { comboBox4.Enabled = true; comboBox5.Enabled = true; comboBox6.Enabled = true; comboBox7.Enabled = true; comboBox8.Enabled = true; ComDevice.Close(); } else { textBox1.ReadOnly = false; textBox2.ReadOnly = false; textBox3.ReadOnly = false; textBox4.ReadOnly = false; textBox26.ReadOnly = false; udpRcvThread.Abort(); } printMessage("连接关闭.\r\n", "green"); Constate = DISCONNECT; cntTimeCount = 0; radioButton1.Enabled = true; radioButton2.Enabled = true; radioButton3.Enabled = true; radioButton4.Enabled = true; } }