UART + Glide not working

Hi!

Can you help me find out why my code doesnt work if I inserted its real application (UART interfacing with RFID reader). Here’s the code.


using System.Threading;
using System;
using Microsoft.SPOT;
using System.IO.Ports;
using Microsoft.SPOT.Input;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT.Touch;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
namespace Test
{
    public class Program : Microsoft.SPOT.Application
    {

        static Window window;
        static bool ByteArrayCompare(byte[] a1, byte[] a2)
        {
            //if (a1.Length != a2.Length)
            //    return false;

            for (int i = 0; i < a1.Length; i++)
                if (a1[i] != a2[i])
                    return false;

            return true;
        }
        static int maximum(byte[] numbers)
        {
            byte m = numbers[0];
            int index = 0; int maxint = 0;
            for (index = 0; index < numbers.Length; index++)
                if (m < numbers[index])
                {
                    m = numbers[index];
                    maxint = index;
                }
            return maxint;
        }
        // This will hold the main window.

        static int maxim(byte[] numbers)
        {
            byte m = numbers[0];
            int index = 0; int maxint = 0;
            for (index = 0; index < numbers.Length; index++)
                if (m < numbers[index])
                {
                    m = numbers[index];
                    maxint = index;
                }
            return maxint;
        }
        private static string ByteArrayToHex(byte[] barray)
        {
            char[] c = new char[barray.Length * 2];
            byte b;
            for (int i = 0; i < barray.Length; ++i)
            {
                b = ((byte)(barray[i] >> 4));
                c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
                b = ((byte)(barray[i] & 0xF));
                c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
            }

            return new string(c);
        }

        public static void Main()
        {
            // Load the window
            string xml;
            xml = "<Glide Version=\"" + Glide.Version + "\">";
            xml += "<Window Name=\"window\" Width=\"320\" Height=\"240\" BackColor=\"dce3e7\">";
            xml += "<Button Name=\"init\" X=\"240\" Y=\"8\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"init\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<Button Name=\"btn\" X=\"4\" Y=\"202\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"Read\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<Button Name=\"stopbtn\" X=\"236\" Y=\"202\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"Clear\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<TextBlock Name=\"text0\" X=\"5\" Y=\"9\" Width=\"200\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"Frequency\" X=\"10\" Y=\"102\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Frequency:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"EPC\" X=\"10\" Y=\"73\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"EPC:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"RSSI\" X=\"10\" Y=\"130\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"RSSI(-):\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"Phase\" X=\"9\" Y=\"158\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Phase:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text1\" X=\"105\" Y=\"71\" Width=\"200\" Height=\"32\" Alpha=\"255\" Text=\" \" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"2\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text2\" X=\"107\" Y=\"103\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text3\" X=\"106\" Y=\"130\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text4\" X=\"106\" Y=\"157\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"kHz\" X=\"215\" Y=\"104\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"kHz\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"dBm\" X=\"215\" Y=\"130\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"dBm\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"deg\" X=\"215\" Y=\"158\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"deg\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"tagsdet\" X=\"89\" Y=\"203\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Tags Detected:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"0\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text5\" X=\"199\" Y=\"200\" Width=\"35\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"0\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
        //    xml += "<Image Name=\"img1\" X=\"2\" Y=\"6\" Width=\"317\" Height=\"35\" Alpha=\"255\"/>";
            //   xml += "<Button Name=\"btn\" X=\"100\" Y=\"104\" Width=\"120\" Height=\"32\" Alpha=\"255\" Text=\"Read\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "</Window>";
            xml += "</Glide>";

        //      SerialPort UART = new SerialPort("COM1", 115200);
        //      UART.Handshake = Handshake.None;

            // Resize any loaded Window to the LCD's size.
            Glide.FitToScreen = true;

            // Load the Window XML string.
            window = GlideLoader.LoadWindow(xml);

            // Activate touch
            GlideTouch.Initialize();

            // Initialize the window.
            InitWin();

            // Assigning a window to MainWindow flushes it to the screen.
            // This also starts event handling on the window.
            Glide.MainWindow = window;

            Thread.Sleep(-1);
        }

