Timer and keypad using in fez spider

i am using timer to send and receive data from hardware . i am using thread. bt when i attach keypad my timer work slowly. how to solve it?

How often do you have your timer set to fire? Show some code.

Biren,

please show us a full set of working code that we can reproduce this with. Please make this the minimum code we need (none of your other content) to show the behaviour. Can you also tell us exactly why you think the timer is “going slow”?

Define what you mean by " timer work slowly". At this setting, the timer should only fire once a second. Seems slow to me if you’re trying to capture keypad input. If that’s what you’re doing, I’m not sure I’d use a timer for that anyway.

@ ianlee74 - void ProgramStarted()
{

        // mainwindow = display.WPFWindow;

        /*******************************************************************************************
        Modules added in the Program.gadgeteer designer view are used by typing 
        their name followed by a period, e.g.  button.  or  camera.
        
        Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
            button.ButtonPressed +=<tab><tab>
        
        If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
            GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
            timer.Tick +=<tab><tab>
            timer.Start();
        *******************************************************************************************/
        UART = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
        UART.Open();
        showglidedisplay();
            // showdisplaydata();
        thrTimer = new Thread(new ThreadStart(timer_Tick));
        thrTimer.Priority = ThreadPriority.Normal;
        thrTimer.Start();
        
        showKeyboard();
        thrKeyboard = new Thread(new ThreadStart(key_hit));
        thrKeyboard.Priority = ThreadPriority.Normal;
        thrKeyboard.Start();

   }

    void timer_Tick()
    {

        while (true)
        {
            //if (keyboard == false)
            //{
                view.Clear();
                view.DrawRectangle(GT.Color.Green, 2, 0, 0, 310, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                //if (read_count == 0)
                //{
                //    view.DrawText("No Input Data Availabel", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Red, 60, 25);
                //}
                if (read_count != 0)
                {
                    drawgraph();
                }
                int buffLen = 0;
                buffLen = UART.BytesToRead;
                rx_byte = new byte[buffLen];

                read_count = UART.Read(rx_byte, 0, buffLen);


                for (j = 0; j <= buffLen - 1; j++)
                {
                    // ReadByte = Convert.ToChar(rx_byte[j]);
                    ReadByte = rx_byte[j];
                    if (start == false)
                    {
                        if (ReadByte == START_BYTE)
                        {
                            dt[count] = ReadByte;
                            //dt[count] = ReadByte;
                            count++;
                            start = true;
                        }
                    }
                    else
                    {
                        dt[count] = ReadByte;
                        count++;
                    }

                    if (count == g_PACKET_LENGTH)
                    {
                        long chkSum;
                        long finalChkSum;
                        chkSum = 0;
                        finalChkSum = 0;

                        for (i = 0; i <= g_PACKET_LENGTH - 2; i++)
                        {
                            chkSum = finalChkSum + dt[i];
                            if (chkSum > 255)
                            {
                                finalChkSum = chkSum - 256;
                            }
                            else
                            {
                                finalChkSum = chkSum;
                            }
                        }

                        if (dt[1] == 0)
                        {
                            if (finalChkSum + dt[g_PACKET_LENGTH - 1] == 256)
                            {
                                if (m_status != int.MaxValue)
                                {
                                    m_status++;
                                }
                                else
                                {
                                    m_status = 0;
                                }
                                SetupUI();
                                count = 0;
                                start = false;
                            }
                            else
                            {
                                count = 0;
                                start = false;
                            }
                        }

                        else
                        {
                            count = 0;
                            start = false;
                        }

                       

                    }



                }
                
            //}
        }
    }

@ ianlee74 - i am using two threads in a program bt my timer thread not execute every second. it takes more time than second how to solve

@ Brett - ianlee74 - void ProgramStarted()
{

// mainwindow = display.WPFWindow;

/*******************************************************************************************
Modules added in the Program.gadgeteer designer view are used by typing
their name followed by a period, e.g. button. or camera.

Many modules generate useful events. Type += to add a handler to an event, e.g.:
button.ButtonPressed +=

If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
timer.Tick +=
timer.Start();
*******************************************************************************************/
UART = new SerialPort(“COM1”, 19200, Parity.None, 8, StopBits.One);
UART.Open();
showglidedisplay();
// showdisplaydata();
thrTimer = new Thread(new ThreadStart(timer_Tick));
thrTimer.Priority = ThreadPriority.Normal;
thrTimer.Start();

showKeyboard();
thrKeyboard = new Thread(new ThreadStart(key_hit));
thrKeyboard.Priority = ThreadPriority.Normal;
thrKeyboard.Start();

}

void timer_Tick()
{

while (true)
{
//if (keyboard == false)
//{
view.Clear();
view.DrawRectangle(GT.Color.Green, 2, 0, 0, 310, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0);
//if (read_count == 0)
//{
// view.DrawText(“No Input Data Availabel”, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.Red, 60, 25);
//}
if (read_count != 0)
{
drawgraph();
}
int buffLen = 0;
buffLen = UART.BytesToRead;
rx_byte = new byte[buffLen];

read_count = UART.Read(rx_byte, 0, buffLen);

for (j = 0; j <= buffLen - 1; j++)
{
// ReadByte = Convert.ToChar(rx_byte[j]);
ReadByte = rx_byte[j];
if (start == false)
{
if (ReadByte == START_BYTE)
{
dt[count] = ReadByte;
//dt[count] = ReadByte;
count++;
start = true;
}
}
else
{
dt[count] = ReadByte;
count++;
}

if (count == g_PACKET_LENGTH)
{
long chkSum;
long finalChkSum;
chkSum = 0;
finalChkSum = 0;

for (i = 0; i <= g_PACKET_LENGTH - 2; i++)
{
chkSum = finalChkSum + dt[i];
if (chkSum > 255)
{
finalChkSum = chkSum - 256;
}
else
{
finalChkSum = chkSum;
}
}

if (dt[1] == 0)
{
if (finalChkSum + dt[g_PACKET_LENGTH - 1] == 256)
{
if (m_status != int.MaxValue)
{
m_status++;
}
else
{
m_status = 0;
}
SetupUI();
count = 0;
start = false;
}
else
{
count = 0;
start = false;
}
}

else
{
count = 0;
start = false;
}

}

}

//}
}
}

OK, so lets go back to first principles.

Minimise your code to prove that the timer is not running, or at least to understand what other portion of your code is taking a long time to execute and that makes your timer not run. For me, the simplest test is to have a timer that runs and increments a counter, and updates it on your display.

For a start, I’d get rid of your serial input in your timer.

read_count = UART.Read(rx_byte, 0, buffLen);

In fact, none of the code you have in that block makes a lot of sense to do within a timer…

…so lets go further back, and ask what are the outcomes you want to achieve in your app? Tell us all the things you want to happen, all the things you expect to have connected, all the interactions you want the app and the user to have, and how you see it all working. Then we can start to help unravel it.

Also, you really need to use the CODE tags. When you paste code in, go and select it all and hit the “101010” icon and it will wrap it with a tag and format it nicely. You can go back and edit one of your old posts and do this process to see. You also should post your keyboard thread code, and use the code tags :wink:

@ Brett - thanks