/// <summary> /// Send a char array command to grbl port /// </summary> /// <param name="bytes"></param> public void Send(char[] bytes) { if (grblPort != null && grblPort.IsOpen) { lock (lockObject) { try { grblPort.Write(bytes, 0, bytes.Length); } catch (Exception e) { PortStateChangeArg arg = new PortStateChangeArg(); arg.State = "Disconnected"; OnPortEvent(arg); } } } else { PortStateChangeArg arg = new PortStateChangeArg(); arg.State = "Disconnected"; OnPortEvent(arg); } }
public static void USBRemoved(object sender, EventArgs e) { PortStateChangeArg args = new PortStateChangeArg(); args.State = "REMOVED"; OnThresholdReached(args); }
protected virtual void OnPortEvent(PortStateChangeArg e) { EventHandler handler = PortEvent; if (handler != null) { handler(this, e); } }
private static void OnThresholdReached(PortStateChangeArg e) { EventHandler handler = USBPortChangeEvent; if (handler != null) { handler(null, e); } }
/// <summary> /// Open grbl port /// </summary> /// <param name="portDesc">port description object</param> /// <returns></returns> public bool Open(PortDesc portDesc) { bool IsConnected = false; //Initiate parameter descriptions and types GrblParameterBase.Init(); //Initialize the serial port grblPort = new SerialPort(portDesc.DeviceId, 115200, Parity.None, 8, StopBits.One); //Setup event handler grblPort.DataReceived += GrblPort_DataReceived; grblPort.ErrorReceived += GrblPort_ErrorReceived; //Open the port if (!grblPort.IsOpen) { try { grblPort.Open(); this.portDesc = portDesc; System.Threading.Thread.Sleep(1000); CMD_RESET(); System.Threading.Thread.Sleep(1000); CMD_QUERY(); } catch (Exception e) { //Raise port open error event } } else { //Raise port already open event; } if (grblPort.IsOpen) { PortStateChangeArg arg = new PortStateChangeArg(); arg.State = "Connected"; OnPortEvent(arg); CMD_GETOFFSET(); queryThread = new Thread(delegate() { StartQueryLoop(); }); queryThread.Start(); IsConnected = true; } return(IsConnected); }