/// <summary> /// Start a serial connection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_connect_serial_Click(object sender, EventArgs e) { //If communication port is not initialized yet, initialize if (!CommHandler.initialized) { CommHandler.InitSerial(cmb_comport.SelectedItem.ToString(), int.Parse(cmb_baudrate.SelectedItem.ToString())); } // If port is open, close it. Else, open the port. if (CommHandler.IsOpen()) { if (CommHandler.Close()) { btn_comm_serialSend.Enabled = false; btn_comm_CANconnect.Enabled = true; } } else { if (CommHandler.Open()) { btn_comm_serialSend.Enabled = true; btn_comm_CANconnect.Enabled = false; } } }
//tick sends heartbeat to monitor bus connection private void tim_heartBeat_Tick(object sender, EventArgs e) { pulse = false; heartBeatTimer.Start(); heartBeat_prev = heartBeat; sendHeartbeat = true; try { if (CommHandler.connectionType == CommHandler.ConnectionType.CANbus) //TEMP disable heartbet for Ægir { return; } KeyValuePair <int, int> kvp; if (st.status[0] == 0) { kvp = new KeyValuePair <int, int>(0, 1); } else { kvp = new KeyValuePair <int, int>(0, 0); } CommHandler.Send(kvp); } catch { Console.Write("No connection for heartbeat"); } }
private void btn_send_serial_Click(object sender, EventArgs e) { int index = (int)nud_con_serial_index.Value; int value = (int)nud_con_serial_value.Value; CommHandler.Send(new KeyValuePair <int, int>(index, value)); }
public Client(Server server, RealmManager manager, SocketAsyncEventArgs send, SocketAsyncEventArgs receive, byte[] clientKey) { _server = server; Manager = manager; _clientKey = clientKey; ReceiveKey = new RC4(_clientKey); SendKey = new RC4(ServerKey); _handler = new CommHandler(this, send, receive); }
/// <summary> /// Update format of message sent and message recieved if radiobuttons changes /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void rdb_comm_status_CheckedChanged(object sender, EventArgs e) { if (rdb_comm_status_dec.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToString(CommHandler.messageRecieved); } else if (rdb_comm_status_hex.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToHEXString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToHEXString(CommHandler.messageRecieved); } else if (rdb_comm_status_bytes.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToByteString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToByteString(CommHandler.messageRecieved); } }
/// <summary> /// Start a CAN buss connection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_comm_CANconnect_Click(object sender, EventArgs e) { //If communication port is not initialized yet, initialize if (!CommHandler.initialized) { string ip; int port; try { ip = IPAddress.Parse(txt_comm_CANip.Text).ToString(); port = Int32.Parse(txt_comm_CANport.Text); } catch (Exception CANConnectExeption) { MessageBox.Show("Please enter a valid IP address and port number", "caption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } CommHandler.InitCAN(ip, port); } // If port is open, close it. Else, open the port. if (CommHandler.IsOpen()) { if (CommHandler.Close()) { btn_comm_CANsend.Enabled = false; btn_comm_serialConnect.Enabled = true; } } else { if (CommHandler.Open()) { btn_comm_CANsend.Enabled = true; btn_comm_serialConnect.Enabled = false; } } //bgw_aegirMessageRequest.RunWorkerAsync(); }
public static bool Reload() { bool succ = true; dataHolder = new DataHolder(); reader.Restart(); try { _Load(); // Reset all the items to be reloaded, (not failed to load) if (dataHolder.cur_graphicSetting != null) { Program.windowStatus.graphicsCreator.Prototype.Dispose(); } if (dataHolder.indexSettings != null) { Program.windowStatus.indexSettings.Dispose(); } if (dataHolder.indexStats != null) { Program.windowStatus.indexStats.Dispose(); } _InsertDataToProgram(); // Fix up some stuff, loose ends Program.windowStatus.indexStats.CheckAllLinks(); Program.windowStatus.graphicsCreator.Prototype.UpdateIdxSettingReference(); } catch { succ = false; } Program.windowStatus.txtbox_dllimported.Text = CommHandler.dllpath; CommHandler.InitDllImport(); dataHolder.Clear(); dataHolder = null; return(succ); }
/// <summary> /// Request a sertain value from the remote vehicle /// </summary> /// <param name="index"></param> public void Request(int index) { if (!IsOpen()) { return; } byte readIdentifyer = 0x01; byte startAddressHi = (byte)(index >> 8); byte startAddressLo = (byte)(index); byte numBytesHi = 0; byte numBytesLo = 8; byte[] packet = { readIdentifyer, startAddressHi, startAddressLo, numBytesHi, numBytesLo }; // Send the message to the connected TcpServer. stream.Write(packet, 0, packet.Length); //TEMP read response // Buffer to store the response bytes. byte[] response = new byte[9]; //TEMP Read the first batch of the TcpServer response bytes. try { int numRecieved = stream.Read(response, 0, response.Length); } catch (Exception e) { Console.WriteLine(e.ToString()); } KeyValuePair <int, int>[] status; status = CommHandler.AegirConvertData(index, response); foreach (KeyValuePair <int, int> s in status) { ST_Register.status[s.Key] = s.Value; } }
public static void Load() { dataHolder = new DataHolder(); // Set up failproof values if the load fails Program.windowStatus.graphicsCreator.SetPrototype(new GraphicsCreator.graphicPrototype("", new GraphicsCreator.graphicPrototype.prototypeIndex[0])); try { _Load(); _InsertDataToProgram(); } catch (Exception e) { Console.WriteLine(e); Program.errors.Add("Failed to load settings."); } Program.windowStatus.txtbox_dllimported.Text = CommHandler.dllpath; CommHandler.InitDllImport(); dataHolder.Clear(); dataHolder = null; }
private static void _bgrSendCommands(object sender, DoWorkEventArgs e) { // Ignore this call if the port has not been assigned yet if (CommHandler.connection == null) { commands.ResetArray(); Program.windowStatus.tim_SendCommandsDelay.Interval = (int)Program.windowStatus.nud_comm_transfreq.Value; Program.windowStatus.tim_SendCommandsDelay.Start(); return; } ST_Array.arrelement[] data = commands.GetAllValuesAndReset(); // Return if there are no elements to send if (data.Length == 0) { Program.windowStatus.tim_SendCommandsDelay.Interval = (int)Program.windowStatus.nud_comm_transfreq.Value; Program.windowStatus.tim_SendCommandsDelay.Start(); return; } // Stop the timer for next update, to start again when finished with sending these commands stopwatch.Restart(); // Send commands KeyValuePair <int, int>[] tosend = new KeyValuePair <int, int> [data.Length]; for (int i = 0, j = data.Length; i < j; i++) { tosend[i] = new KeyValuePair <int, int>(data[i].index, data[i].value); } CommHandler.Send(tosend); // Set the delay to the next send of commands, based on time elapsed sending current commands and time till next frame stopwatch.Stop(); int tick = (int)Program.windowStatus.nud_comm_transfreq.Value - (int)stopwatch.ElapsedMilliseconds; tick = tick < 1 ? 1 : tick; Program.windowStatus.tim_SendCommandsDelay.Interval = tick; Program.windowStatus.tim_SendCommandsDelay.Start(); }
private void tim_100ms_update_Tick(object sender, EventArgs e) { //serial connection if (CommHandler.initialized) { //the button to update Button btn; switch (CommHandler.connectionType) { case CommHandler.ConnectionType.Serial: btn = btn_comm_serialConnect; break; case CommHandler.ConnectionType.Ethernet: btn = btn_comm_ethernetConnect; break; case CommHandler.ConnectionType.CANbus: btn = btn_comm_CANconnect; break; default: btn = null; break; } //update connection button color if (CommHandler.GetConnectionStatus() == CommHandler.ConnectionStatus.Connected) { btn.BackColor = System.Drawing.Color.SpringGreen; //Darg green?, ForrestGreen? LimeGreen? SpringGreen? //btn_connect_serial.ForeColor = System.Drawing.Color.White; } else if (CommHandler.GetConnectionStatus() == CommHandler.ConnectionStatus.NotConnected) { btn.UseVisualStyleBackColor = true; btn.ForeColor = System.Drawing.SystemColors.MenuHighlight; } else if (CommHandler.GetConnectionStatus() == CommHandler.ConnectionStatus.Disconnected) { btn.BackColor = System.Drawing.Color.Red; //Firebrick? btn.ForeColor = System.Drawing.Color.White; } if (CommHandler.newMessage) { if (rdb_comm_status_dec.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToString(CommHandler.messageRecieved); } else if (rdb_comm_status_hex.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToHEXString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToHEXString(CommHandler.messageRecieved); } else if (rdb_comm_status_bytes.Checked) { txt_con_messageSendt.Text = CommHandler.PacketToByteString(CommHandler.messageSendt); txt_con_messageRecieved.Text = CommHandler.PacketToByteString(CommHandler.messageRecieved); } CommHandler.newMessage = false; } //TEMP reset zero depth click //ST_Register.commands[801] = 0; } //temp /* * byte[] b = new byte[9] { 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; * KeyValuePair<int, int>[] a; * a = CommHandler.AegirConvertData(13, b); * ST_Register.status[4] = a[0].Value; */ if (sendHeartbeat) { if (st.status[0] == 0) { //try { CommHandler.Send(0, 1); } catch { Program.errors.Add("No serial connection for heartbeat"); } } else if (st.status[0] > 0) { //try { CommHandler.Send(0, 0); } catch { Program.errors.Add("No serial connection for heartbeat"); } } sendHeartbeat = false; } //display error messages if they are updated if (Program.errors.HaveUpdated()) { txt_error.Text = Program.errors.ToString(); } }
private void btn_loaddll_Click(object sender, EventArgs e) { CommHandler.dllpath = txtbox_dllimported.Text; CommHandler.InitDllImport(); }