Ejemplo n.º 1
0
        public void Disconnect()
        {
            if (!Connected)
            {
                throw new Exception("Can't Disconnect: Not Connected");
            }

            Connected = false;

            WorkerThread.Join();

            Connection.Close();
            Connection.Dispose();
            Connection = null;

            Mode = OperatingMode.Disconnected;

            MachinePosition = new Vector3();
            WorkPosition    = new Vector3();

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            Status       = "Disconnected";
            DistanceMode = ParseDistanceMode.Absolute;
            Unit         = ParseUnit.Metric;
            Plane        = ArcPlane.XY;
            BufferState  = 0;

            ToSend.Clear();
            ToSendPriority.Clear();
            Sent.Clear();
        }
Ejemplo n.º 2
0
        public void Connect()
        {
            if (Connected)
            {
                throw new Exception("Can't Connect: Already Connected");
            }

            switch (Properties.Settings.Default.ConnectionType)
            {
            case ConnectionType.Serial:
                SerialPort port = new SerialPort(Properties.Settings.Default.SerialPortName, Properties.Settings.Default.SerialPortBaud);
                port.Open();
                Connection = port.BaseStream;
                break;

            default:
                throw new Exception("Invalid Connection Type");
            }

            Connected = true;

            ToSend.Clear();
            Sent.Clear();

            Mode = OperatingMode.Manual;

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            WorkerThread          = new Thread(Work);
            WorkerThread.Priority = ThreadPriority.AboveNormal;
            WorkerThread.Start();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses a recevied status report (answer to '?')
        /// </summary>
        private void ParseStatus(string line)
        {
            Match statusMatch = StatusEx.Match(line);

            if (!statusMatch.Success)
            {
                NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line));
                return;
            }

            Group status = statusMatch.Groups["State"];

            if (status.Success)
            {
                Status = status.Value;
            }

            Vector3 NewMachinePosition, NewWorkPosition;
            bool    update = false;

            Group mx = statusMatch.Groups["MX"], my = statusMatch.Groups["MY"], mz = statusMatch.Groups["MZ"];

            if (mx.Success)
            {
                NewMachinePosition = new Vector3(double.Parse(mx.Value, Constants.DecimalParseFormat), double.Parse(my.Value, Constants.DecimalParseFormat), double.Parse(mz.Value, Constants.DecimalParseFormat));

                if (MachinePosition != NewMachinePosition)
                {
                    update = true;
                }

                MachinePosition = NewMachinePosition;
            }

            Group wx = statusMatch.Groups["WX"], wy = statusMatch.Groups["WY"], wz = statusMatch.Groups["WZ"];

            if (wx.Success)
            {
                NewWorkPosition = new Vector3(double.Parse(wx.Value, Constants.DecimalParseFormat), double.Parse(wy.Value, Constants.DecimalParseFormat), double.Parse(wz.Value, Constants.DecimalParseFormat));

                if (WorkPosition != NewWorkPosition)
                {
                    update = true;
                }

                WorkPosition = NewWorkPosition;
            }

            if (update && Connected && PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }
        }
Ejemplo n.º 4
0
        public void Connect()
        {
            if (Connected)
            {
                throw new Exception("Can't Connect: Already Connected");
            }

            switch (Properties.Settings.Default.ConnectionType)
            {
            case ConnectionType.Serial:
                SerialPort port = new SerialPort(Properties.Settings.Default.SerialPortName, Properties.Settings.Default.SerialPortBaud);
                port.Encoding        = System.Text.Encoding.GetEncoding(1252);
                port.WriteBufferSize = Properties.Settings.Default.ControllerBufferSize;
                port.ReadBufferSize  = Properties.Settings.Default.ControllerBufferSize;
                port.Open();
                Connection = port.BaseStream;
                break;

            default:
                throw new Exception("Invalid Connection Type");
            }

            if (Properties.Settings.Default.LogTraffic)
            {
                try
                {
                    Log = new StreamWriter(Constants.LogFile);
                }
                catch (Exception e)
                {
                    NonFatalException("could not open logfile: " + e.Message);
                }
            }

            Connected = true;

            ToSend.Clear();
            ToSendPriority.Clear();
            Sent.Clear();
            ToSendMacro.Clear();

            Mode = OperatingMode.Manual;

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            WorkerThread          = new Thread(Work);
            WorkerThread.Priority = ThreadPriority.AboveNormal;
            WorkerThread.Start();
        }
