Exemplo n.º 1
0
        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;
                }
            });
        }