/// <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> /// This function allows to convert the User Inut String before storing the data in the ringbuffer /// </summary> /// <param name="stringMessage"></param> public MsgData getRawMsgWithCurrentViewSettings(string stringMessage) { // Define the needed Variables MsgData message = new MsgData(); message.setCurrentTimeStamp(); // Variables for the Split string[] splitValues = new string[] { ",", " " }; string[] splitedMsg = stringMessage.Split(splitValues, 1024, StringSplitOptions.RemoveEmptyEntries); byte[] rawData = new byte[splitedMsg.Length]; // Convert the User Input to the Current Settings // Return the Msg with the Current View Configuration switch (viewSettings.dataPresentation) { default: // ASCII Encoded Strings message.value = Converty.specialAsciiStringToMsgData(stringMessage); break; case 1: // HEX Values of the Bytes recived for (int i = 0; i <= splitedMsg.Length - 1; i++) { rawData[i] = Convert.ToByte(splitedMsg[i], 16); } message.value = rawData; break; case 2: // DEC Values of the Bytes recived for (int i = 0; i <= splitedMsg.Length - 1; i++) { rawData[i] = Convert.ToByte(splitedMsg[i], 10); } message.value = rawData; break; case 3: // BIN Values of the Bytes recived for (int i = 0; i <= splitedMsg.Length - 1; i++) { rawData[i] = Convert.ToByte(splitedMsg[i], 2); } message.value = rawData; break; } return(message); }
/// <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> /// 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> /// 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(); }