I receive data from serial port and with this method MSDN source:
static void DataReceivedHandler(
Object^ sender,
SerialDataReceivedEventArgs^ e)
{
SerialPort^ sp = (SerialPort^)sender;
String^ indata = sp->ReadExisting();
System::Diagnostics::Debug::Write("Data Received:");
System::Diagnostics::Debug::Write(indata);
}
I can read this data. But because it is static function I don't have access to my GUI window objects and I can't write data to textBox widget.
The problem is known (e.g. C# related question) and I know I should use my own delegate
, but I'm not familiar with C++ and C# and after my quite long struggle I don't know how to use it.
I removed DataReceivedHandler
and this->mySerialPort->DataReceived += gcnew SerialDataReceivedEventHandler(DataReceivedHandler);
and what I have now (setDelegates
is called in Form constructor):
private: delegate void UpdateUiTextDelegate(String^ text);
UpdateUiTextDelegate^ myDelegate;
private: void setDelegates(void) {
myDelegate = gcnew UpdateUiTextDelegate(this,AddDataMethod);
}
public: void AddDataMethod(String^ myString)
{
this->readTextBox->AppendText(myString);
}
private: void mySerialPort_DataReceived(Object^ sender, SerialDataReceivedEventArgs^ e)
{
SerialPort^ sp = (SerialPort^)sender;
String^ s= sp->ReadExisting();
readTextBox->Invoke(this->myDelegate, s);
}
But mySerialPort_DataReceived
function is not called and I don't see data.
Can you help with this?