public static void Main()
    {
        string         name;
        string         message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread         readThread     = new Thread(Read);
        ByteStuffer    byteStuffer    = new ByteStuffer();

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName  = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate  = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity    = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits  = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits  = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout  = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        //data sending loop
        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                String begin_end_flag = "01111110";
                String afterStuffing  = byteStuffer.CodeBytes(message, ByteStufferWorkStrategy.CODE, begin_end_flag);
                Console.WriteLine("data after stuffing");
                Console.WriteLine(afterStuffing);
                String afterStuffingWithBeginFlag    = afterStuffing.Insert(0, begin_end_flag);
                String afterStuffingWithBeginEndFlag = afterStuffingWithBeginFlag.Insert(afterStuffingWithBeginFlag.Length, begin_end_flag);
                Console.WriteLine("data after stuffing with flags");
                Console.WriteLine(afterStuffingWithBeginEndFlag);
                _serialPort.WriteLine(
                    String.Format("{1}", name, afterStuffingWithBeginEndFlag));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }
    public static void Read()
    {
        ByteStuffer byteStuffer = new ByteStuffer();

        //data recieving loop
        while (_continue)
        {
            try

            {
                string message = _serialPort.ReadLine();
                Console.WriteLine("raw data");
                Console.WriteLine(message);

                String begin_end_flag          = "01111110";
                String messageWithoutBeginFlag = message.Replace(begin_end_flag, "");
                Console.WriteLine("data w/o flags");
                Console.WriteLine(messageWithoutBeginFlag);
                Console.WriteLine("decoded data");
                Console.WriteLine(byteStuffer.CodeBytes(messageWithoutBeginFlag, ByteStufferWorkStrategy.DECODE, "0111111"));
            }
            catch (TimeoutException) { }
        }
    }