Welcome! Log In Create A New Profile

Advanced

C#: Getting feedback from the RepRap?

Posted by arthurmani 
C#: Getting feedback from the RepRap?
October 07, 2012 05:57AM
Hi Guys,

The code below builds with no errors, the connection to the device seems to work too although I do not get any feedback from the device.

on V.S., I have placed a break point at the line `myReceivedLines = sp.ReadExisting();` and the variable `myReceivedLines` comes back null.

On pronterface, couple lines of feedback appear when the reprap is connected (see below), why is this variable null in my case?

Lines that appear on pronterface:

Connecting...
start
Printer is now online.
echo:Marlin: 1.0.0 RC2
echo: Last Updated: 2012-05-22-1 | Author: eMAKER
...etc...


Code:

//Fields
    string myReceivedLines;

    //subscriber method for the port.DataReceived Event
    private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        myReceivedLines = sp.ReadExisting();
    }


       protected override void SolveInstance(IGH_DataAccess DA)
      {

        List gcode = new List();
        DA.GetDataList(0, gcode);
        string selectedportname = default(string);
        DA.GetData(1, ref selectedportname);
        int selectedbaudrate = default(int);
        DA.GetData(2, ref selectedbaudrate);
        bool connecttodevice=default(bool);
        DA.GetData(3, ref connecttodevice);
        bool sendtoprint= default(bool);
        DA.GetData(4, ref sendtoprint);

        if (!DA.GetDataList(0, gcode)) return;
        if (!DA.GetData(1, ref selectedportname)) return;
        if (!DA.GetData(2, ref selectedbaudrate)) return;
        if (!DA.GetData(3, ref connecttodevice)) return;
        if (!DA.GetData(4, ref sendtoprint)) return;


        SerialPort 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 += new SerialDataReceivedEventHandler(DataReceivedHandler);


        if (gcode == null)
        {
            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Specify a valid GCode");
            return;
        }

        if (connecttodevice == true)
        {
            DA.SetDataList(0, myReceivedLines);
        }
        else
        {
            port.Close();
        }

            if (sendtoprint == true)
            {
                foreach (String s in gcode)
                {
                    port.WriteLine(s);
                }
            }

                  }


--
Arthur Mamou-Mani
Architect, AAdipl, RIBA III
Tutor DS10 Westminster University
Mamou-Mani.com ¦ WeWantToLearn.net
Re: C#: Getting feedback from the RepRap?
October 07, 2012 08:06AM
Change the order of the code lines

From:
port.Open();             //Open the port
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

To:
port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
port.Open();             //Open the port


Bob Morrison
Wörth am Rhein, Germany
"Luke, use the source!"
BLOG - PHOTOS - Thingiverse
Re: C#: Getting feedback from the RepRap?
October 07, 2012 02:10PM
Thank you very much Bob,

Does it give more time for the DataReceived to fire?
Is there a way to get a line saying "connecting..." while it connects to the port?

Also, the data received from the serial port uses a new line for every character as opposed to line by line for every "sentence". Is there a way to get "sentences" to appear on separate lines as opposed to character?

Many thanks,

Arthur


--
Arthur Mamou-Mani
Architect, AAdipl, RIBA III
Tutor DS10 Westminster University
Mamou-Mani.com ¦ WeWantToLearn.net
Sorry, only registered users may post in this forum.

Click here to login