        static void InitWin()
        {
            TextBlock freq = (TextBlock)window.GetChildByName("Frequency");
            TextBlock epc = (TextBlock)window.GetChildByName("EPC");
            TextBlock rssi = (TextBlock)window.GetChildByName("RSSI");
            TextBlock phase = (TextBlock)window.GetChildByName("Phase");
            TextBlock tagsdet = (TextBlock)window.GetChildByName("tagsdet");
            TextBlock kHz = (TextBlock)window.GetChildByName("kHz");
            TextBlock dBm = (TextBlock)window.GetChildByName("dBm");
            TextBlock deg = (TextBlock)window.GetChildByName("deg");
            TextBlock text = (TextBlock)window.GetChildByName("text0");

            // Get the Button
            Button btn = (Button)window.GetChildByName("btn");
            Button stopbtn = (Button)window.GetChildByName("stopbtn");
            Button init = (Button)window.GetChildByName("init");
            // Listen for tap events
            btn.TapEvent += new OnTap(btn_TapEvent);
            stopbtn.TapEvent += new OnTap(stopbtn_TapEvent);
            init.TapEvent += new OnTap(init_TapEvent);

        }
        static void init_TapEvent(object sender)
        {
            SerialPort UART = new SerialPort("COM1", 115200);
            UART.Handshake = Handshake.None;
          //  int read_count = 0;
            var rx_data = new byte[1000];
            byte[] datarray = new byte[rx_data.Length];
            byte[] b = { 0xFF, 0x00, 0x68, 0x1D, 0x67 };
            byte[] gp_on;
            byte[] boot = { 0xFF, 0x00, 0x03, 0x1D, 0x0C };
            byte[] getprog = { 0xFF, 0x00, 0x0C, 0x1D, 0x03 };
            byte[] currprog = new byte[] { 0xFF, 0x01, 0x68, 0x00, 0x00, 0x00, 0xA4, 0xBF };
            byte[] gpset = new byte[] { 0xFF, 0x04, 0x96, 0x01, 0x03, 0x01, 0x00, 0x0C, 0x2A };
            byte[] testgp = { 0xFF, 0x00, 0x96, 0x00, 0x00, 0x67, 0xBF };
            gp_on = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x01, 0x03, 0xDC };
            byte[] gp_off = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x00, 0x03, 0xDB };
            byte[] setprot = new byte[] { 0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D };
            byte[] getprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
            byte[] currprot = new byte[] { 0xFF, 0x02, 0x63, 0x00, 0x00, 0x00, 0x05, 0x21, 0x46 };
            byte[] setreg = new byte[] { 0xFF, 0x01, 0x97, 0xFF, 0x4B, 0x42 }; //North Am Region(FF0197014BBC), OPEN( FF  01  97  FF  4B  42)
            //        byte[] rec_reg = new byte[] { 0xFF, 0x00, 0x97, 0x00, 0x00, 0x77, 0x9E };
            byte[] setant1 = new byte[] { 0xFF, 0x02, 0x91, 0x01, 0x01, 0x70, 0x3B };
            byte[] rec_ant1 = new byte[] { 0xFF, 0x00, 0x91, 0x00, 0x00, 0x17, 0x58 };
            byte[] setEPClen = new byte[] { 0xFF, 0x03, 0x9A, 0x01, 0x02, 0x01, 0xAD, 0x5C };
            byte[] rec_EPClen = new byte[] { 0xFF, 0x00, 0x9A, 0x00, 0x00, 0xA6, 0x33 };
            byte[] clrtagbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x1D, 0x25 };
            byte[] rec_clrbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x00, 0x00, 0x01, 0xE8 };
            byte[] gettagdata = new byte[] { 0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03 };
            byte[] setbaud = new byte[] { 0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4, 0x60 };
            byte[] readconfig1 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C };
            byte[] antconfig = new byte[] { 0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8 };
            byte[] readconfig2 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42 };
            byte[] gettagprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
            byte[] freqtable = new byte[] { 0xFF, 0x6C, 0x95, 0x00, 0x0D, 0xC3, 0x70, 0x00, 0x0D, 0xC7, 0x58, 0x00, 0x0D, 0xCB, 0x40, 0x00, 0x0D, 0xCF, 0x28, 0x00, 0x0D, 0xD3, 0x10, 0x00, 0x0D, 0xD6, 0xF8, 0x00, 0x0D, 0xDA, 0xE0, 0x00, 0x0D, 0xDE, 0xC8, 0x00, 0x0D, 0xE2, 0xB0, 0x00, 0x0D, 0xE6, 0x98, 0x00, 0x0D, 0xEA, 0x80, 0x00, 0x0D, 0xEE, 0x68, 0x00, 0x0D, 0xF2, 0x50, 0x00, 0x0D, 0xF6, 0x38, 0x00, 0x0D, 0xFA, 0x20, 0x00, 0x0D, 0xFE, 0x08, 0x00, 0x0E, 0x01, 0xF0, 0x00, 0x0E, 0x05, 0xD8, 0x00, 0x0E, 0x09, 0xC0, 0x00, 0x0E, 0x0D, 0xA8, 0x00, 0x0E, 0x11, 0x90, 0x00, 0x0E, 0x15, 0x78, 0x00, 0x0E, 0x19, 0x60, 0x00, 0x0E, 0x1D, 0x48, 0x00, 0x0E, 0x21, 0x30, 0x00, 0x0E, 0x25, 0x18, 0x00, 0x0E, 0x29, 0x00, 0xD0, 0x50 };
            byte[] staticQ = new byte[] { 0xFF, 0x04, 0x9B, 0x05, 0x12, 0x01, 0x00, 0x80, 0xAF };
            byte[] readpower = new byte[] { 0xFF, 0x02, 0x92, 0x06, 0xA4, 0x47, 0xFD };
            TextBlock text = (TextBlock)window.GetChildByName("text0");
            UART.ReadTimeout = 0;
            if (UART.IsOpen)
            {
                text.Text = "COM Port open!";
                text.Invalidate();
            }
         /*   UART.Write(boot, 0, boot.Length);
            Thread.Sleep(700);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0))
            {
                text.Text = "Initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "firmware not ok";
                text.Invalidate();
            }
            UART.Write(getprog, 0, getprog.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get current program not ok";
                text.Invalidate();
            }
            UART.Write(b, 0, b.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if (ByteArrayCompare(currprog, rx_data))
            {
                text.Text = "initializing";
                text.Invalidate();
                //       lcd.ShowMessage("Full Power Mode");
                Thread.Sleep(200);
            }
            UART.Write(setbaud, 0, setbaud.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "baud set ok";
                text.Invalidate();
            }
            else
            {
                text.Text = "baudset not ok";
                text.Invalidate();
            }
            UART.Write(readconfig1, 0, readconfig1.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {

                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "read config 1 not ok";
                text.Invalidate();
            }
            UART.Write(antconfig, 0, antconfig.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get antenna config not ok";
                text.Invalidate();
            }
            UART.Write(readconfig2, 0, readconfig2.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get read config 2 not ok";
                text.Invalidate();
            }
            UART.Write(gettagprot, 0, gettagprot.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get current tag protocol not ok";
                text.Invalidate();
            }
            UART.Write(gpset, 0, gpset.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if (ByteArrayCompare(testgp, rx_data))
            {
                text.Text = "initializing";
                text.Invalidate();
                //  lcd.ShowMessage("Correct GPSettings");
            }
            else
            {
                text.Text = "Incorrect GPsettings";
                text.Invalidate();
            }
            UART.Write(setprot, 0, setprot.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            Thread.Sleep(500);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "set tag protocol not ok";
                text.Invalidate();
            }
            UART.Write(getprot, 0, getprot.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
                //  lcd.ShowMessage("TagProt is Gen2");
            }
            else
            {
                text.Text = "Incorrect TagProt settings";
                text.Invalidate();
            }
            Thread.Sleep(500);
            UART.Write(setreg, 0, setreg.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
                //   lcd.ShowMessage("Region is Open");
            }
            else
            {
                text.Text = "region not set properly";
                text.Invalidate();
            }
            UART.Write(antconfig, 0, antconfig.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get antenna config not ok";
                text.Invalidate();
            }
            UART.Write(clrtagbuf, 0, clrtagbuf.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "tag buff not cleared";
                text.Invalidate();
            } */
            text.Text = "initializing";
            text.Invalidate();
        }
        static void stopbtn_TapEvent(object sender)
        {
            Button btn = (Button)window.GetChildByName("btn");
            //    Button stopbtn = (Button)window.GetChildByName("stopbtn");
            TextBox text1 = (TextBox)window.GetChildByName("text1");
            TextBox text2 = (TextBox)window.GetChildByName("text2");
            TextBox text3 = (TextBox)window.GetChildByName("text3");
            TextBox text4 = (TextBox)window.GetChildByName("text4");
            TextBox text5 = (TextBox)window.GetChildByName("text5");
            text1.Text = " ";
            text1.Invalidate();
            text2.Text = " ";
            text2.Invalidate();
            text3.Text = " ";
            text3.Invalidate();
            text4.Text = " ";
            text4.Invalidate();
            text5.Text = " ";
            text5.Invalidate();
            InitWin();
            //    btn.TapEvent += new OnTap(btn_TapEvent);
            //        stopbtn.TapEvent += new OnTap(stopbtn_TapEvent);
        }

        // Handle tap events
        static void btn_TapEvent(object sender)
        {
          //\  byte e = new byte();

            TextBox text1 = (TextBox)window.GetChildByName("text1");
            TextBox text2 = (TextBox)window.GetChildByName("text2");
            TextBox text3 = (TextBox)window.GetChildByName("text3");
            TextBox text4 = (TextBox)window.GetChildByName("text4");
            TextBox text5 = (TextBox)window.GetChildByName("text5");
                    byte[] datarray ={   0xFF,  0x40, 0x29, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x02,  0x01, 0xD8,  0x11, 0x0E, 0x16,
    0x72, 0x00, 0x00, 0x00, 0x2D, 0x00, 0x27, 0x05, 0x00, 0x00, 0x0E, 0x00, 0x40, 0x10, 0x00, 0x24,
    0x00, 0x13,  0x98, 0x42, 0x45, 0x03, 0xE8, 0x11, 0x0E, 0x16, 0x72, 0x00,
    0x00,  0x00,  0x21, 0x00, 0x35, 0x05,0x00, 0x00, 0x0E, 0x00, 0x80, 0x30, 0x00, 0x30, 0x08, 0x33,
    0xB2,  0xDD,  0xD9, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x39, 0xBB,  0x5B,  0xEE};
            int e = datarray[8];
            int freqbef1 = 0;
            byte[] freqbef = new byte[3];
            byte[] rssibef = new byte[e];
            byte[] phasebef = new byte[e];
            int[] EPCflag = new int[e];
            byte[] EPCs = new byte[150];
            byte[] EPC = new byte[20];
                int intc = 0, count = 0, count1 = 0;
                int cnt = 9; int tagcnt = 0; int[] tagptr = new int[e + 1];
                int m = 0;
                int size = 0;
                tagptr[0] = cnt; intc++;  //int freqaft1 = 0;
                freqbef[count1++] = datarray[cnt + 3];
                freqbef[count1++] = datarray[cnt + 4];
                freqbef[count1] = datarray[cnt + 5];
                freqbef1 = freqbef[count1] + (freqbef[count1 - 1] << 8) + (freqbef[count1 - 2] << 16);
                int EPCont = 0;
                do
                {
                    m = 0;
                    if (datarray[cnt + 17] == 0x80)
                    {
                        //             lcd.ShowMessage("EPC length is 0x80 XD"); Thread.Sleep(500);
                        EPCflag[tagcnt] = 1;
                        size += 12;
                        do
                        {
                            EPCs[EPCont] = datarray[(cnt + m) + 20];
                            m++; EPCont++;
                        } while (m < 12);
                        cnt += 34;// Thread.Sleep(100);
                        tagptr[intc++] = cnt;
                    }
                    else if (datarray[cnt + 17] == 0x40)
                    {
                        //          lcd.ShowMessage("EPC length is 0x40"); Thread.Sleep(500);
                        size += 4;
                        do
                        {
                            EPCs[EPCont] = datarray[(cnt + m) + 20];
                            m++; EPCont++;
                        } while (m < 4);
                        cnt += 26;
                        EPCflag[tagcnt] = 2;
                        tagptr[intc++] = cnt;
                    }
                    tagcnt++;
                } while (tagcnt < e);
                cnt = tagcnt = 0; count = 0;
                // int n = 0; 
                /*   do
                   {
                       lcd.ShowMessage("EPCflag [" + n + "]: " + EPCflag[n]);
                       Thread.Sleep(1500);
                       n++;
                   } while (n < e); */
                //  n = 0;
                /*       do
                       {
                           lcd.ShowMessage("EPCs [" + n + "]: " + EPCs[n]); Thread.Sleep(100); n++;
                       } while (n < EPCont); */
                tagcnt = 0; int index = 0; 
                do
                {
                    index = tagptr[tagcnt];
                    rssibef[count] = datarray[index + 1];
                    phasebef[count++] = datarray[index + 9];
                    tagcnt++;
                } while (tagcnt < e); int j = 0; cnt = 0; int p = 0; tagcnt = 0;
                index = 0;
                do
                {
                    if (EPCflag[j] == 2)
                    {
                        while (cnt < 4)
                        {
                            Array.Copy(EPCs, p, EPC, 0, 4); cnt = 4;
                    //        lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                        } //p += 12; 
                        p = p+ 4;
                    }
                    if (EPCflag[j] == 1)
                    {
                        while (cnt < 12)
                        {
                            Array.Copy(EPCs, p++, EPC, 0, 12); cnt = 12;
                    //        lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                        }// p += 4;
                        p = p + 12;
                    }
                    text1.Text = ByteArrayToHex(EPC);
                    text1.Invalidate();
                    text2.Text = freqbef1.ToString();
                    text2.Invalidate();
         //           lcd.ShowMessage("Frequency:" + freqbef1); Thread.Sleep(500);
                    text3.Text = (256 - rssibef[j]).ToString();
                    text3.Invalidate();
         //           lcd.ShowMessage("RSSI [" + j + "] -" + (256 - rssibef[j]));
         //           Thread.Sleep(500);
                    text4.Text = phasebef[j].ToString();
                    text4.Invalidate();
                    text5.Text = e.ToString();
                    text5.Invalidate();
         //           lcd.ShowMessage("Phases [" + j + "] " + phasebef[j]);
                    Thread.Sleep(500);
                    j++; tagcnt++; cnt = 0;
                } while (j < e);
              
        }
    }
}

