Grasshopper

algorithmic modeling for Rhino

Hi Guys,

I use the method DA.SetDataList several time within the Solve Instance Method to send lists of strings to a specific output. For example, I use the following statement: `DA.SetDataList(0, variable[i] + "\n");` and then later: `DA.SetDataList(0, "string");`

When I run the commands generating these arguments for DA.SetDataList(), the previous output gets erased and the new data is displayed. How can I keep appending to the output so that it keeps a trace of everything that is being sent to it?

Many thanks,

Arthur

Views: 527

Replies to This Discussion

I would create a global variable, outside of the Solve Instance routine to be of whatever type the variable you want to store (it looks like you're trying to store a string).  So something like this:

List<string> foo = new List<string>();

Then, inside your Solve Instance routine, just use the add function to add whatever you want to each item.  You've pretty much already got the code for this:

foo.Add(variable[i] + "\n");  //this is assuming you're putting this inside a loop where i is changing

Then, just set the output to use this list.

 

DA.SetDataList(0, foo);

Is this what you were looking for?

Thanks a lot Andy,

 

I am very glad that you replied to this question as I am dealing with the SerialPort() methods and you created a component using them with Firefly. The problem here might be more complicated than it seems as I have an event and a subscriber method receiving data from a serial port.

In the code below, the strings received within myReceivedLines appear when connecting with the serial port (when connecttodevice is true). However they disapear when I launch another command (when homeallis true).

 

As you recommended in your reply, I have added the field called myReceivedLineswithin the class so that I could use the method String.Add() to all the feedback received and commands sent.

 

Why does the feedback dispear when a command is sent? Is the string going to myReceivedLine disappearing because they happen within a subscriber method or is it related to the DA.SetDataList() method used to assign myReceivedLinesto the output?

 

Many thanks!

 

 

public class SendToPrintComponent : GH_Component
{
//Fields
List<string> myReceivedLines = new List<string>();
SerialPort port;

//subscriber method for the port.DataReceived Event
private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
while (sp.BytesToRead > 0)
{
try
{
myReceivedLines
.Add(sp.ReadLine());
}
catch (TimeoutException)
{
break;
}
}
}

protected override void SolveInstance(IGH_DataAccess DA)
{
//Opening the port
if (port == null)
{
string selectedportname = default(string);
DA
.GetData(1, ref selectedportname);
int selectedbaudrate = default(int);
DA
.GetData(2, ref selectedbaudrate);


//Assigning an object to the field within the SolveInstance method()
port
= new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);

//Enables the data terminal ready (dtr) signal during serial communication (handshaking)
port
.DtrEnable = true;
port
.WriteTimeout = 500;
port
.ReadTimeout = 500;
}

//Event Handling Method
bool connecttodevice
= default(bool);
DA
.GetData(3, ref connecttodevice);

if (connecttodevice == true)
{
if (!port.IsOpen)
{
port
.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
DA
.SetDataList(0, myReceivedLines);
port
.Open();

}
}
else
if (port.IsOpen)
{
port
.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
port
.Close();
}


if (port.IsOpen)
{
DA
.SetData(1, "Port Open");
}

//If the port is open do all the rest
if (port.IsOpen)
{
bool homeall
= default(bool);
DA
.GetData(5, ref homeall);


//Home all sends all the axis to the origin
if (homeall == true)
{
port
.Write("G28" + "\n");
myReceivedLines
.Add("G28" + "\n");
DA
.SetDataList(2, myReceivedLines);
}

}
else
{
DA
.SetData(1, "Port Closed");
}
}
}

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service