public void Connect(String IP, Int32 port) { if (Globals.IPAddr != null) { try { client = new TcpClient { SendTimeout = 1000 }; client.Connect(IP, port); stream = client.GetStream(); } catch (SocketException ex) { LogBOX.AppendText(ex.Message); LogBOX.AppendText(Environment.NewLine); } finally { SafeInvoke(Eng_L, () => { Eng_L.Value = 0; }); SafeInvoke(Eng_R, () => { Eng_R.Value = 0; }); } } else { LogBOX.AppendText("Enter IP Address first."); LogBOX.AppendText(Environment.NewLine); } }
// Wysylanie i odbieranie ramek private string SendReceive(string msg) { try { byte[] msg_s = Encoding.ASCII.GetBytes(msg); stream.Write(msg_s, 0, msg_s.Length); String response = String.Empty; Byte[] msg_r = new Byte[28]; Int32 bytes = stream.Read(msg_r, 0, msg_r.Length); response = Encoding.ASCII.GetString(msg_r, 0, msg_r.Length); return(response); } catch (System.IO.IOException) {; SafeInvoke(LogBOX, () => { LogBOX.AppendText("Connection interrupted."); LogBOX.AppendText(Environment.NewLine); }); client.Close(); stream.Close(); client.Dispose(); stream.Dispose(); Timer1.Stop(); return(String.Empty); } catch (ObjectDisposedException) { SafeInvoke(LogBOX, () => { LogBOX.AppendText("Connection interrupted."); LogBOX.AppendText(Environment.NewLine); }); client.Close(); stream.Close(); client.Dispose(); stream.Dispose(); Timer1.Stop(); return(String.Empty); } }
private void Disconnect() { if (IsConnected() == true) { Timer1.Stop(); client.Close(); stream.Close(); client.Dispose(); stream.Dispose(); } else { LogBOX.AppendText("Not connected."); LogBOX.AppendText(Environment.NewLine); } }
private void BTNConnect_Click(object sender, EventArgs e) { Globals.IPAddr = IPBox.Text; if (IsConnected() == true) { LogBOX.AppendText("Disconnect before trying to connect again."); LogBOX.AppendText(Environment.NewLine); } else { if (IPBox.Text != "") { Connect(Globals.IPAddr, Globals.port); IsConnected(); } else { LogBOX.AppendText("Error. Make sure you entered proper IP address and host is reachable."); LogBOX.AppendText(Environment.NewLine); } } }
private void UpdateUI() { SafeInvoke(LogBOX, () => { LogBOX.AppendText(Globals.ResponseString); LogBOX.AppendText(Environment.NewLine); }); if (Globals.msg_buffer_r.Length == 28 && Globals.msg_buffer_r.Substring(0, 3) == "[00" & Globals.msg_buffer_r.Substring(27, 1) == "]") { try { SafeInvoke(RStatus, () => { RStatus.Text = "Status: " + Globals.msg_buffer_r.Substring(1, 2); }); SafeInvoke(BatteryLevel, () => { BatteryLevel.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(3, 4), 16); }); SafeInvoke(Sensor1, () => { if (Convert.ToUInt16(Globals.msg_buffer_r.Substring(7, 4), 16) >= 53255) { Sensor1.Value = Sensor1.Maximum; } else { Sensor1.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(7, 4), 16); }; }); SafeInvoke(Sensor2, () => { if (Convert.ToUInt16(Globals.msg_buffer_r.Substring(11, 4), 16) >= 53255) { Sensor2.Value = Sensor2.Maximum; } else { Sensor2.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(11, 4), 16); }; }); SafeInvoke(Sensor3, () => { if (Convert.ToUInt16(Globals.msg_buffer_r.Substring(15, 4), 16) >= 53255) { Sensor3.Value = Sensor3.Maximum; } else { Sensor3.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(15, 4), 16); }; }); SafeInvoke(Sensor4, () => { if (Convert.ToUInt16(Globals.msg_buffer_r.Substring(19, 4), 16) >= 53255) { Sensor4.Value = Sensor4.Maximum; } else { Sensor4.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(19, 4), 16); }; }); SafeInvoke(Sensor5, () => { if (Convert.ToUInt16(Globals.msg_buffer_r.Substring(23, 4), 16) >= 53255) { Sensor5.Value = Sensor5.Maximum; } else { Sensor5.Value = Convert.ToUInt16(Globals.msg_buffer_r.Substring(23, 4), 16); }; }); } catch (FormatException) { SafeInvoke(LogBOX, () => { LogBOX.AppendText("Invalid response, can't update UI controls."); LogBOX.AppendText(Environment.NewLine); }); } catch (ArgumentOutOfRangeException) { //SafeInvoke(Sensor1, () => { Sensor1.Value = 20000; }); //SafeInvoke(Sensor2, () => { Sensor2.Value = 20000; }); //SafeInvoke(Sensor3, () => { Sensor3.Value = 20000; }); //SafeInvoke(Sensor4, () => { Sensor4.Value = 20000; }); //SafeInvoke(Sensor5, () => { Sensor5.Value = 20000; }); } } else { SafeInvoke(LogBOX, () => { LogBOX.AppendText("Invalid response, can't update UI controls."); LogBOX.AppendText(Environment.NewLine); }); } }