private void AppendBytesToTerminal(byte[] bytes) { if (bytes.Length == 0) { return; } bytesInCounter.Add((uint)bytes.Length); BytesCounter.MeasureUnit mUnit = bytesInCounter.RecomendedMeasureUnit(); var format = "{0:0}"; if (mUnit != BytesCounter.MeasureUnit.Bytes) { format = "{0:0.00}"; } var processedCounter = String.Format(format, bytesInCounter.GetProcessedCounter(mUnit)); Invoke((MethodInvoker) delegate { try { bytesInLbl.BackColor = Color.LimeGreen; bytesInTimer.Stop(); bytesInTimer.Start(); bytesInTimer.Enabled = true; bytesInLbl.Text = processedCounter + " " + BytesCounter.MeasureUnitToString(mUnit); // handle request for detailed text (timestamp) string displayStr = ""; if (detailedChkBx.Checked) { displayStr = TimestampString(); } if (pkgParseChkBx.Checked) { string parsedPackage = ParseIncomingPackage(bytes); displayStr += Environment.NewLine + parsedPackage; // if log file is started, log formatted bytes if (fileLog != null) { fileLog.Write(parsedPackage); } } else { // handle format request string formattedBytes = ""; switch (txtFormat) { case TextFormatType.ASCII: formattedBytes = BytesToAsciiString(bytes); break; case TextFormatType.BINARY: formattedBytes = BytesToString(bytes, "{", "}", "|") + Environment.NewLine; break; case TextFormatType.HEX: formattedBytes = BytesToHexString(bytes) + EndOfLineString(); break; case TextFormatType.INVALID: formattedBytes = "INTERNAL ERROR: INVALID FORMAT TYPE" + Environment.NewLine; break; } displayStr += formattedBytes; // if log file is started, log formatted bytes if (fileLog != null) { if (detailedChkBx.Checked) { fileLog.WriteWithTimestamp(formattedBytes); } else { fileLog.Write(formattedBytes); } } } if (autoScrollChkBx.Checked) { displayTxt.AppendText(displayStr); } else { displayTxt.Text += displayStr; } /* if new line arrived, pass it to graph form */ if (graphFrom_ != null && displayTxt.Lines.Any() && displayTxt.Lines.Length >= 2 && displayTxt.Lines.Length != prevLinesCount) { prevLinesCount = displayTxt.Lines.Length; string newLine = displayTxt.Lines[displayTxt.Lines.Length - 2]; graphFrom_.OnIncomingData(newLine); } } catch (ObjectDisposedException exp) { return; } }); }
private void OnIncomingBytes(byte[] bytes) { bytesInCounter.Add((uint)bytes.Length); }
/// <summary> /// Send message bytes to one of the open connections, and /// present bytes statistics /// </summary> /// <param name="msg">bytes array</param> private void SendMsg(byte [] msg) { bool send_success = false; if (ConnectionsManager.Inst.IsTcpServerInitiated()) { send_success = ConnectionsManager.Inst.TCPServer.Send(msg); if (!send_success) { Invoke((MethodInvoker) delegate { tcpDisconnectBtn.PerformClick(); }); } } else if (ConnectionsManager.Inst.IsTcpClientInitiated()) { send_success = ConnectionsManager.Inst.TCPClient.Send(msg); if (!send_success) { Invoke((MethodInvoker) delegate { tcpDisconnectBtn.PerformClick(); }); } } else if (ConnectionsManager.Inst.IsUdpServerInitiated()) { send_success = ConnectionsManager.Inst.UDPServer.Send(msg); if (!send_success) { Invoke((MethodInvoker) delegate { udpDisconnectBtn.PerformClick(); }); } } else if (ConnectionsManager.Inst.IsUdpClientInitiated()) { send_success = ConnectionsManager.Inst.UDPClient.Send(msg); if (!send_success) { Invoke((MethodInvoker) delegate { udpDisconnectBtn.PerformClick(); }); } } else if (ConnectionsManager.Inst.IsSerialInitiated()) { send_success = ConnectionsManager.Inst.Serial.Send(msg); if (!send_success) { Invoke((MethodInvoker) delegate { serialDisconnectBtn.PerformClick(); }); } } if (send_success && ConnectionsManager.Inst.IsSomeConnectionInitiated()) { bytesOutCounter.Add((uint)msg.Length); BytesCounter.MeasureUnit mUnit = bytesOutCounter.RecomendedMeasureUnit(); var format = "{0:0}"; if (mUnit != BytesCounter.MeasureUnit.Bytes) { format = "{0:0.00}"; } var processedCounter = String.Format(format, bytesOutCounter.GetProcessedCounter(mUnit)); Invoke((MethodInvoker) delegate { bytesOutLbl.BackColor = Color.LimeGreen; bytesInTimer.Stop(); bytesInTimer.Start(); bytesOutTimer.Enabled = true; bytesOutLbl.Text = processedCounter + " " + BytesCounter.MeasureUnitToString(mUnit); }); } }