CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2014
    Posts
    9

    Creating a loop around a checkedbox

    Good day to all,
    Please i need your support on how to modify the program below so that it can read the data(temperature value) sent from the microcontroller as long as the Checkbox is checked.At the moment the function
    Code:
    myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
    reads only the first temperature value sent from the microcontroller,the subsequent temperatue values are not being updated even though the Checkbox is still checked.I would be very glad if somebody could put me through on how to create a Loop as Long as the Checkbox is checked.Thank you for the usual Support.Best regards.




    Code:
    
     private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                UInt32 numBytesRead = 0;
                UInt32 numBytesToRead = 1;
                byte[] readData = new byte[10];
    
               ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
            
               label11.Text = Convert.ToString(readData[0]);
         
            }

  2. #2
    Join Date
    May 2002
    Posts
    511

    Re: Creating a loop around a checkedbox

    Something like this will read until there is no more data to read.

    If you are looking at toggling on and off the actual read process you'll probably need to fire off a worker thread that checks some static Boolean variable in your class that holds the state of the checkbox as to if it is checked or not. The thread would then continually try to read from the device until the checkbox is unchecked. Once unchecked you could kill the thread.

    Code:
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
          if (checkbox1.Checked == true)
          {   
                UInt32 numBytesRead = 1;
                UInt32 numBytesToRead = 1;
                byte[] readData = new byte[10];
    
               // this can result in an infinite loop if the device always has data
               while (numBytesRead > 0)
               {
                     ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
            
                     if (numBytesRead == 1)
                     {
                              label11.Text = Convert.ToString(readData[0]);
                     }
               }
          }
     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured