[spoiler]
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;
using System.IO.Ports;
using System.IO;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DeviceApplication4
{
    public partial class Form1 : Form
    {   
        //import the coredll.dll with the functions QueryPerformanceCounter
        //and QueryPerformanceFrequency
        [DllImport("coredll.dll")]
        extern static int QueryPerformanceCounter(ref long perfCounter);
        [DllImport("coredll.dll")]
        extern static int QueryPerformanceFrequency(ref long frequency);
        //initialize the parameters for the QueryPerformanceCounter
        long ctrStart = 0, ctrAkt = 0, ctrEnd = 0;
        
        //initialize the parameters for the SerialPort object
        public static String portName = "COM2";
        static int baudRate = 115200;
        public static Parity parity = Parity.None;
        static int dataBits = 7;
        public static StopBits stopBits = StopBits.One;
        SerialPort IR_COM = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
        //initialize the char-array and variables for the function ManchesterCode and RohCodeToManchester
        char[] rohData = new char[14] { '1','1','0','0','0','0','0','0','0','0','0','1','0','0' };  
        public char[] manchesterData = new char[29];
        int index = 0, md = 0;
        
        //initialize the byte-array for the function BurstOut
        byte[] bufferBurst = new byte[22] { 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B, 0x5B };
        
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            GetName();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //set the ToggleBit
            ToggleBit();
            //manchester coding
            RohCodeToManchester();
 
            //open the port and send data
            try
            {
                if (IR_COM.IsOpen == false)
                {
                    IR_COM.Open();
                }
                //message   
                statusBar2.Text = "port status: open";
                    
                //send data
                BurstOut();  
                //reset the indices
                md = 0;
                index = 0;
                //message
                statusBar1.Text = "status: data has been sent";
                //close the port
                IR_COM.Close();
            }
            catch (ArgumentOutOfRangeException)
            {
                label1.Text = "Der ьbergebene Parity-Wert oder Stopbit-Wert ist kein gьltiger Wert in der Enumeration.";
            }
            catch (NullReferenceException nre)
            {
                label1.Text = nre.ToString();
            }
            catch (ArgumentNullException)
            {
                label1.Text = "Der ьbergebene IsOpen-Wert ist NULL (Nothing in Visual Basic).";
            }
            catch (ArgumentException)
            {
                label1.Text = "Der ьbergebene IsOpen-Wert ist eine leere Zeichenfolge!";
            }
            catch (IOException)   
            {
                label1.Text = "Der Port kann nicht geцffnet werden!";
            }
        }
        public void GetName()
        {
            StringBuilder s = new StringBuilder();
            String s1;
            //get a list of port names
            string[] ports = SerialPort.GetPortNames();
            //display port names
            foreach (string port in ports)
            {
                s.Append(port+" ");
            }
            s1 = s.ToString();
            label1.Text = "Port Names: "+s1;
        }
        public void ManchesterCode(char c)
        {   
             if (c == '1')
             {  // 1 -> 10
                 manchesterData[index] = '1';
                 manchesterData[++index] = '0';
             }
             if (c == '0')
             {  // 0 -> 01
                 manchesterData[index] = '0';
                 manchesterData[++index] = '1';
             }
             index++;    
        }
        public void RohCodeToManchester()
        {   
            for (int i=0; i < rohData.Length; i++)
            {   //the array rohData is passed to the function ManchesterCode
                ManchesterCode(rohData[i]);
            }
            //mark the end
            manchesterData[28] = 'e';
        }
        public void ToggleBit()
        {
            if (rohData[2] == '1') rohData[2] = '0';
            else { rohData[2] = '1'; }
        }
        public void QPC(long data)
        {   //start the counter
            QueryPerformanceCounter(ref ctrStart);
            
            //specified the end time
            ctrEnd = ctrStart + data;
            while (ctrAkt < ctrEnd)
            {   //wait until the counter reaches the end time
                QueryPerformanceCounter(ref ctrAkt);
            }
        }
        public void BurstOut()
        {
            while (manchesterData[md] != 'e')
            {   
                //pause of 889µs
                if (manchesterData[md] == '1' && manchesterData[md + 1] == '0')
                {
                    QPC(319);
                }
                //pause of 1,778ms
                if (manchesterData[md] == '1' && manchesterData[md + 1] == '1')
                {
                    QPC(766);
                    md++;
                }
                //burst of 889µs
                if (manchesterData[md] == '0' && manchesterData[md + 1] == '1')
                {
                    IR_COM.Write(bufferBurst, 0, 10);
                }
                //pause of 1,778ms
                if (manchesterData[md] == '0' && manchesterData[md + 1] == '0')
                {
                    IR_COM.Write(bufferBurst, 0, 22);
                    md++;
                }
                //the end
                if (manchesterData[md] == '0' && manchesterData[md + 1] == 'e')
                {
                    IR_COM.Write(bufferBurst, 0, 10);
                }
                md++;
            }
        }      
    }
}[/spoiler]