// Invoke the Changed event; called whenever list New Message should be send protected virtual void msgSendRecived(MsgSendRecivedEventArgs e) { if (MsgSendRecived != null) { MsgSendRecived(this, e); } }
/// <summary> /// Redirect the Log Messages Fromm the Connections /// </summary> /// <param name="logMessage"></param> public void SendRecivedEventHandler(object sender, MsgSendRecivedEventArgs e) { ConnectionInterface connection = sender as ConnectionInterface; if (e.msgData.type == MsgData.messageType.recived) { // Create a new Send Object! MsgData sendData = new MsgData(e.msgData.value, MsgData.messageType.redirect); if (connection.interfaceNumber == 1) { connection2.Send(sendData); } else { connection1.Send(sendData); } // Update the MsgLog bevore we send the Data to the Corresponding Data msgSendRecived(e); } else if (e.msgData.type == MsgData.messageType.send) { // Update the Msg Log with the injected Message by the User msgSendRecived(e); } else if (e.msgData.type == MsgData.messageType.infoPositive || e.msgData.type == MsgData.messageType.infoNegative) { msgSendRecived(e); } }
/// <summary> /// Send Data over Serial Interface /// </summary> /// <param name="data"></param> public void send(MsgData message) { if (pcSerialPort.IsOpen) { try { // Send the Data over Serial and to the UI pcSerialPort.Write(message.value, 0, message.value.Length);// Send the Data after replacing the Human Readable ASCII Chars message.setCurrentTimeStamp(); // Set the Time Stamp message.connectionNumber = interfaceNumber; // Set the Interface Reference // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = message; msgSendRecived(msgLogEventArgs); } catch (Exception ex) { MsgData logMessage = new MsgData(); // Set an error Message to the UI logMessage.value = Encoding.ASCII.GetBytes("Konnte nicht gesendet werden: " + message.value + "\n" + ex); logMessage.type = MsgData.messageType.infoNegative; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = message; msgSendRecived(msgLogEventArgs); } } }
/// <summary> /// Connect the Serial Port /// </summary> public void connect() { MsgData logMessage = new MsgData(); // Check if we have to close the Serial Port! if (pcSerialPort != null) { if (pcSerialPort.IsOpen) { pcSerialPort.Close(); } } else { pcSerialPort = new SerialPort(); } // Open the Serial Port try { // Set the Serial Paramter pcSerialPort.PortName = pcSerialParam.port; pcSerialPort.BaudRate = pcSerialParam.baud; pcSerialPort.DataBits = pcSerialParam.dataBits; pcSerialPort.Parity = pcSerialParam.parity; pcSerialPort.StopBits = pcSerialParam.stopBits; // Add a new ComPort on Recive Handler pcSerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); pcSerialPort.Open(); // Set the User Hint logMessage.value = Encoding.ASCII.GetBytes("Verbindung: " + pcSerialPort.PortName + " Erfolgreich aufgebaut"); logMessage.type = MsgData.messageType.infoPositive; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } catch (Exception) { // Set a User Hint logMessage.value = Encoding.ASCII.GetBytes("Verbindung: " + pcSerialPort.PortName + " Konnte nicht aufgebaut werden"); logMessage.type = MsgData.messageType.infoNegative; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } }
/// <summary> /// Send the Data to the Eventhandler.. To update the Main UI and send the Data to teh Connection /// </summary> private void SendData(string data) { // Set the User Hint to the TextBox MsgData _logMessage = new MsgData(); _logMessage.value = Converty.specialAsciiStringToMsgData(data); _logMessage.type = MsgData.messageType.send; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs _msgLogEventArgs = new MsgSendRecivedEventArgs(); _msgLogEventArgs.msgData = _logMessage; msgSendRecived(_msgLogEventArgs); }
/// <summary> /// Update the ui With an Log Message and trigger the UpdateStatusBar /// </summary> /// <param name="rtbMsg">Message for the Log Window</param> /// <param name="msgType">Msg Type</param> private void updateUi(string rtbMsg, MsgData.messageType msgType) { // Set the User Hint to the TextBox MsgData logMessage = new MsgData(); logMessage.value = Encoding.ASCII.GetBytes(rtbMsg); logMessage.type = msgType; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); }
/// <summary> /// Click of the Quick Button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void QuickButton_Click(object sender, RoutedEventArgs e) { Button _quickButton = sender as Button; // Set the User Hint to the TextBox MsgData logMessage = new MsgData(); logMessage.value = Converty.specialAsciiStringToMsgData((String)_quickButton.Resources["data"]); logMessage.type = MsgData.messageType.send; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); }
/// <summary> /// Send Data to the Client or Server /// </summary> /// <param name="message"></param> public void send(MsgData message) { if (tcpClient.Connected) { NetworkStream clientStream = tcpClient.GetStream(); clientStream.Write(message.value, 0, message.value.Length); clientStream.Flush(); message.setCurrentTimeStamp(); // Set the Time Stamp message.connectionNumber = interfaceNumber; // Set the reference to the Interface // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = message; msgSendRecived(msgLogEventArgs); } }
/// <summary> /// /// </summary> /// <param name="inputKey"></param> public void KeyInpuEvent(Key inputKey) { foreach (Button _button in buttonList) { if ((Key)_button.Resources["shortCut"] == inputKey) { // Set the User Hint to the TextBox MsgData logMessage = new MsgData(); logMessage.value = Converty.specialAsciiStringToMsgData((String)_button.Resources["data"]); logMessage.type = MsgData.messageType.send; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } } }
/// <summary> /// Disconnect the Serial Port /// </summary> public void disconnect() { // Check if we have to close the Serial Port! if (pcSerialPort != null) { if (pcSerialPort.IsOpen) { pcSerialPort.Close(); // Set the User hint MsgData logMessage = new MsgData(); logMessage.value = Encoding.ASCII.GetBytes("Verbindung: " + pcSerialPort.PortName + " Erfolgreich abgebaut"); logMessage.type = MsgData.messageType.infoNegative; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } } }
/// <summary> /// Update the Rich Text Box from an other Thread /// </summary> /// <param name="message"></param> /// <param name="messageColor"></param> public void MsgSendRecived_Event(object sender, MsgSendRecivedEventArgs e) { this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Threading.DispatcherOperationCallback(delegate { // Store the Data to the msg Log Handler msgLog.Add(e.msgData); // Update the RichtextBox UpdateRTB(e.msgData); if (simUi.Count > 0) { foreach (Simulation_UI ui in simUi) { ui.InputUpdate(e.msgData); } } return(null); }), null); }
/// <summary> /// Data Recive Handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { MsgData logMessage = new MsgData(); byte[] message = new byte[4096]; int bytesRead; try { // Read the Data bytesRead = pcSerialPort.Read(message, 0, 4096); // Set the Current TimeStamp logMessage.setCurrentTimeStamp(); // Save the Data to the logMessage.value = new byte[bytesRead]; // Create an Array with the Size of readed Data Array.Copy(message, logMessage.value, bytesRead); // Copy the Data to the Array logMessage.type = MsgData.messageType.recived; //Set the Type to Recived Message logMessage.connectionNumber = interfaceNumber; // Set the Interface Reference // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } catch (Exception ex) { // Set an error Message to the UI logMessage.value = Encoding.ASCII.GetBytes("Konnte nicht empfangen werden: " + "\n" + ex); logMessage.type = MsgData.messageType.infoNegative; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } }
/// <summary> /// Redirect the Log Messages Fromm the Connections /// </summary> /// <param name="logMessage"></param> public void SendRecivedEventHandler(object sender, MsgSendRecivedEventArgs e) { // Set the Event that the User Changed an Input sendRecivedEvent(e);//EventArgs.Empty }
/// <summary> /// msgSend Event from the Simulation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void simulatioSendMsg(object sender, MsgSendRecivedEventArgs e) { ConnectionInterface_SendMsgData(e.msgData); }
/// <summary> /// Handling the Client Communication /// </summary> /// <param name="client"></param> private void HandleClientComm(object client) { MsgData logMessage; tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; bool stopClientComm = false; while (!stopThreads && !stopClientComm) { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { //a socket error has occured if (!settings.restartTcpServer) { stopClientComm = true; stopThreads = true; } else { stopClientComm = true; } break; } if (bytesRead == 0) { //the client has disconnected from the server if (!settings.restartTcpServer) { stopClientComm = true; stopThreads = true; } else { stopClientComm = true; } break; } // Save the Data to the logMessage = new MsgData(); // Set the Current TimeStamp logMessage.setCurrentTimeStamp(); logMessage.value = new byte[bytesRead]; // Create an Array with the Size of readed Data Array.Copy(message, logMessage.value, bytesRead); // Copy the Data to the Array logMessage.type = 0; //Set the Type to Recived Message logMessage.connectionNumber = interfaceNumber; // Set the Event that the User Changed an Input MsgSendRecivedEventArgs msgLogEventArgs = new MsgSendRecivedEventArgs(); msgLogEventArgs.msgData = logMessage; msgSendRecived(msgLogEventArgs); } // Update the UI updateUi("Client disconnected with IP:" + settings.ip + ":" + settings.port.ToString(), MsgData.messageType.infoNegative); tcpClient.Close(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void msgSend(object sender, MsgSendRecivedEventArgs e) { msgSendRecived(e); }