Ejemplo n.º 5
0
        public void Disconnect()
        {
            if (Log != null)
            {
                Log.Close();
            }
            Log = null;

            Connected = false;

            WorkerThread.Join();

            try
            {
                Connection.Close();
            }
            catch { }

            Connection.Dispose();
            Connection = null;

            Mode = OperatingMode.Disconnected;

            MachinePosition  = new Vector3();
            WorkOffset       = new Vector3();
            FeedRateRealtime = 0;
            CurrentTLO       = 0;

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            Status       = "Disconnected";
            DistanceMode = ParseDistanceMode.Absolute;
            Unit         = ParseUnit.Metric;
            Plane        = ArcPlane.XY;
            BufferState  = 0;

            FeedOverride    = 100;
            RapidOverride   = 100;
            SpindleOverride = 100;

            if (OverrideChanged != null)
            {
                OverrideChanged.Invoke();
            }

            PinStateLimitX = false;
            PinStateLimitY = false;
            PinStateLimitZ = false;
            PinStateProbe  = false;

            if (PinStateChanged != null)
            {
                PinStateChanged.Invoke();
            }

            ToSend.Clear();
            ToSendPriority.Clear();
            Sent.Clear();
            ToSendMacro.Clear();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Parses a recevied status report (answer to '?')
        /// </summary>
        private void ParseStatus(string line)
        {
            MatchCollection statusMatch = StatusEx.Matches(line);

            if (statusMatch.Count == 0)
            {
                NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line));
                return;
            }

            bool posUpdate      = false;
            bool overrideUpdate = false;
            bool pinStateUpdate = false;
            bool resetPins      = true;

            foreach (Match m in statusMatch)
            {
                if (m.Index == 1)
                {
                    Status = m.Groups[1].Value;
                    continue;
                }

                if (m.Groups[1].Value == "Ov")
                {
                    try
                    {
                        string[] parts = m.Groups[2].Value.Split(',');
                        FeedOverride    = int.Parse(parts[0]);
                        RapidOverride   = int.Parse(parts[1]);
                        SpindleOverride = int.Parse(parts[2]);
                        overrideUpdate  = true;
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }

                else if (m.Groups[1].Value == "WCO")
                {
                    try
                    {
                        string OffsetString = m.Groups[2].Value;

                        if (Properties.Settings.Default.IgnoreAdditionalAxes)
                        {
                            string[] parts = OffsetString.Split(',');
                            if (parts.Length > 3)
                            {
                                Array.Resize(ref parts, 3);
                                OffsetString = string.Join(",", parts);
                            }
                        }

                        WorkOffset = Vector3.Parse(OffsetString);
                        posUpdate  = true;
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }

                else if (SyncBuffer && m.Groups[1].Value == "Bf")
                {
                    try
                    {
                        int availableBytes = int.Parse(m.Groups[2].Value.Split(',')[1]);
                        int used           = Properties.Settings.Default.ControllerBufferSize - availableBytes;

                        if (used < 0)
                        {
                            used = 0;
                        }

                        BufferState = used;
                        RaiseEvent(Info, $"Buffer State Synced ({availableBytes} bytes free)");
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }

                else if (m.Groups[1].Value == "Pn")
                {
                    resetPins = false;

                    string states = m.Groups[2].Value;

                    bool stateX = states.Contains("X");
                    if (stateX != PinStateLimitX)
                    {
                        pinStateUpdate = true;
                    }
                    PinStateLimitX = stateX;

                    bool stateY = states.Contains("Y");
                    if (stateY != PinStateLimitY)
                    {
                        pinStateUpdate = true;
                    }
                    PinStateLimitY = stateY;

                    bool stateZ = states.Contains("Z");
                    if (stateZ != PinStateLimitZ)
                    {
                        pinStateUpdate = true;
                    }
                    PinStateLimitZ = stateZ;

                    bool stateP = states.Contains("P");
                    if (stateP != PinStateProbe)
                    {
                        pinStateUpdate = true;
                    }
                    PinStateProbe = stateP;
                }

                else if (m.Groups[1].Value == "F")
                {
                    try
                    {
                        FeedRateRealtime = double.Parse(m.Groups[2].Value, Constants.DecimalParseFormat);
                        posUpdate        = true;
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }

                else if (m.Groups[1].Value == "FS")
                {
                    try
                    {
                        string[] parts = m.Groups[2].Value.Split(',');
                        FeedRateRealtime     = double.Parse(parts[0], Constants.DecimalParseFormat);
                        SpindleSpeedRealtime = double.Parse(parts[1], Constants.DecimalParseFormat);
                        posUpdate            = true;
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }
            }

            SyncBuffer = false;             //only run this immediately after button press

            //run this later to catch work offset changes before parsing position
            Vector3 NewMachinePosition = MachinePosition;

            foreach (Match m in statusMatch)
            {
                if (m.Groups[1].Value == "MPos" || m.Groups[1].Value == "WPos")
                {
                    try
                    {
                        string PositionString = m.Groups[2].Value;

                        if (Properties.Settings.Default.IgnoreAdditionalAxes)
                        {
                            string[] parts = PositionString.Split(',');
                            if (parts.Length > 3)
                            {
                                Array.Resize(ref parts, 3);
                                PositionString = string.Join(",", parts);
                            }
                        }

                        NewMachinePosition = Vector3.Parse(PositionString);

                        if (m.Groups[1].Value == "WPos")
                        {
                            NewMachinePosition += WorkOffset;
                        }

                        if (NewMachinePosition != MachinePosition)
                        {
                            posUpdate       = true;
                            MachinePosition = NewMachinePosition;
                        }
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }
            }

            if (posUpdate && Connected && PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            if (overrideUpdate && Connected && OverrideChanged != null)
            {
                OverrideChanged.Invoke();
            }

            if (resetPins)                                                                         //no pin state received in status -> all zero
            {
                pinStateUpdate = PinStateLimitX | PinStateLimitY | PinStateLimitZ | PinStateProbe; //was any pin set before

                PinStateLimitX = false;
                PinStateLimitY = false;
                PinStateLimitZ = false;
                PinStateProbe  = false;
            }

            if (pinStateUpdate && Connected && PinStateChanged != null)
            {
                PinStateChanged.Invoke();
            }

            if (Connected && StatusReceived != null)
            {
                StatusReceived.Invoke(line);
            }
        }
Ejemplo n.º 7
0
        public void Disconnect()
        {
            if (Log != null)
            {
                Log.Close();
            }
            Log = null;

            Connected = false;

            WorkerThread.Join();
            switch (Properties.Settings.Default.ConnectionType)
            {
            case ConnectionType.Serial:
                try
                {
                    Connection.Close();
                }
                catch { }
                Connection.Dispose();
                Connection = null;
                break;

            case ConnectionType.Ethernet:
                if (Connection != null)
                {
                    Connection.Close();
                    ClientEthernet.Close();
                }
                Connection = null;
                break;

            default:
                throw new Exception("Invalid Connection Type");
            }
            Mode = OperatingMode.Disconnected;

            MachinePosition  = new Vector3();
            WorkOffset       = new Vector3();
            FeedRateRealtime = 0;
            CurrentTLO       = 0;

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            Status       = "Disconnected";
            DistanceMode = ParseDistanceMode.Absolute;
            Unit         = ParseUnit.Metric;
            Plane        = ArcPlane.XY;
            BufferState  = 0;

            FeedOverride    = 100;
            RapidOverride   = 100;
            SpindleOverride = 100;

            if (OverrideChanged != null)
            {
                OverrideChanged.Invoke();
            }

            PinStateLimitX = false;
            PinStateLimitY = false;
            PinStateLimitZ = false;
            PinStateProbe  = false;

            if (PinStateChanged != null)
            {
                PinStateChanged.Invoke();
            }

            ToSend.Clear();
            ToSendPriority.Clear();
            Sent.Clear();
            ToSendMacro.Clear();
        }
Ejemplo n.º 8
0
        public void Connect()
        {
            if (Connected)
            {
                throw new Exception("Can't Connect: Already Connected");
            }


            switch (Properties.Settings.Default.ConnectionType)
            {
            case ConnectionType.Serial:
                SerialPort port = new SerialPort(Properties.Settings.Default.SerialPortName, Properties.Settings.Default.SerialPortBaud);
                port.DtrEnable = Properties.Settings.Default.SerialPortDTR;
                port.Open();
                Connection = port.BaseStream;
                Connected  = true;
                break;

            case ConnectionType.Ethernet:
                try
                {
                    RaiseEvent(Info, "Connecting to " + Properties.Settings.Default.EthernetIP + ":" + Properties.Settings.Default.EthernetPort);
                    ClientEthernet = new TcpClient(Properties.Settings.Default.EthernetIP, Properties.Settings.Default.EthernetPort);
                    Connected      = true;
                    RaiseEvent(Info, "Successful Connection");
                    Connection = ClientEthernet.GetStream();
                }
                catch (ArgumentNullException)
                {
                    MessageBox.Show("Invalid address or port");
                }
                catch (SocketException)
                {
                    MessageBox.Show("Connection failure");
                }

                break;

            default:
                throw new Exception("Invalid Connection Type");
            }

            if (!Connected)
            {
                return;
            }

            if (Properties.Settings.Default.LogTraffic)
            {
                try
                {
                    Log = new StreamWriter(Constants.LogFile);
                }
                catch (Exception e)
                {
                    NonFatalException("could not open logfile: " + e.Message);
                }
            }



            ToSend.Clear();
            ToSendPriority.Clear();
            Sent.Clear();
            ToSendMacro.Clear();

            Mode = OperatingMode.Manual;

            if (PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }

            WorkerThread          = new Thread(Work);
            WorkerThread.Priority = ThreadPriority.AboveNormal;
            WorkerThread.Start();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Parses a recevied status report (answer to '?')
        /// </summary>
        private void ParseStatus(string line)
        {
            MatchCollection statusMatch = StatusEx.Matches(line);

            if (statusMatch.Count == 0)
            {
                NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line));
                return;
            }

            bool posUpdate = false;

            foreach (Match m in statusMatch)
            {
                if (m.Index == 1)
                {
                    Status = m.Groups[1].Value;
                    continue;
                }

                if (m.Groups[1].Value == "WCO")
                {
                    try
                    {
                        WorkOffset = Vector3.Parse(m.Groups[2].Value);
                        posUpdate  = true;
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }

                if (SyncBuffer && m.Groups[1].Value == "Bf")
                {
                    try
                    {
                        int availableBytes = int.Parse(m.Groups[4].Value);
                        int used           = Properties.Settings.Default.ControllerBufferSize - availableBytes;

                        if (used < 0)
                        {
                            used = 0;
                        }

                        BufferState = used;
                        RaiseEvent(Info, $"Buffer State Synced ({availableBytes} bytes free)");
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }
            }

            SyncBuffer = false;             //only run this immediately after button press

            //run this later to catch work offset changes before parsing position
            Vector3 NewMachinePosition = MachinePosition;

            foreach (Match m in statusMatch)
            {
                if (m.Groups[1].Value == "MPos" || m.Groups[1].Value == "WPos")
                {
                    try
                    {
                        NewMachinePosition = Vector3.Parse(m.Groups[2].Value);

                        if (m.Groups[1].Value == "WPos")
                        {
                            NewMachinePosition += WorkOffset;
                        }

                        if (NewMachinePosition != MachinePosition)
                        {
                            posUpdate       = true;
                            MachinePosition = NewMachinePosition;
                        }
                    }
                    catch { NonFatalException.Invoke(string.Format("Received Bad Status: '{0}'", line)); }
                }
            }

            if (posUpdate && Connected && PositionUpdateReceived != null)
            {
                PositionUpdateReceived.Invoke();
            }
        }