private void LoadSettingsBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter           = "PIE Settings File (*.pie)|*.pie";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog.Multiselect      = false;

            if (openFileDialog.ShowDialog() == true)
            {
                string fileName = openFileDialog.FileName;

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    FileStream    fs         = new FileStream(fileName, FileMode.Open);
                    Settings      settings   = (Settings)serializer.Deserialize(fs);

                    SPIComms.persist = settings.persist;
                    if (SPIComms.Connected())
                    {
                        SPIComms.WritePersist();
                    }

                    updateSettings(settings);
                }
                catch (Exception ex)
                {
                    LogScreen.PushMessage(ex.StackTrace, MessageType.Warning);
                }
            }
        }
        private void loadBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter           = "PIE Job File (*.job)|*.job";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            openFileDialog.Multiselect      = false;

            if (openFileDialog.ShowDialog() == true)
            {
                string fileName = openFileDialog.FileName;

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(JobSettings));
                    FileStream    fs         = new FileStream(fileName, FileMode.Open);
                    JobSettings   settings   = (JobSettings)serializer.Deserialize(fs);

                    SPIComms.global.touchRMS        = settings.touchRMS;
                    SPIComms.global.peckDistance    = settings.peckDistance;
                    SPIComms.global.retractDistance = settings.retractDistance;

                    SPIComms.global.xOffset        = settings.xOffset;
                    SPIComms.global.heaterSetpoint = settings.heaterTemp;

                    SPIComms.global.ecAmpTarget    = settings.currentTarget;
                    SPIComms.global.ecTime         = settings.ecTime;
                    SPIComms.global.enableSloshing = settings.enableSloshing;
                    SPIComms.global.sloshAmplitude = settings.sloshAmp;
                    SPIComms.global.sloshFrequency = settings.sloshFreq;
                    SPIComms.global.sloshTime      = settings.sloshTime;

                    SPIComms.global.holeSpacing = settings.holeSpacing;
                    SPIComms.global.numHoles    = settings.numHoles;


                    if (SPIComms.Connected())
                    {
                        SPIComms.WriteGlobal();
                    }

                    updateSettings(settings);
                }
                catch (Exception ex)
                {
                    LogScreen.PushMessage(ex.StackTrace, MessageType.Warning);
                }
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SPIComms = Communication.Instance;
            try
            {
                bitConnected    = new BitmapImage(new Uri("pack://application:,,,/Resources/Icons/Green.ico", UriKind.Absolute));
                bitDisconnected = new BitmapImage(new Uri("pack://application:,,,/Resources/Icons/Grey.ico", UriKind.Absolute));
            }
            catch (Exception ex)
            {
                LogScreen.PushMessage(ex.StackTrace, MessageType.Error);
                Console.WriteLine(ex.StackTrace);
            }

            BaudRateCmB.SelectedIndex = 0;   //  Baud rate
            ConnTypeCmB.SelectedIndex = 0;   //  Ethernet connection type
            ComTypeCmB.SelectedIndex  = 0;   //  Communication medium (e.g. Serial, Ethernet)
            CommPortCmB.SelectedIndex = 0;   //  COM channel
        }
        private void ConnectBtn_Click(object sender, RoutedEventArgs e)
        {
            //if communication is closed
            if (!SPIComms.Connected())
            {
                try
                {
                    if (ComTypeCmB.SelectedIndex == 0)                  //  Serial
                    {
                        int Baud_Rate;
                        if (BaudRateCmB.SelectedValue.ToString() == "Auto")
                        {
                            Baud_Rate = -1;
                        }
                        else
                        {
                            Baud_Rate = Convert.ToInt32(BaudRateCmB.SelectedValue.ToString());
                        }

                        // Open serial communuication.
                        // CommPortCmB.SelectedIndex + 1 defines the COM port.
                        SPIComms.OpenCommSerial(CommPortCmB.SelectedIndex + 1, Baud_Rate);
                    }
                    else if (ComTypeCmB.SelectedIndex == 1)             //  Ethernet
                    {
                        int Protocol;
                        if (ConnTypeCmB.SelectedIndex == 0)             //  Point to Point
                        {
                            Protocol = (int)EthernetCommOption.ACSC_SOCKET_DGRAM_PORT;
                        }
                        else
                        {
                            Protocol = (int)EthernetCommOption.ACSC_SOCKET_STREAM_PORT;
                        }
                        // Open ethernet communuication.
                        // RemoteAddress.getNumeric() defines the controller's TCP/IP address.
                        // Protocol is TCP/IP in case of network connection, and UDP in case of point-to-point connection.
                        SPIComms.OpenCommNetwork(RemoteAddressTB.Text, Protocol);
                    }
                    else if (ComTypeCmB.SelectedIndex == 2)             //  PCI
                    {
                        //Open PCI Bus communuication
                        int SlotNumber = Convert.ToInt32(SlotNumberTB.Text);
                        SPIComms.OpenCommPCI(SlotNumber);
                    }
                    else //(CComTypeCmB.SelectedIndex == 3)
                         //Open communuication with Simulator
                    {
                        SPIComms.OpenCommSimulator();
                    }

                    // Save new communication media
                    ComTypeOld = ComTypeCmB.SelectedIndex;
                    if (SPIComms.Connected())
                    {
                        ConnectionState.Source = bitConnected;
                        updateSettings(new Settings());
                    }
                }
                catch (Exception Ex)
                {
                    LogScreen.PushMessage(Ex.StackTrace, MessageType.Error);
                    Console.WriteLine(Ex);                      //  Throw exception if this fails
                }
            }
        }