Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more: , +
First of all, I'm pretty new to C++.

I'm writing a program to communicate with a device via the serial port.

Radio buttons are used to toggle the mode and the program is supposed to execute a loop until a radio button is changed. As soon as the button is pressed to toggle onto Pmode, the program crashes and nothing can be pressed.

I've been looking online and people with similar problems have suggested using Invoking but I don't really understand how to use this?

Here's the relevant bit of code: thanks for any advice

private: System::Void rbCMode_CheckedChanged(System::Object^  sender, System::EventArgs^  e)
{
  if(rbCMode->Checked)
  {
    //set to command mode
    String^ message = "Set_Mode_C";
    mySerialPort->WriteLine(String::Format("{0}", message));

    //wait for OK confirmation
    OKCheck();
  }
  else //Pmode must be checked
  {
    //set to polling mode mode
    String^ message = "Set_Mode_P";
    mySerialPort->WriteLine(String::Format("{0}", message));
    // wait for OK confirmation
    OKCheck();
  }
             
  while(rbPMode->Checked == true)
  {   
    ReadAngles(xangle, yangle, x_angle_text, y_angle_text); //read the angles
    txtXDisplay->Text=x_angle_text; //update displays
    txtYDisplay->Text=y_angle_text;
  }
}

 

static void ReadAngles(double^ %xangle, double^ %yangle, String^ %x_angle_text, String^ %y_angle_text)
{
  //read from the buffer
  //check the number of bytes in buffer, dont read unless 19 bytes
  int databytes = mySerialPort->BytesToRead;
  while (databytes < 19) 
  {
    databytes = mySerialPort->BytesToRead;
  }

  //if the received string is "OK" then reject
  String^ buffer = mySerialPort->ReadLine();
  while (buffer->Length <= 5)
  {
    buffer = mySerialPort->ReadLine();
  }

  //split string into correct parts and convert to double
  x_angle_text = buffer->Substring(2,6);
  y_angle_text = buffer->Substring(11,6);
  xangle = System::Convert::ToDouble(x_angle_text);
  yangle = System::Convert::ToDouble(y_angle_text);
  return;
}
Posted
Updated 11-Jul-11 0:37am
v2
Comments
Joan M 11-Jul-11 6:38am    
Added code blocks, arranged a little bit the code and updated a couple of statements...

1 solution

You are never returning from the event handler. So it's not really a crash that happens, but rather a hangup because the GUI can no longer respond to events. As soon as you the enter the rbCMode checkboxe's event handler you are on the GUI thread. As long as the event handler does not return no other GUI events can take place. You need to start the part from the while loop in a separate thread. Be carefull though that you can't access GUI components directly from a non-GUI thread. You'll have to use Invoke for that.

Cheers!

—MRB
 
Share this answer
 
v2
Comments
ender4g 11-Jul-11 7:20am    
Thanks for your answer! I think I understand what you mean, but I don't really understand how to use "Invoke", and I can't find any tutorials or help online. Is there anything you can suggest?
Thanks!
Manfred Rudolf Bihy 11-Jul-11 7:40am    
Look here for an example: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx#Y211
You should learn how to use search engines. I just googled: MSDN Control Invoke
ender4g 11-Jul-11 7:44am    
I found this but I didn't really understand what the code was doing, that's why I was looking for a tutorial.
Thanks for your help, I'll keep looking.
Manfred Rudolf Bihy 11-Jul-11 8:58am    
Unaccepting a solution that has been answered and worked on by me is not well received by me. You have all the information you need.
ender4g 11-Jul-11 9:16am    
I sincerely apologise, I was simply hoping to attract further attention from other users who might be able to help me as so far I have been unable to use the Invoke method. Once again I apologise for my inexperience.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900