예제 #1
0
 /// <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);
     }
 }
예제 #2
0
        public static void USBRemoved(object sender, EventArgs e)
        {
            PortStateChangeArg args = new PortStateChangeArg();

            args.State = "REMOVED";
            OnThresholdReached(args);
        }
예제 #3
0
        protected virtual void OnPortEvent(PortStateChangeArg e)
        {
            EventHandler handler = PortEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }
예제 #4
0
        private static void OnThresholdReached(PortStateChangeArg e)
        {
            EventHandler handler = USBPortChangeEvent;

            if (handler != null)
            {
                handler(null, e);
            }
        }
예제 #5
0
        /// <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);
        }