Thread: Serial Port communication program crashes

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Serial Port communication program crashes

    I am reading serial data from a pic development board via usb virtual rs232 com port. My program compiles with no errors but crashes when i run it, i have attached some of the code if anyone can help, it would be greatly appreciated.

    Code:
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                      
                                         updatePorts();    //Function finds available com ports and displays the list in the combo box
    		  
                                          }
    
    private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {
      
                                          test();  //Function reads the serial port buffer an sends text string to Text Box
    		 
                      	                  }
    
    private: System::Void btnConnect_Click(System::Object^  sender, System::EventArgs^  e) {
    		
    		if (ComPort->IsOpen)
    		                   {
    		 
                                         	 	disconnect();
    		 
                                        	  	}
    		else
    		                   { 
    		 
                                        	 	connect();
    			Thread^ oThread = gcnew Thread( gcnew ThreadStart(this, &Form1::test) );
    			oThread->Start();
    		
                                       		}
    	                 }
    
    
                 bool x;   // Controls the while loop start stop condition when communication starts
    
                static SerialPort^ ComPort;
    
                 void updatePorts(){
    	             
                                                  array<String^>^ serialPorts = nullptr;
    	                            serialPorts = SerialPort::GetPortNames();
    	                            for each(String^ port in serialPorts)
    	             
                                                 {
    		
    		           cmbPortName->Items->Add(port);
    
                                                 }
    	
                                                  ComPort = gcnew SerialPort(); 
    
                                                 }
    
               void connect()
    		{
    			bool error = false;
    			
    			// Check if all settings have been selected
    
    			if (cmbPortName->SelectedIndex != -1 & cmbBaudRate->SelectedIndex != -1 & cmbParity->SelectedIndex != -1 & cmbDataBits->SelectedIndex != -1 & cmbStopBits->SelectedIndex != -1)
    			{
    					//if yes than Set The Port's settings
    					ComPort->PortName = cmbPortName->Text;
    					ComPort->BaudRate = Convert::ToInt32(cmbBaudRate->Text); //convert Text to Integer
    				                    ComPort->Parity = safe_cast<Parity>(Enum::Parse(Parity::typeid, cmbParity->Text)); //convert Text to Parity
    					ComPort->DataBits = Convert::ToInt32(cmbDataBits->Text); //convert Text to Integer
    					ComPort->StopBits = safe_cast<StopBits>(Enum::Parse(StopBits::typeid, cmbStopBits->Text)); //convert Text to stop bits
    
    				try //always try to use this try and catch method to open your port.
    				{
    					 //if there is an error your program will not display a message instead of freezing.
    					//Open Port
    					ComPort->Open();
    				}
    				catch (UnauthorizedAccessException ^e1)
    				{
    					error = true;
    				}
    				catch (System::IO::IOException ^e2)
    				{
    					error = true;
    				}
    				catch (ArgumentException ^e3)
    				{
    					error = true;
    				}
    
    				if (error)
    				{
    					MessageBox::Show(this, "Could not open the COM port. Most likely it is already in use, has been removed, or is unavailable.", "COM Port unavailable", MessageBoxButtons::OK, MessageBoxIcon::Stop);
    				}
    
    			}
    			else
    			{
    				MessageBox::Show("Please select all the COM Serial Port Settings", "Serial Port Interface", MessageBoxButtons::OK, MessageBoxIcon::Stop);
    
    			}
    			   //if the port is open, Change the Connect button to disconnect, enable the send button.
    			   //and disable the groupBox to prevent changing configuration of an open port.
    			if (ComPort->IsOpen)
    			{
    				btnConnect->Text = "Disconnect";
    				x=1;  //Start Thread to display receved data in text box
    			} 
    			
    			
    		}
    	                
                                       // Call this function to close the port.
             void disconnect()
    		{
    		ComPort->Close();
    		btnConnect->Text = "Connect";
    		groupBox1->Enabled = true;
    		x=0;  // Stop Thread from displaying data in text box
                        	}
                        
                          void test()      // read all available data in the receiving buffer.
    		{
                                       
                                       while(x)
    		            {                                                                   if I //ReadExisting() and replace it with a regular string "Hello" the program works
    		            String^ recievedData = serialPort1->ReadExisting(); //read all available data in the receiving buffer.
    		           // Show in the terminal window 
    		           rtxtDataArea->ForeColor = Color::Green; //write text data in Green
    		           rtxtDataArea->AppendText(recievedData);
    		           }
    		}

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Two points:
    1. Your code does not seem to be normal C++.
    2. If you want other humans to read your code, you need to indent it in a sane fashion.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by algorism View Post
    1. Your code does not seem to be normal C++.
    It's not. It's C++/CLI - a rather different language from ISO Standard C++. This forum is targeted for ISO Standard C++, and while it's possible you can get help with C++/CLI, unfortunately for you, it's somewhat unlikely.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Port or USB Communication Help
    By guitardenver in forum C Programming
    Replies: 4
    Last Post: 11-19-2011, 06:40 AM
  2. Serial Port Communication
    By ArunS in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-09-2011, 06:50 AM
  3. serial port communication using c++
    By mayuri_608 in forum C++ Programming
    Replies: 7
    Last Post: 04-20-2011, 04:30 AM
  4. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 09:32 AM
  5. Serial Port Communication
    By maxorator in forum C++ Programming
    Replies: 11
    Last Post: 04-27-2006, 03:13 PM

Tags for this Thread