Scott_N

I am new to C and am trying to write a simple program to read from the SerialPort using the .NET component. I have yet to find any complete samples on using the SerialPort component in VC++ but I have managed to piece together some code from different Forum Posts. With this code I can Open COM1 and write something out it but I can't seem to read anything in. I don't get any Compiler/Debugger errors so I can't tell whats wrong. I am sending a few characters with a line feed to the port but it appears that the DataReceived Event isn't firing-off as I can't even get the [richTextBox1->Text = "Received Event"] update to happen.

Any Ideas
and what does the "Data" in [ this->richTextBox1->Invoke(pfnDelegate, "Data");] represent


Code Snippet

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
serialPort1->BaudRate = 9600;
serialPort1->DataBits = 8;
serialPort1->StopBits = System::IO::Ports::StopBits::One;
serialPort1->PortName = "COM1";
serialPort1->Handshake = System::IO::Ports::Handshake::None;
serialPort1->Parity = System::IO::Ports::Parity::None;
serialPort1->Open();
if(serialPort1->IsOpen)
label1->Text = "COM 1 Open",
serialPort1->Write("hello world ");
}


delegate void SetRichTextDelegateType(System::String ^);


private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
{
richTextBox1->Text = "Received Event";
String^ myString = serialPort1->ReadLine();
SetRichTextDelegateType ^pfnDelegate = gcnew SetRichTextDelegateType(this, &Form1::SetRichText);
this->richTextBox1->Invoke(pfnDelegate, "Data");
}

void SetRichText(String ^myString)
{
richTextBox1->AppendText(myString);
}



Re: Visual C++ General SerialPort DataReceived Event: What's wrong with this code?

Scott_N

Nevermind, after spending a few hours checking everything, I found that the SerialPort Control "Properties" page (under the lightning bolt) was not configured to receive any events by default. I sure wish that was documented somewhere.