Sending the correct Gcode string to the serial port?

I am trying to send gcode g28 to my 3D RepRap printer via the port.Write("g28"); .

My program connects to the correct serial port, however, when I try to send information as a string, access to the COM port becomes negative. This is strange because the serial port was open before sending Gcode to it. He even sent some data back. What is the problem and how can I fix it?

Below are the lines of code that I use. A list of gcode commands is available on this page.

I tried adding "\n" to the end of the line, but that didn't work.

  //Fields List<string> myReceivedLines = new List<string>(); //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) { 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 homeall = default(bool); DA.GetData(5, ref homeall); SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); port.DtrEnable = true; port.Open(); if (connecttodevice == true) { port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); DA.SetDataList(0, myReceivedLines); } if (homeall == true) { port.Write("g28"); } } 
+4
source share
1 answer

Usually, when I create a Com port, it is a class level object that I initialize once at startup. I would move the initialization code from your method to a constructor or method that you can call once during startup. This should prevent you from trying to open it more than once and leave it available to respond to the Received Data.


I'm not sure about your architecture, it looks like you plan on having multiple ports, but to answer your question in the comments, everything you do to create a SerialPort. The minimum declaration for SerialPort must be a class level variable. Then you can check if there was nothing and create it for the first time.

 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); SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); DA.SetDataList(0, myReceivedLines); port.Open(); // optional I usually open it at the time that I initialize it. you can check `port.IsOpen()` // and Open it if it is false. 

second option with a SerialPort class level. Try something like this:

 protected override void SolveInstance(IGH_DataAccess DA) { if(port == null) { 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); port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); if (connecttodevice == true) { port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); DA.SetDataList(0, myReceivedLines); } port.DtrEnable = true; port.Open(); } bool homeall = default(bool); DA.GetData(5, ref homeall); if (homeall == true) { port.Write("g28"); } } 
+3
source

Source: https://habr.com/ru/post/1439700/


All Articles