Beispiel #1
0
        /// <summary>
        /// Rerverts Console back to original state.
        /// </summary>
        internal void RevertConsole()
        {
            // set page code back to original
            NativeMethods.SetCodePage(codepage);

            // re-enable word-wrap
            NativeMethods.EnableWordWrap(); // Enable Word Wrap

            // Disable Ctrl+C capture, allow it termiante.
            Console.TreatControlCAsInput = false;

            // set console buffer size back to original state.
            base.SetConsoleBufferSize(bufferWidth, bufferHeight);

            // Issue a graceful terminate.
            SharedFunc.SetSerialSession(false);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes class and sets defaults.
        /// </summary>
        internal AnsiEscape(T session)
        {
            // Clear the Console
            Console.Clear();

            serialSession = session;

            // Set console and buffer sizes. Constrain window size to largest allowed
            base.SetConsoleSize(Math.Min(consoleWidth, Console.LargestWindowWidth),
                                Math.Min(consoleHeight, Console.LargestWindowHeight));
            base.SetConsoleBufferSize(consoleBufferWidth, consoleBufferHeight);

            // set cursor positions to beginning
            PositionLeft = 0;
            PositionTop  = 0;

            // set cursor position to default
            base.SetCursorPosition(PositionTop, PositionLeft);

            // capture current code page.
            uint cp = NativeMethods.GetCodePage();

            if (cp > 0)
            {
                codepage = cp;
            }

            // Set the Console Code page to 437
            NativeMethods.SetCodePage(437);

            // pInvoke Call to Disable Console
            // wordwrap.  By default VT100 does not
            // expect wordwrap
            NativeMethods.DisableWordWrap(); // Disable Word Wrap

            // Capture Ctrl+C as key command.
            Console.TreatControlCAsInput = true;

            // enable receive loop.
            SharedFunc.SetSerialSession(true);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes class and sets defaults.
        /// </summary>
        internal AnsiEscape(T session)
        {
            // Clear the Console
            Console.Clear();

            serialSession = session;

            // set console and buffer size
            base.SetConsoleSize(80, 25);
            base.SetConsoleBufferSize(80, 25);

            // set cursor positions to begining
            PositionLeft = 0;
            PositionTop  = 0;

            // set cursor position to default
            base.SetCursorPosition(PositionLeft, PositionTop);

            // capture current code page.
            uint cp = NativeMethods.GetCodePage();

            if (cp > 0)
            {
                codepage = cp;
            }

            // Set the Console Code page to 437
            NativeMethods.SetCodePage(437);

            // pInvoke Call to Disable Console
            // wordwrap.  By default VT100 does not
            // expect wordwarp
            NativeMethods.DisableWordWrap(); // Disable Word Wrap

            // Capture Ctrl+C as key command.
            Console.TreatControlCAsInput = true;

            // enable receive loop.
            SharedFunc.SetSerialSession(true);
        }
Beispiel #4
0
        /// <summary>
        /// Rerverts Console back to original state.
        /// </summary>
        internal void RevertConsole()
        {
            // set page code back to original
            NativeMethods.SetCodePage(codepage);

            // re-enable word-wrap
            NativeMethods.EnableWordWrap(); // Enable Word Wrap

            // Disable Ctrl+C capture, allow it termiante.
            Console.TreatControlCAsInput = false;

            // Set console and buffer sizes back to the original state.
            // Constrain window size to largest allowed.
            base.SetConsoleSize(Math.Min(initWindowWidth, Console.LargestWindowWidth),
                                Math.Min(initWindowHeight, Console.LargestWindowHeight));
            base.SetConsoleBufferSize(initBufferWidth, initBufferHeight);

            // Issue a graceful terminate.
            SharedFunc.SetSerialSession(false);

            //clear console
            Console.Clear();
        }
Beispiel #5
0
        /// <summary>
        /// Encode VT100 escape sequences into function key
        /// enterEncodeCRLF: VT100 encoding for Enter key. True: CR+LF for Enter. False: CR for Enter.
        /// </summary>
        private byte[] Vt100Encode(ConsoleKeyInfo keyInfo, bool enterEncodeCRLF)
        {
            byte[] enc = new byte[3];
            enc[0] = 0x1B; // Esc

            if (keyInfo.Key >= ConsoleKey.F1 && // if key between F1 & F12
                keyInfo.Key <= ConsoleKey.F12)
            {
                enc[1] = 0x4F; // O
                switch (keyInfo.Key)
                {
                case ConsoleKey.F1:
                    enc[2] = 0x50;     // P
                    break;

                case ConsoleKey.F2:
                    enc[2] = 0x51;     // Q
                    break;

                case ConsoleKey.F3:
                    enc[2] = 0x52;     // R
                    break;

                case ConsoleKey.F4:
                    enc[2] = 0x53;     // S
                    break;

                case ConsoleKey.F5:
                    enc[2] = 0x54;
                    break;

                case ConsoleKey.F6:
                    enc[2] = 0x55;
                    break;

                case ConsoleKey.F7:
                    enc[2] = 0x56;
                    break;

                case ConsoleKey.F8:
                    enc[2] = 0x57;
                    break;

                case ConsoleKey.F9:
                    enc[2] = 0x58;
                    break;

                case ConsoleKey.F10:
                    enc[2] = 0x59;
                    break;

                case ConsoleKey.F11:
                    enc[2] = 0x5A;
                    break;

                case ConsoleKey.F12:
                    enc[2] = 0x5B;
                    break;

                default:
                    break;
                }
            }
            else if (keyInfo.Key >= ConsoleKey.LeftArrow && // if key is Arrow
                     keyInfo.Key <= ConsoleKey.DownArrow)
            {
                enc[1] = 0x5B; // bracket

                switch (keyInfo.Key)
                {
                case ConsoleKey.UpArrow:
                    enc[2] = 0x41;     // P
                    break;

                case ConsoleKey.DownArrow:
                    enc[2] = 0x42;     // P
                    break;

                case ConsoleKey.RightArrow:
                    enc[2] = 0x43;     // P
                    break;

                case ConsoleKey.LeftArrow:
                    enc[2] = 0x44;     // P
                    break;

                default:
                    break;
                }
            }
            else if (keyInfo.Key == ConsoleKey.Enter) // if key is Enter
            {
                byte enc_length;

                if (enterEncodeCRLF)
                {
                    enc_length = 2;
                    enc        = new byte[2] {
                        0x0D, 0x0A
                    };
                }
                else
                {
                    enc_length = 1;
                    enc        = new byte[1] {
                        0x0D
                    };
                }

                if (scrData != string.Empty && scrData.Length > 0)
                {
                    // get screen data bytes
                    byte[] scrPayload = Encoding.UTF8.GetBytes(scrData);

                    // flush screen data
                    scrData = string.Empty;

                    // create new serialized packet with screen bytes and return payload
                    enc = new byte[(scrPayload.Length + enc_length)];

                    Buffer.BlockCopy(scrPayload, 0, enc, 0, scrPayload.Length);

                    // Add return key
                    enc[scrPayload.Length] = 0x0D;
                    if (enterEncodeCRLF)
                    {
                        enc[(scrPayload.Length + 1)] = 0x0A;
                    }
                }
            }
            else if (keyInfo.Key == ConsoleKey.Delete)
            {
                // ^[3~"
                enc    = new byte[4];
                enc[0] = 0x1B;
                enc[1] = 0x5B; // bracket
                enc[2] = 0x33;
                enc[3] = 0x7E;
            }
            else if (keyInfo.Key == ConsoleKey.Escape) // Escape
            {
                enc    = new byte[1];
                enc[0] = 0x1B;
            }
            else if (keyInfo.Key == ConsoleKey.C &&
                     keyInfo.Modifiers == ConsoleModifiers.Control) // Ctrl + C
            {
                // Issue a graceful terminate.
                SharedFunc.SetSerialSession(false);
            }

            return(enc);
        }