svenhb / GRBL-Plotter

A GCode sender (not only for lasers or plotters) for up to two GRBL controller. SVG, DXF, HPGL import. 6 axis DRO.
https://grbl-plotter.de/
GNU General Public License v3.0
674 stars 176 forks source link

functions handling the data revceived on com-port #38

Closed deHarro closed 5 years ago

deHarro commented 5 years ago

Hi Sven,

This is not a big issue since your current implementation works, but I wanted to point it out.

You recently pointed me to the instructions for GRBL communication and we got through all of theses in the past. You posted the relevant code part for communication to my joystick as:

A command line should end with \r\n (CR LF) to be recognized as one command.

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {   while ((serialPort.IsOpen) && (serialPort.BytesToRead > 0))
            {   rxString = string.Empty;
                try
                {   rxString = serialPort.ReadTo("\r\n");              //read line from grbl, discard CR LF
                    this.Invoke(new EventHandler(handleRxData));        //tigger rx process 
                    while ((serialPort.IsOpen) && (isDataProcessing)) ;  //wait previous data line processed done
                }
                catch (Exception errort)
                {   serialPort.Close();
                    logError("Error reading line from serial port", errort);
                }
            }
        }

        private void handleRxData(object sender, EventArgs e)
        {
            rtbLog.AppendText(string.Format("< {0} \r\n", rxString));
            OnRaiseCommandEvent(new CommandEventArgs(rxString));
            isDataProcessing = false;
        }

Originally posted by @svenhb in https://github.com/svenhb/GRBL-Plotter/issues/29#issuecomment-418317564

I just again read our discussion on the communication to GRBL and I stumbeled over the following instructions on the GRBL V1.1 site:

The primary way to talk to Grbl is performed by sending it a string of characters, followed by a carriage return

So your implementation seems to be over defined, you search for "CR LF", GRBL only wants "CR".

Harald

svenhb commented 5 years ago

You are right, I will change it next time.

svenhb commented 5 years ago

It's already implemented in the current version. I check for CR or LF:

        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {   while ((serialPort.IsOpen) && (serialPort.BytesToRead > 0))
            {
                byte[] rxBuff = new byte[serialPort.BytesToRead];
                serialPort.Read(rxBuff, 0, serialPort.BytesToRead);
                byte rxTmpChar = 0;
                try
                {   foreach (byte rxTmpChart in rxBuff)
                    {
                        rxTmpChar = rxTmpChart;
                        if ((rxTmpChar > 0x7F) || (isRealTimeCmd.Contains(rxTmpChar)))  // is real time cmd ?
                        {   rxChar = rxTmpChar;
                            this.Invoke(new EventHandler(handleRxData));
                        }
                        else
                        {   rxTmpString += (char)rxTmpChar;
                            if ((rxTmpChar == '\r') || (rxTmpChar == '\n'))             // end of regular command
                            {   if (lastChar >= ' ')
                                {
                                    rxChar = 0;
                                    rxString = rxTmpString.Trim();
                                    this.Invoke(new EventHandler(handleRxData));
                                }
                                rxTmpString = "";
                            }
                        }
                        lastChar = rxTmpChar;
                    }
                }
                catch (Exception errort)
                {   serialPort.Close();
                    logError("Error reading line from serial port", errort);
                }
            }
        }