I would like to read information from a device connected to a SerialPort. I did this previously using a form (code below) but I am trying to do it without one, just storing the information into an array of string.
Code used with a form:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string line = port.ReadExisting();
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
}
private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox3.AppendText(line);
}
How far i got without a form (the DA method is allows to store variables within the program). I get the following error on the last line Cannot implicitly convert type 'string' to 'System.IO.Ports.SerialDataReceivedEventHandler'.
protected override void SolveInstance(IGH_DataAccess DA)
{
string selectedportname;
DA.GetData(1, ref selectedportname);
int selectedbaudrate;
DA.GetData(2, ref selectedbaudrate);
bool connecttodevice;
DA.GetData(3, ref connecttodevice);
bool sendtoprint;
DA.GetData(3, ref sendtoprint);
port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); //Create the serial port
port.DtrEnable = true; //enables the Data Terminal Ready (DTR) signal during serial communication (Handshaking)
port.Open(); //Open the port
port.DataReceived += port.ReadExisting();
}
Assuming
ReadExistingis a method with the right parameters:But the error indicates it is returning a ‘string’ so there is more to solve here.
Edit:
Looks like it should be: