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);
}
}