I would really appreciate your help. :slight_smile:

The code works with the Glide alone. Also, the code for UART works without the glide (i have a class called LCD that prints if connection has established and the proper bytes sent by reader). However, when I integrated it with the GUI, FEZ screen doesn’t show even the GUI. :frowning:
I really dont know what to do now. Hope you could lend me a hand in this one.

I have a button for initialization of the reader (therefore, establishes UART connection, there and then). However, as what I’ve said, it doesn’t work. :frowning:

What I mean by integration is that, instead of the class used for printing the results, i used textblock named text. :

Update:

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
//if i used these, together with the reference, GTM.GHIElectronics.Display_T35, the UART is fine… however if i use this with the glide, this doesnt seem to work. :frowning:

If you’re using the Gadgeteer framework portions, you need to have a Gadgeteer app, but you’re working in a non-Gadgeteer way (using Main() not ProgramStarted() and letting that finish initialising) so I think that’s what you need to do - use one methodology not mixing them

@ Brett - I also use the main function when I used the UART… so I thought basically, by just pasting the code will be just fine. Can you suggest something that I have to do for this to work? I badly need this to get to work today. Hope you can help me

@ Brett - what do you mean by gadgeteer app by the way? i use T35 display (the screen included in FEZ), since I’ll be using the display. is that a gadgeteer app?

Stop using the gadgeteer bits with your MAIN() loop.

You’re confusing two things, don’t try to mix them. If you’re not using Gadgeteer modules and the designer, then you don’t need to use Gadgeteer methods, so get rid of the USING and REFERENCES for Gadgeteer.

I mean when you say “new project”, did you select Gadgeteer or netmf and Cobra. If you use Gadgeteer, don’t edit MAIN(), you need to only edit ProgramStarted(). If you didn’t use Gadgeteer app, then stop using Gadgeteer code as it relies on the dispatcher to have properly completed - see http://blogs.msdn.com/b/net_gadgeteer/archive/2011/12/19/why-not-while-true.aspx for some background.

Next step: capture a screen shot of your REFERENCES in your project

@ Brett - I selected the project FEZ Cobra WIndow Application. And I didnt use gadgeteer. Can you suggest something that I should do to make this thing to work? This is a screenshot of my working UART code program (minus the references for gadgeteer). I dont know why it doesnt work if I didnt use gadgeteer as reference.

ok, so that’s the right thing to do.

You have the USINGs related to Gadgeteer commented out, great.

Now what’s the behaviour of the app? Can you post the current code?

@ Brett - The FEZ screen says “waiting for debug command”… Here’s my looong code. :expressionless: pardon for this…


using System;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using Microsoft.SPOT;
using System.IO.Ports;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Touch;
using System.Text;
//using GT = Gadgeteer;
//using GTM = Gadgeteer.Modules;
//using Gadgeteer.Modules.GHIElectronics;

namespace FEZ_Cobra_Window_Application3
{
    public class Program : Microsoft.SPOT.Application
    {
        class LCD
        {
            public Bitmap LCDPanel = new Bitmap(SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);
            Font font = Resources.GetFont(Resources.FontResources.small);
            
            public LCD()
            {
                LCDPanel.Clear();
                LCDPanel.Flush();
            }

            public void ShowMessage(string text)
            {
             
                LCDPanel.Clear();
                LCDPanel.DrawText(text, font, Colors.White, 0,5);
                LCDPanel.Flush();
                
            }

            static bool ByteArrayCompare(byte[] a1, byte[] a2)
            {
                //if (a1.Length != a2.Length)
                //    return false;

                for (int i = 0; i < a1.Length; i++)
                    if (a1[i] != a2[i])
                        return false;

                return true;
            }
            static int maximum(byte[] numbers)
            {
                byte m = numbers[0];
                int index = 0; int maxint = 0;
                for (index = 0; index < numbers.Length; index++)
                    if (m < numbers[index])
                    {
                        m = numbers[index]; 
                        maxint = index;
                    }
                return maxint;
            }
            public static void Main()
            {
                LCD lcd = new LCD();
                SerialPort UART = new SerialPort("COM1", 115200);
                UART.Handshake = Handshake.None;
                int read_count = 0;
                byte a = new byte(); byte d = new byte(); byte e = new byte();
                var rx_data = new byte[1000];
                byte[] datarray = new byte[rx_data.Length];
                byte[] b =  { 0xFF, 0x00, 0x68, 0x1D, 0x67 };
                byte[] gp_on;
                byte[] boot = { 0xFF, 0x00, 0x03, 0x1D, 0x0C };
                byte[] getprog = { 0xFF, 0x00, 0x0C, 0x1D, 0x03 };
                byte[] currprog = new byte[] { 0xFF, 0x01, 0x68, 0x00, 0x00, 0x00, 0xA4, 0xBF };
                byte[] gpset = new byte[] { 0xFF, 0x04, 0x96, 0x01, 0x03, 0x01, 0x00, 0x0C, 0x2A };
                byte[] testgp = { 0xFF, 0x00, 0x96, 0x00, 0x00, 0x67, 0xBF };
                gp_on = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x01, 0x03, 0xDC };
                byte[] gp_off = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x00, 0x03, 0xDB };
                byte[] setprot = new byte[] { 0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D };
                byte[] getprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
                byte[] currprot = new byte[] { 0xFF, 0x02, 0x63, 0x00, 0x00, 0x00, 0x05, 0x21, 0x46 };
                byte[] setreg = new byte[] { 0xFF, 0x01, 0x97, 0xFF, 0x4B, 0x42 }; //North Am Region(FF0197014BBC), OPEN( FF  01  97  FF  4B  42)
        //        byte[] rec_reg = new byte[] { 0xFF, 0x00, 0x97, 0x00, 0x00, 0x77, 0x9E };
                byte[] setant1 = new byte[] { 0xFF, 0x02, 0x91, 0x01, 0x01, 0x70, 0x3B };
                byte[] rec_ant1 = new byte[] { 0xFF, 0x00, 0x91, 0x00, 0x00, 0x17, 0x58 };
                byte[] setEPClen = new byte[] { 0xFF, 0x03, 0x9A, 0x01, 0x02, 0x01, 0xAD, 0x5C };
                byte[] rec_EPClen = new byte[] { 0xFF, 0x00, 0x9A, 0x00, 0x00, 0xA6, 0x33 };
                byte[] clrtagbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x1D, 0x25 };
                byte[] rec_clrbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x00, 0x00, 0x01, 0xE8 };
                byte[] gettagdata = new byte[] { 0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03 };
                byte[] setbaud = new byte[] { 0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4, 0x60 };
                byte[] readconfig1 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C };
                byte[] antconfig = new byte[] { 0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8 };
                byte[] readconfig2 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42 };
                byte[] gettagprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
                byte[] freqtable = new byte[] { 0xFF, 0x6C, 0x95, 0x00, 0x0D, 0xC3, 0x70, 0x00, 0x0D, 0xC7, 0x58, 0x00, 0x0D, 0xCB, 0x40, 0x00, 0x0D, 0xCF, 0x28, 0x00, 0x0D, 0xD3, 0x10, 0x00, 0x0D, 0xD6, 0xF8, 0x00, 0x0D, 0xDA, 0xE0, 0x00, 0x0D, 0xDE, 0xC8, 0x00, 0x0D, 0xE2, 0xB0, 0x00, 0x0D, 0xE6, 0x98, 0x00, 0x0D, 0xEA, 0x80, 0x00, 0x0D, 0xEE, 0x68, 0x00, 0x0D, 0xF2, 0x50, 0x00, 0x0D, 0xF6, 0x38, 0x00, 0x0D, 0xFA, 0x20, 0x00, 0x0D, 0xFE, 0x08, 0x00, 0x0E, 0x01, 0xF0, 0x00, 0x0E, 0x05, 0xD8, 0x00, 0x0E, 0x09, 0xC0, 0x00, 0x0E, 0x0D, 0xA8, 0x00, 0x0E, 0x11, 0x90, 0x00, 0x0E, 0x15, 0x78, 0x00, 0x0E, 0x19, 0x60, 0x00, 0x0E, 0x1D, 0x48, 0x00, 0x0E, 0x21, 0x30, 0x00, 0x0E, 0x25, 0x18, 0x00, 0x0E, 0x29, 0x00, 0xD0, 0x50 };
                byte[] staticQ = new byte[] { 0xFF, 0x04, 0x9B, 0x05, 0x12, 0x01, 0x00, 0x80, 0xAF };
                byte[] readpower = new byte[] { 0xFF, 0x02, 0x92, 0x06, 0xA4, 0x47, 0xFD };
                UART.ReadTimeout = 0;
                if (UART.IsOpen)
                {
                    lcd.ShowMessage("COM port is open!"); Thread.Sleep(100);
                }
                else
                {
                    lcd.ShowMessage("Failed to open port");
                    UART.Open();
                    lcd.ShowMessage("COM port is now open!"); Thread.Sleep(100);
                }
                UART.Write(boot, 0, boot.Length);
                Thread.Sleep(700);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0)){
                    lcd.ShowMessage("get firmware ok.");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("firmware not ok");
                    Thread.Sleep(500);
                }
                lcd.ShowMessage("read_count for boot=" + read_count); Thread.Sleep(500);
                for (int i = 0; i < read_count; i++)
                {
                    lcd.ShowMessage("read_count for firm/app[" + i + "] " + rx_data[i]);
                    Thread.Sleep(1000);
                }
                UART.Write(getprog, 0, getprog.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("get current program ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("get current program not ok");
                    Thread.Sleep(500);
                }
                UART.Write(b, 0, b.Length);
                Thread.Sleep(650);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if (ByteArrayCompare(currprog, rx_data))
                {
                    Debug.Print("Full Power Mode");
                    lcd.ShowMessage("Full Power Mode");
                    Thread.Sleep(200);
                }
                UART.Write(setbaud, 0, setbaud.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("baud set ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("baud set not ok");
                    Thread.Sleep(500);
                }
                UART.Write(readconfig1, 0, readconfig1.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("read configuration 1 ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("read configuration 1 not ok");
                    Thread.Sleep(500);
                }
                UART.Write(antconfig, 0, antconfig.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("get antenna config ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("get antenna config not ok");
                    Thread.Sleep(500);
                }
                UART.Write(readconfig2, 0, readconfig2.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("get read config 2 ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("get read config 2 not ok");
                    Thread.Sleep(500);
                }
                UART.Write(gettagprot, 0, gettagprot.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("get current tag protocol ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("get current tag protocol not ok");
                    Thread.Sleep(500);
                }
                /*UART.Write(gpset, 0, gpset.Length);
                Thread.Sleep(650);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if (ByteArrayCompare(testgp, rx_data))
                {
                    Debug.Print("Correct GPSettings");
                    lcd.ShowMessage("Correct GPSettings");
                }
                else
                {
                    Debug.Print("Incorrect GPSettings :(");
                    lcd.ShowMessage("Incorrect GPSettings :(");
                } */
                UART.Write(setprot, 0, setprot.Length);
                Thread.Sleep(650);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                Thread.Sleep(500);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("set tag protocol ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("set tag protocol not ok");
                    Thread.Sleep(500);
                }
                UART.Write(getprot, 0, getprot.Length);
                Thread.Sleep(650);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    Debug.Print("TagProt is Gen2");
                    lcd.ShowMessage("TagProt is Gen2");
                }
                else
                {
                    Debug.Print("Incorrect TagProt settings");
                    lcd.ShowMessage("Incorrect TagProt settings");
                }
                Thread.Sleep(500);
                UART.Write(setreg, 0, setreg.Length);
                Thread.Sleep(650);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    Debug.Print("Region is Open");
                    lcd.ShowMessage("Region is Open");
                }
                else
                {
                    Debug.Print("Region not set properly");
                    lcd.ShowMessage("Region not set properly");
                }
                UART.Write(antconfig, 0, antconfig.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("get antenna config ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("get antenna config not ok");
                    Thread.Sleep(500);
                }
                Thread.Sleep(500);
                UART.Write(readpower, 0, readpower.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("txpower set ok");
                }
                else
                {
                    lcd.ShowMessage("set txpower not ok");
                }
                Thread.Sleep(1000);
                lcd.ShowMessage("read_count for settxpower= " + read_count); Thread.Sleep(1000);
                for (int i = 0; i < read_count; i++)
                {
                    lcd.ShowMessage("read_count for set txpower[" + i + "] " + rx_data[i]);
                    Thread.Sleep(500);
                }
                UART.Write(clrtagbuf, 0, clrtagbuf.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if (ByteArrayCompare(rec_clrbuf, rx_data))
                {
                    Debug.Print("Cleared tag buffer");
                    lcd.ShowMessage("Cleared tag buffer");
                }
                else
                {
                    Debug.Print("Tag buffer not cleared");
                    lcd.ShowMessage("Tag buffer not cleared");
                }
                Thread.Sleep(500);
                lcd.ShowMessage("read_count for tagbuffer= " + read_count); Thread.Sleep(500);
                for (int i = 0; i < read_count; i++)
                {
                    lcd.ShowMessage("read_count for get prot[" + i + "] " + rx_data[i]);
                    Thread.Sleep(500);
                }
                UART.Write(setant1, 0, setant1.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if (ByteArrayCompare(rec_ant1, rx_data))
                {
                    Debug.Print("Antenna 1 is set to TXRX");
                    lcd.ShowMessage("Ant\na 1 is set to TXRX");
                }
                else
                {
                    Debug.Print("Antenna not set properly");
                    lcd.ShowMessage("Antenna not set properly");
                }
                Thread.Sleep(500);
                UART.Write(freqtable, 0, freqtable.Length);
                Thread.Sleep(600);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("set freq table 1 ok");
                    Thread.Sleep(100);
                }
                else
                {
                    lcd.ShowMessage("set freq table not ok");
                    Thread.Sleep(100);
                } Thread.Sleep(500);
                UART.Write(staticQ, 0, staticQ.Length);
                Thread.Sleep(600);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                lcd.ShowMessage("read_count static Q recv: " + read_count);
                Thread.Sleep(500);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    lcd.ShowMessage("set static,q=0 ok");
                    Thread.Sleep(500);
                }
                else
                {
                    lcd.ShowMessage("Set static, q=0 not ok");
                    Thread.Sleep(500);
                } 
                UART.Write(clrtagbuf, 0, clrtagbuf.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    Debug.Print("Cleared tag buffer");
                    lcd.ShowMessage("Cleared tag buffer");
                }
                else
                {
                    Debug.Print("Tag buffer not cleared");
                    lcd.ShowMessage("Tag buffer not cleared");
                } Thread.Sleep(500);
                Thread.Sleep(100); 
                UART.Write(setant1, 0, setant1.Length);
                Thread.Sleep(100);
                read_count = UART.Read(rx_data, 0, rx_data.Length);
                if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                {
                    Debug.Print("Antenna 1 is set to TXRX");
                    lcd.ShowMessage("Antenna 1 is set to TXRX");                }
                else
                {
                    Debug.Print("Antenna not set properly");
                    lcd.ShowMessage("Antenna not set properly");
                } Thread.Sleep(100);

                //start of do while for read
                do
                {
                    do
                    {
                        UART.Write(clrtagbuf, 0, clrtagbuf.Length);
                        Thread.Sleep(650);
                        read_count = UART.Read(rx_data, 0, rx_data.Length);
                        if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
                        {
                            Debug.Print("Cleared tag buffer");
                            lcd.ShowMessage("Cleared tag buffer");
                        }
                        else
                        {
                            Debug.Print("Tag buffer not cleared");
                            lcd.ShowMessage("Tag buffer not cleared");
                        }
                        Thread.Sleep(500);
                        byte[] readmult = new byte[] { 0xFF, 0x05, 0x22, 0x00, 0x00, 0x10, 0x00, 0x64, 0x1A, 0xEA }; //0x07d0 - 2secs,0x01F4 - 500msecs
                        UART.Write(readmult, 0, readmult.Length);
                        Thread.Sleep(200);//read buffer after indicated amount of time to read tags, hence sleep
                        read_count = UART.Read(rx_data, 0, rx_data.Length);
                        lcd.ShowMessage("read_count =" + read_count);
                          Thread.Sleep(500);
                        for (int x = 0; x < read_count; x++)
                        {
                            if (x == 3)
                            {
                                a = rx_data[x];
                            }
                            if (x == 4)
                            {
                                d = rx_data[x];
                            }
                            if (x == 11)
                            {
                                e = rx_data[x];
                            }
                            Debug.Print("Readmult Byte:" + rx_data[x]);
                            lcd.ShowMessage("Readmult Byte received:" + x + ": " + rx_data[x].ToString());
                            Thread.Sleep(1000);
                        }
                        lcd.ShowMessage("Number of tags read:" + e.ToString());
                        Thread.Sleep(500);
                    } while ((e == 0)||(read_count==7));
                        UART.Write(gettagdata, 0, gettagdata.Length);
                    Thread.Sleep(200);
                    read_count = UART.Read(rx_data, 0, rx_data.Length);
                    Array.Clear(datarray, 0, datarray.Length);
                    Array.Copy(rx_data, datarray, read_count);
                    if ((a == 0x00) && (d == 0x00))
                    {
                        Debug.Print("Received for read ok.");
                        lcd.ShowMessage("Received for read ok.");
                        Thread.Sleep(500);
                    }
                    byte[] rssibef = new byte[e];
                    byte[] rssiaft = new byte[e];
                    byte[] phasebef = new byte[e];
                    byte[] freqbef = new byte[3];
                    byte[] freqaft = new byte[3];
                    byte[] phaseaft = new byte[e];
                    int[] EPCflag = new int[e+1];
                    Array.Clear(rssibef, 0, rssibef.Length);
                    Array.Clear(phasebef, 0, phasebef.Length);
                    Array.Clear(freqbef, 0, freqbef.Length);
                   /* for (int x = 0; x < read_count; x++)
                    {
                        lcd.ShowMessage("DataArray: " + x + ": " + datarray[x].ToString());
                        Thread.Sleep(500);
                    }*/
                    int intc = 0, count = 0, count1 = 0;
                    int freqbef1 = new int(); int j = 0; 
                    int cnt = 9; int tagcnt = 0; int[] tagptr = new int[e + 1];
                    int m = 0; byte[] EPCs = new byte[150];
                    int size = 0;
                    tagptr[0] = cnt; intc++;  //int freqaft1 = 0;
                    freqbef[count1++] = datarray[(cnt + 3)];
                    freqbef[count1++] = datarray[(cnt + 4)];
                    freqbef[count1] = datarray[(cnt + 5)];
                    freqbef1 = freqbef[count1] + (freqbef[count1 - 1] << 8) + (freqbef[count1 - 2] << 16);
                    lcd.ShowMessage("----------"); Thread.Sleep(500);
                    int EPCont = 0;
                    do
                    {
                        m = 0;
                        if (datarray[cnt + 17] == 0x80)
                        {
                            //             lcd.ShowMessage("EPC length is 0x80 XD"); Thread.Sleep(500);
                            EPCflag[tagcnt] = 1;
                            size += 12;
                            do
                            {
                                EPCs[EPCont] = datarray[(cnt + m) + 20];
                                m++; EPCont++;
                            } while (m < 12);
                            cnt += 34;// Thread.Sleep(100);
                            tagptr[intc++] = cnt;
                        }
                        else if (datarray[cnt + 17] == 0x40)
                        {
                            //          lcd.ShowMessage("EPC length is 0x40"); Thread.Sleep(500);
                            size += 4;
                            do
                            {
                                EPCs[EPCont] = datarray[(cnt + m) + 20];
                                m++; EPCont++;
                            } while (m < 4);
                            cnt += 26;
                            EPCflag[tagcnt] = 2;
                            tagptr[intc++] = cnt;
                        }
                        tagcnt++;
                    } while (tagcnt < e);
                    cnt = tagcnt = 0; count = 0;
                    // int n = 0; 
                    /*   do
                       {
                           lcd.ShowMessage("EPCflag [" + n + "]: " + EPCflag[n]);
                           Thread.Sleep(1500);
                           n++;
                       } while (n < e); */
                    //  n = 0;
                    /*       do
                           {
                               lcd.ShowMessage("EPCs [" + n + "]: " + EPCs[n]); Thread.Sleep(100); n++;
                           } while (n < EPCont); */
                    tagcnt = 0; int index = 0;
                    // yung do-while sa baba naman, yung naglalagay ng RSSI at phase na nari-read sa respective array.
                    do
                    {
                        index = tagptr[tagcnt];
                        rssibef[count] = datarray[index + 1];
                        phasebef[count++] = datarray[index + 9];
                        tagcnt++;
                    } while (tagcnt < e); cnt = 0; int p = 0; tagcnt = 0; int ind = 0; j = 0;
                    // yung do-while sa baba, pang-print ng EPC, Freq, RSSI, phase. Ang ginawa ko, isang array lahat ng EPCs, Freq, RSSI, phase
                    // yung sa EPC kasi, may two cases lang ako naconsider na length ng EPC, dpende sa case yung print niyang length, ganun.. 
                    do
                    {
                        if (EPCflag[j] == 2)
                        {
                            while (cnt < 4)
                            {
                                lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                            } //p += 12;
                        }
                        if (EPCflag[j] == 1)
                        {
                            while (cnt < 12)
                            {
                                lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                            }// p += 4;
                        }
                        lcd.ShowMessage("Frequency:" + freqbef1 + " kHz"); Thread.Sleep(500);
                        lcd.ShowMessage("RSSI [" + j + "] -" + (256 - rssibef[j]));
                        Thread.Sleep(500);
                        lcd.ShowMessage("Phases [" + j + "] " + phasebef[j]);
                        Thread.Sleep(500);
                        tagcnt++; cnt = 0; j++;
                    } while (j < e); 
                    p = 0; intc = 0;
                    ind = maximum(rssibef);
                    while (intc < ind)
                    {
                        if (EPCflag[intc] == 1)
                        {
                            p =p+ 12;
                        }
                        if (EPCflag[intc] == 2)
                        {
                            p = p+ 4;
                        }
                        intc++;
                    } lcd.ShowMessage("p = " + p); Thread.Sleep(500);
                        // do
                        // {
                    intc = 0;
                    lcd.ShowMessage("intc = " + intc + " ind= " + ind); Thread.Sleep(500); 
                        if (EPCflag[ind] == 2)
                        {
                            while (cnt < 4)
                            {
                                lcd.ShowMessage("EPC: [" + ind + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                            } //p += 12;
                        }
                        if (EPCflag[ind] == 1)
                        {
                            while (cnt < 12)
                            {
                                lcd.ShowMessage("EPC: [" + ind + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                            }// p += 4;
                        }
                        lcd.ShowMessage("Frequency:" + freqbef1 + " kHz"); Thread.Sleep(500);
                        int rssiint = (int)rssibef[ind];
                        lcd.ShowMessage("HighestRSSI [" + ind + "] -" + (256 - rssiint));
                        Thread.Sleep(500);
                        lcd.ShowMessage("Phases [" + ind + "] " + phasebef[ind]);
                        Thread.Sleep(500);
                        tagcnt++; cnt = 0; j = 0;// j++;
                        //  } while (j < e);

                        rssiint = rssiint - 20;
                        double rssidoub = 0;
                        rssidoub = rssiint / (-10 * 4.4);
                        rssidoub = System.Math.Pow((double)10, rssidoub);
                        lcd.ShowMessage("Distance is " + rssidoub + "meters"); Thread.Sleep(500);
                    Array.Clear(datarray, 0, datarray.Length);
                    UART.Write(clrtagbuf, 0, clrtagbuf.Length);
                    Thread.Sleep(650);
                    read_count = UART.Read(rx_data, 0, rx_data.Length);
                    if (ByteArrayCompare(rec_clrbuf, rx_data))
                    {
                        Debug.Print("Cleared tag buffer");
                        lcd.ShowMessage("Cleared tag buffer");
                    }
                    else
                    {
                        Debug.Print("Tag buffer not cleared");
                        lcd.ShowMessage("Tag buffer not cleared");
                    }
                    Thread.Sleep(100); 
                    
                    /*UART.Write(readmult, 0, readmult.Length);
                    Thread.Sleep(200);//read buffer after indicated amount of time to read tags, hence sleep
                    read_count = UART.Read(rx_data, 0, rx_data.Length);
                    lcd.ShowMessage("read_count =" + read_count);
                    for (int x = 0; x < read_count; x++)
                    {
                        if (x == 3)
                        {
                            a = rx_data[x];
                        }
                        if (x == 4)
                        {
                            d = rx_data[x];
                        }
                        if (x == 11)
                        {
                            e = rx_data[x];
                        }
                        Debug.Print("Readmult Byte:" + rx_data[x]);
                        lcd.ShowMessage("Readmult Byte received:" + x + ": " + rx_data[x].ToString());
                        Thread.Sleep(1000);
                    }
                    lcd.ShowMessage("Number of tags read:" + e.ToString());
                    Thread.Sleep(1000);
                    UART.Write(gettagdata, 0, gettagdata.Length);
                    Thread.Sleep(200);
                    read_count = UART.Read(rx_data, 0, rx_data.Length);
                    lcd.ShowMessage("read_count gettagdata: " + read_count.ToString());
                    Thread.Sleep(1000);
                    datarray = new byte[rx_data.Length];
                    lcd.ShowMessage("rx_data.length =" + rx_data.Length);
                    Thread.Sleep(500);
                    Array.Clear(datarray, 0, datarray.Length);
                    Array.Copy(rx_data, datarray, read_count);
                    if ((a == 0x00) && (d == 0x00))
                    {
                        Debug.Print("Received for read ok.");
                        lcd.ShowMessage("Received for read ok.");
                        Thread.Sleep(500);
                    }
                    for (int x = 0; x < read_count; x++)
                    {
                        lcd.ShowMessage("DataArray: " + x + ": " + datarray[x].ToString());
                        Thread.Sleep(100);
                    }
                    Array.Clear(tagptr, 0, tagptr.Length);
                    intc = count = count1 = tagcnt= 0;
                    cnt = 9; 
                    tagptr[0] = cnt; intc++; freqbef1 = 0;
                    freqaft[count1++] = datarray[cnt + 3];
                    freqaft[count1++] = datarray[cnt + 4];
                    freqaft[count1] = datarray[cnt + 5];
                    freqaft1 = freqaft[count1] + (freqaft[count1 - 1] << 8) + (freqaft[count1 - 2] << 16);
                    while (tagcnt < (e - 1))
                    {
                        if (datarray[cnt + 17] == 0x80)
                        {
                            lcd.ShowMessage("EPC length is 0x80 XD");
                            cnt += 34; Thread.Sleep(1000);
                            tagptr[intc++] = cnt;
                        }
                        else if (datarray[cnt + 17] == 0x40)
                        {
                            lcd.ShowMessage("EPC length is 0x40");
                            cnt += 26; Thread.Sleep(1000);
                            tagptr[intc++] = cnt;
                        }
                        tagcnt++;
                    }
                    cnt = tagcnt = 0; count = 0;
                    do
                    {
                        lcd.ShowMessage("Tagptr [" + tagcnt + "] " + tagptr[tagcnt]); Thread.Sleep(1500);
                        lcd.ShowMessage("Tag read count[" + tagptr[tagcnt] + "]: " + datarray[tagptr[tagcnt]]);
                        Thread.Sleep(1500);
                        tagcnt++;
                    } while (tagcnt < e); n = 0;
                    do
                    {
                        lcd.ShowMessage("TAGPTR [" + n + "] " + tagptr[n]); Thread.Sleep(1000); n++;
                    } while (n < e);
                    tagcnt = 0; index = 0;
                    do
                    {
                        index = tagptr[tagcnt];
                        rssiaft[count] = datarray[index + 1];
                        phaseaft[count++] = datarray[index + 9];
                        tagcnt++;
                    } while (tagcnt < tagptr.Length); j = 0;
                    do
                    {
                        lcd.ShowMessage("Frequency:" + freqaft1); Thread.Sleep(1000);
                        lcd.ShowMessage("RSSI [" + j + "] " + rssiaft[j]);
                        Thread.Sleep(1000);
                        lcd.ShowMessage("Phases [" + j + "] " + phaseaft[j]);
                        Thread.Sleep(1000);
                        j++;
                    } while (j < e); 
                  //  UART.Close();
                while (true)
                    {
                    }*/
                } while (true);
             //   UART.Close();
            }
        }
    }
}


You have a lot of “USING” statements that are probably redundant, but that shouldn’t really cause a concern. The thing I would first do is create a new project, copy over your CODE only and then add in the minimum number of references to remove the “can’t find” issues that come up. I suspect you have something that’s causing the Cobra to max-out the application space (although that’s a huge task). Can you also triple check you have the firmware version that matches your SDK installed version?

Final thought, can you just deploy the base Cobra app that blinks an LED and make sure that deploys correctly?

@ Brett - hi, I really dont know what to do now. I did what you have said, I redo a project, pasted the code, and added references one-by-one, however, an error in font occured. It says something regarding the resources, I did this:

Font font = FEZ_Cobra_Window_Application11.Resources.GetFont(FEZ_Cobra_Window_Application11.Resources.FontResources.small);

and it was okay.
However, FEZ still says “waiting for debug commands”.

my sdk version is 1.6.0. I cant test for the LED, for i dont have LED atm nor multimeter. please help me.

@ Brett - If gadgeteer app has something to do with programstarted (as main program) and about handlers, I would like to ask why the serialport class was affected by the gadgeteer?

The reason I would think is because you might have used the Gadgeteer code but the proper Gadgeteer core initialisation hadn’t happened and the dispatcher wasn’t working. It was a theory based on the information we had.

“Waiting for debug commands” means it’s not getting to code.

Please deploy a test application that only flashes a LED. You don’t need to actually flash a LED, but at least add debug.print statements so you can track the program flow and that it deploys.

What is the netmf version you’re using not the “raw” SDK version. We need to know are you running 4.1.8.0 or something on your PC and on your device (go to mfdeploy, show what “Device Capabilities” shows)

your UART code is pretty messy and looks very specific to the RFID module you’re using. I don’t have anything suitable to test, and I won’t be near my Cobra for the next 12 hours to test out your code, but I’ll try to do so when I get home tonight

what is your readV8.cs file responsible for? And where is your Glide XML?

@ Brett - Hi brett! sorry for replying very late… however, i still need to get this done. hope you can still help me…


using System.Threading;
using System;
using System.Text;
using Microsoft.SPOT;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using System.IO.Ports;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Controls;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace Test
{
    public class Program : Microsoft.SPOT.Application
    {

        static GHIElectronics.NETMF.Glide.Display.Window window;
        static bool ByteArrayCompare(byte[] a1, byte[] a2)
        {
            //if (a1.Length != a2.Length)
            //    return false;

            for (int i = 0; i < a1.Length; i++)
                if (a1[i] != a2[i])
                    return false;

            return true;
        }
        static int maximum(byte[] numbers)
        {
            byte m = numbers[0];
            int index = 0; int maxint = 0;
            for (index = 0; index < numbers.Length; index++)
                if (m < numbers[index])
                {
                    m = numbers[index];
                    maxint = index;
                }
            return maxint;
        }
        // This will hold the main window.

        static int maxim(byte[] numbers)
        {
            byte m = numbers[0];
            int index = 0; int maxint = 0;
            for (index = 0; index < numbers.Length; index++)
                if (m < numbers[index])
                {
                    m = numbers[index];
                    maxint = index;
                }
            return maxint;
        }
        private static string ByteArrayToHex(byte[] barray)
        {
            char[] c = new char[barray.Length * 2];
            byte b;
            for (int i = 0; i < barray.Length; ++i)
            {
                b = ((byte)(barray[i] >> 4));
                c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30);
                b = ((byte)(barray[i] & 0xF));
                c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30);
            }

            return new string(c);
        }

        public static void Main()
        {
            // Load the window
            string xml;
            xml = "<Glide Version=\"" + Glide.Version + "\">";
            xml += "<Window Name=\"window\" Width=\"320\" Height=\"240\" BackColor=\"dce3e7\">";
            xml += "<Button Name=\"init\" X=\"240\" Y=\"8\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"init\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<Button Name=\"btn\" X=\"4\" Y=\"202\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"Read\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<Button Name=\"stopbtn\" X=\"236\" Y=\"202\" Width=\"80\" Height=\"32\" Alpha=\"255\" Text=\"Clear\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "<TextBlock Name=\"text0\" X=\"5\" Y=\"9\" Width=\"200\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"Frequency\" X=\"10\" Y=\"102\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Frequency:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"EPC\" X=\"10\" Y=\"73\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"EPC:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"RSSI\" X=\"10\" Y=\"130\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"RSSI(-):\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"Phase\" X=\"9\" Y=\"158\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Phase:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text1\" X=\"105\" Y=\"71\" Width=\"200\" Height=\"32\" Alpha=\"255\" Text=\" \" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"2\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text2\" X=\"107\" Y=\"103\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text3\" X=\"106\" Y=\"130\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text4\" X=\"106\" Y=\"157\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"kHz\" X=\"215\" Y=\"104\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"kHz\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"dBm\" X=\"215\" Y=\"130\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"dBm\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"deg\" X=\"215\" Y=\"158\" Width=\"50\" Height=\"32\" Alpha=\"255\" Text=\"deg\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"4\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBlock Name=\"tagsdet\" X=\"89\" Y=\"203\" Width=\"100\" Height=\"32\" Alpha=\"255\" Text=\"Tags Detected:\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"0\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
            xml += "<TextBox Name=\"text5\" X=\"199\" Y=\"200\" Width=\"35\" Height=\"32\" Alpha=\"255\" Text=\"\" TextAlign=\"Left\" TextVerticalAlign=\"Top\" Font=\"0\" FontColor=\"0\" BackColor=\"000000\" ShowBackColor=\"False\"/>";
        //    xml += "<Image Name=\"img1\" X=\"2\" Y=\"6\" Width=\"317\" Height=\"35\" Alpha=\"255\"/>";
            //   xml += "<Button Name=\"btn\" X=\"100\" Y=\"104\" Width=\"120\" Height=\"32\" Alpha=\"255\" Text=\"Read\" Font=\"4\" FontColor=\"000000\" DisabledFontColor=\"808080\" TintColor=\"000000\" TintAmount=\"0\"/>";
            xml += "</Window>";
            xml += "</Glide>";

        //      SerialPort UART = new SerialPort("COM1", 115200);
        //      UART.Handshake = Handshake.None;

            // Resize any loaded Window to the LCD's size.
            Glide.FitToScreen = true;

            // Load the Window XML string.
            window = GlideLoader.LoadWindow(xml);

            // Activate touch
            GlideTouch.Initialize();

            // Initialize the window.
            InitWin();

            // Assigning a window to MainWindow flushes it to the screen.
            // This also starts event handling on the window.
            Glide.MainWindow = window;

            Thread.Sleep(-1);
        }

        static void InitWin()
        {
            TextBlock freq = (TextBlock)window.GetChildByName("Frequency");
            TextBlock epc = (TextBlock)window.GetChildByName("EPC");
            TextBlock rssi = (TextBlock)window.GetChildByName("RSSI");
            TextBlock phase = (TextBlock)window.GetChildByName("Phase");
            TextBlock tagsdet = (TextBlock)window.GetChildByName("tagsdet");
            TextBlock kHz = (TextBlock)window.GetChildByName("kHz");
            TextBlock dBm = (TextBlock)window.GetChildByName("dBm");
            TextBlock deg = (TextBlock)window.GetChildByName("deg");
            TextBlock text = (TextBlock)window.GetChildByName("text0");

            // Get the Button
            Button btn = (Button)window.GetChildByName("btn");
            Button stopbtn = (Button)window.GetChildByName("stopbtn");
            Button init = (Button)window.GetChildByName("init");
            // Listen for tap events
            btn.TapEvent += new OnTap(btn_TapEvent);
            stopbtn.TapEvent += new OnTap(stopbtn_TapEvent);
            init.TapEvent += new OnTap(init_TapEvent);

        }
        static void init_TapEvent(object sender)
        {
           // SerialPort UART = new SerialPort("COM1", 115200);
           // UART.Handshake = Handshake.None;
          //  int read_count = 0;
           /* var rx_data = new byte[1000];
            byte[] datarray = new byte[rx_data.Length];
            byte[] b = { 0xFF, 0x00, 0x68, 0x1D, 0x67 };
            byte[] gp_on;
            byte[] boot = { 0xFF, 0x00, 0x03, 0x1D, 0x0C };
            byte[] getprog = { 0xFF, 0x00, 0x0C, 0x1D, 0x03 };
            byte[] currprog = new byte[] { 0xFF, 0x01, 0x68, 0x00, 0x00, 0x00, 0xA4, 0xBF };
            byte[] gpset = new byte[] { 0xFF, 0x04, 0x96, 0x01, 0x03, 0x01, 0x00, 0x0C, 0x2A };
            byte[] testgp = { 0xFF, 0x00, 0x96, 0x00, 0x00, 0x67, 0xBF };
            gp_on = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x01, 0x03, 0xDC };
            byte[] gp_off = new byte[] { 0xFF, 0x02, 0x96, 0x02, 0x00, 0x03, 0xDB };
            byte[] setprot = new byte[] { 0xFF, 0x02, 0x93, 0x00, 0x05, 0x51, 0x7D };
            byte[] getprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
            byte[] currprot = new byte[] { 0xFF, 0x02, 0x63, 0x00, 0x00, 0x00, 0x05, 0x21, 0x46 };
            byte[] setreg = new byte[] { 0xFF, 0x01, 0x97, 0xFF, 0x4B, 0x42 }; //North Am Region(FF0197014BBC), OPEN( FF  01  97  FF  4B  42)
            //        byte[] rec_reg = new byte[] { 0xFF, 0x00, 0x97, 0x00, 0x00, 0x77, 0x9E };
            byte[] setant1 = new byte[] { 0xFF, 0x02, 0x91, 0x01, 0x01, 0x70, 0x3B };
            byte[] rec_ant1 = new byte[] { 0xFF, 0x00, 0x91, 0x00, 0x00, 0x17, 0x58 };
            byte[] setEPClen = new byte[] { 0xFF, 0x03, 0x9A, 0x01, 0x02, 0x01, 0xAD, 0x5C };
            byte[] rec_EPClen = new byte[] { 0xFF, 0x00, 0x9A, 0x00, 0x00, 0xA6, 0x33 };
            byte[] clrtagbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x1D, 0x25 };
            byte[] rec_clrbuf = new byte[] { 0xFF, 0x00, 0x2A, 0x00, 0x00, 0x01, 0xE8 };
            byte[] gettagdata = new byte[] { 0xFF, 0x03, 0x29, 0x01, 0xFF, 0x00, 0x1B, 0x03 };
            byte[] setbaud = new byte[] { 0xFF, 0x04, 0x06, 0x00, 0x01, 0xC2, 0x00, 0xA4, 0x60 };
            byte[] readconfig1 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x12, 0x2E, 0x5C };
            byte[] antconfig = new byte[] { 0xFF, 0x01, 0x61, 0x05, 0xBD, 0xB8 };
            byte[] readconfig2 = new byte[] { 0xFF, 0x02, 0x6A, 0x01, 0x0C, 0x2E, 0x42 };
            byte[] gettagprot = new byte[] { 0xFF, 0x00, 0x63, 0x1D, 0x6C };
            byte[] freqtable = new byte[] { 0xFF, 0x6C, 0x95, 0x00, 0x0D, 0xC3, 0x70, 0x00, 0x0D, 0xC7, 0x58, 0x00, 0x0D, 0xCB, 0x40, 0x00, 0x0D, 0xCF, 0x28, 0x00, 0x0D, 0xD3, 0x10, 0x00, 0x0D, 0xD6, 0xF8, 0x00, 0x0D, 0xDA, 0xE0, 0x00, 0x0D, 0xDE, 0xC8, 0x00, 0x0D, 0xE2, 0xB0, 0x00, 0x0D, 0xE6, 0x98, 0x00, 0x0D, 0xEA, 0x80, 0x00, 0x0D, 0xEE, 0x68, 0x00, 0x0D, 0xF2, 0x50, 0x00, 0x0D, 0xF6, 0x38, 0x00, 0x0D, 0xFA, 0x20, 0x00, 0x0D, 0xFE, 0x08, 0x00, 0x0E, 0x01, 0xF0, 0x00, 0x0E, 0x05, 0xD8, 0x00, 0x0E, 0x09, 0xC0, 0x00, 0x0E, 0x0D, 0xA8, 0x00, 0x0E, 0x11, 0x90, 0x00, 0x0E, 0x15, 0x78, 0x00, 0x0E, 0x19, 0x60, 0x00, 0x0E, 0x1D, 0x48, 0x00, 0x0E, 0x21, 0x30, 0x00, 0x0E, 0x25, 0x18, 0x00, 0x0E, 0x29, 0x00, 0xD0, 0x50 };
            byte[] staticQ = new byte[] { 0xFF, 0x04, 0x9B, 0x05, 0x12, 0x01, 0x00, 0x80, 0xAF };
            byte[] readpower = new byte[] { 0xFF, 0x02, 0x92, 0x06, 0xA4, 0x47, 0xFD };*/
            TextBlock text = (TextBlock)window.GetChildByName("text0");
           /* UART.ReadTimeout = 0;
            if (UART.IsOpen)
            {
                text.Text = "COM Port open!";
                text.Invalidate();
            } */

         /*   UART.Write(boot, 0, boot.Length);
            Thread.Sleep(700);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0))
            {
                text.Text = "Initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "firmware not ok";
                text.Invalidate();
            }
            UART.Write(getprog, 0, getprog.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get current program not ok";
                text.Invalidate();
            }
            UART.Write(b, 0, b.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if (ByteArrayCompare(currprog, rx_data))
            {
                text.Text = "initializing";
                text.Invalidate();
                //       lcd.ShowMessage("Full Power Mode");
                Thread.Sleep(200);
            }
            UART.Write(setbaud, 0, setbaud.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "baud set ok";
                text.Invalidate();
            }
            else
            {
                text.Text = "baudset not ok";
                text.Invalidate();
            }
            UART.Write(readconfig1, 0, readconfig1.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {

                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "read config 1 not ok";
                text.Invalidate();
            }
            UART.Write(antconfig, 0, antconfig.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get antenna config not ok";
                text.Invalidate();
            }
            UART.Write(readconfig2, 0, readconfig2.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get read config 2 not ok";
                text.Invalidate();
            }
            UART.Write(gettagprot, 0, gettagprot.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get current tag protocol not ok";
                text.Invalidate();
            }
            UART.Write(gpset, 0, gpset.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if (ByteArrayCompare(testgp, rx_data))
            {
                text.Text = "initializing";
                text.Invalidate();
                //  lcd.ShowMessage("Correct GPSettings");
            }
            else
            {
                text.Text = "Incorrect GPsettings";
                text.Invalidate();
            }
            UART.Write(setprot, 0, setprot.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            Thread.Sleep(500);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "set tag protocol not ok";
                text.Invalidate();
            }
            UART.Write(getprot, 0, getprot.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
                //  lcd.ShowMessage("TagProt is Gen2");
            }
            else
            {
                text.Text = "Incorrect TagProt settings";
                text.Invalidate();
            }
            Thread.Sleep(500);
            UART.Write(setreg, 0, setreg.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
                //   lcd.ShowMessage("Region is Open");
            }
            else
            {
                text.Text = "region not set properly";
                text.Invalidate();
            }
            UART.Write(antconfig, 0, antconfig.Length);
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "get antenna config not ok";
                text.Invalidate();
            }
            UART.Write(clrtagbuf, 0, clrtagbuf.Length);
            Thread.Sleep(650);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if ((rx_data[3] == 0x00) && (rx_data[4] == 0x00))
            {
                text.Text = "initializing";
                text.Invalidate();
            }
            else
            {
                text.Text = "tag buff not cleared";
                text.Invalidate();
            } */
            text.Text = "initializing";
            text.Invalidate();
        }
        static void stopbtn_TapEvent(object sender)
        {
            Button btn = (Button)window.GetChildByName("btn");
            //    Button stopbtn = (Button)window.GetChildByName("stopbtn");
            TextBox text1 = (TextBox)window.GetChildByName("text1");
            TextBox text2 = (TextBox)window.GetChildByName("text2");
            TextBox text3 = (TextBox)window.GetChildByName("text3");
            TextBox text4 = (TextBox)window.GetChildByName("text4");
            TextBox text5 = (TextBox)window.GetChildByName("text5");
            text1.Text = " ";
            text1.Invalidate();
            text2.Text = " ";
            text2.Invalidate();
            text3.Text = " ";
            text3.Invalidate();
            text4.Text = " ";
            text4.Invalidate();
            text5.Text = " ";
            text5.Invalidate();
            InitWin();
            //    btn.TapEvent += new OnTap(btn_TapEvent);
            //        stopbtn.TapEvent += new OnTap(stopbtn_TapEvent);
        }

        // Handle tap events
        static void btn_TapEvent(object sender)
        {
          //\  byte e = new byte();

            TextBox text1 = (TextBox)window.GetChildByName("text1");
            TextBox text2 = (TextBox)window.GetChildByName("text2");
            TextBox text3 = (TextBox)window.GetChildByName("text3");
            TextBox text4 = (TextBox)window.GetChildByName("text4");
            TextBox text5 = (TextBox)window.GetChildByName("text5");
                    byte[] datarray ={   0xFF,  0x40, 0x29, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x02,  0x01, 0xD8,  0x11, 0x0E, 0x16,
    0x72, 0x00, 0x00, 0x00, 0x2D, 0x00, 0x27, 0x05, 0x00, 0x00, 0x0E, 0x00, 0x40, 0x10, 0x00, 0x24,
    0x00, 0x13,  0x98, 0x42, 0x45, 0x03, 0xE8, 0x11, 0x0E, 0x16, 0x72, 0x00,
    0x00,  0x00,  0x21, 0x00, 0x35, 0x05,0x00, 0x00, 0x0E, 0x00, 0x80, 0x30, 0x00, 0x30, 0x08, 0x33,
    0xB2,  0xDD,  0xD9, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x39, 0xBB,  0x5B,  0xEE};
            int e = datarray[8];
            int freqbef1 = 0;
            byte[] freqbef = new byte[3];
            byte[] rssibef = new byte[e];
            byte[] phasebef = new byte[e];
            int[] EPCflag = new int[e];
            byte[] EPCs = new byte[150];
            byte[] EPC = new byte[20];
                int intc = 0, count = 0, count1 = 0;
                int cnt = 9; int tagcnt = 0; int[] tagptr = new int[e + 1];
                int m = 0;
                int size = 0;
                tagptr[0] = cnt; intc++;  //int freqaft1 = 0;
                freqbef[count1++] = datarray[cnt + 3];
                freqbef[count1++] = datarray[cnt + 4];
                freqbef[count1] = datarray[cnt + 5];
                freqbef1 = freqbef[count1] + (freqbef[count1 - 1] << 8) + (freqbef[count1 - 2] << 16);
                int EPCont = 0;
                do
                {
                    m = 0;
                    if (datarray[cnt + 17] == 0x80)
                    {
                        //             lcd.ShowMessage("EPC length is 0x80 XD"); Thread.Sleep(500);
                        EPCflag[tagcnt] = 1;
                        size += 12;
                        do
                        {
                            EPCs[EPCont] = datarray[(cnt + m) + 20];
                            m++; EPCont++;
                        } while (m < 12);
                        cnt += 34;// Thread.Sleep(100);
                        tagptr[intc++] = cnt;
                    }
                    else if (datarray[cnt + 17] == 0x40)
                    {
                        //          lcd.ShowMessage("EPC length is 0x40"); Thread.Sleep(500);
                        size += 4;
                        do
                        {
                            EPCs[EPCont] = datarray[(cnt + m) + 20];
                            m++; EPCont++;
                        } while (m < 4);
                        cnt += 26;
                        EPCflag[tagcnt] = 2;
                        tagptr[intc++] = cnt;
                    }
                    tagcnt++;
                } while (tagcnt < e);
                cnt = tagcnt = 0; count = 0;
                // int n = 0; 
                /*   do
                   {
                       lcd.ShowMessage("EPCflag [" + n + "]: " + EPCflag[n]);
                       Thread.Sleep(1500);
                       n++;
                   } while (n < e); */
                //  n = 0;
                /*       do
                       {
                           lcd.ShowMessage("EPCs [" + n + "]: " + EPCs[n]); Thread.Sleep(100); n++;
                       } while (n < EPCont); */
                tagcnt = 0; int index = 0; 
                // yung do-while sa baba naman, yung naglalagay ng RSSI at phase na nari-read sa respective array.
                do
                {
                    index = tagptr[tagcnt];
                    rssibef[count] = datarray[index + 1];
                    phasebef[count++] = datarray[index + 9];
                    tagcnt++;
                } while (tagcnt < e); int j = 0; cnt = 0; int p = 0; tagcnt = 0;
                // yung do-while sa baba, pang-print ng EPC, Freq, RSSI, phase. Ang ginawa ko, isang array lahat ng EPCs, Freq, RSSI, phase
                // yung sa EPC kasi, may two cases lang ako naconsider na length ng EPC, dpende sa case yung print niyang length, ganun.. 
                index = 0;
                do
                {
                    if (EPCflag[j] == 2)
                    {
                        while (cnt < 4)
                        {
                            Array.Copy(EPCs, p, EPC, 0, 4); cnt = 4;
                    //        lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                        } //p += 12; 
                        p = p+ 4;
                    }
                    if (EPCflag[j] == 1)
                    {
                        while (cnt < 12)
                        {
                            Array.Copy(EPCs, p++, EPC, 0, 12); cnt = 12;
                    //        lcd.ShowMessage("EPC: [" + tagcnt + "] " + EPCs[p++]); Thread.Sleep(1000); cnt++;
                        }// p += 4;
                        p = p + 12;
                    }
                    text1.Text = ByteArrayToHex(EPC);
                    text1.Invalidate();
                    text2.Text = freqbef1.ToString();
                    text2.Invalidate();
         //           lcd.ShowMessage("Frequency:" + freqbef1); Thread.Sleep(500);
                    text3.Text = (256 - rssibef[j]).ToString();
                    text3.Invalidate();
         //           lcd.ShowMessage("RSSI [" + j + "] -" + (256 - rssibef[j]));
         //           Thread.Sleep(500);
                    text4.Text = phasebef[j].ToString();
                    text4.Invalidate();
                    text5.Text = e.ToString();
                    text5.Invalidate();
         //           lcd.ShowMessage("Phases [" + j + "] " + phasebef[j]);
                    Thread.Sleep(500);
                    j++; tagcnt++; cnt = 0;
                } while (j < e);
              
        }
    }
}