public Form2(Form1 form, Opto22Device pac, int port, int timeout)
 {
     InitializeComponent();
     callingForm  = form;
     this.pac     = pac;
     this.port    = port;
     this.timeout = timeout;
 }
        //recieve data and populate pac dict
        async Task RecieveDataAsync()
        {
            byte[] data = new byte[80];
            do
            {
                if (!timedout)
                {
                    try
                    {
                        var result = await UDPListener.ReceiveAsync();

                        Array.Copy(result.Buffer, data, result.Buffer.Length);
                        foreach (byte b in data)
                        {
                            Console.Write(b.ToString("X") + " ");
                        }
                        Console.WriteLine();

                        byte[] ipbytes      = new byte[4];
                        byte[] gatewaybytes = new byte[4];
                        byte[] subnetbytes  = new byte[4];
                        byte[] macbytes     = new byte[6];
                        byte[] firmbytes    = new byte[4];
                        byte[] unitbytes    = new byte[4];
                        byte[] dnsbytes     = new byte[4];

                        Array.Copy(data, Opto22Device.IPindex, ipbytes, 0, 4);
                        Array.Copy(data, Opto22Device.GATEWAYindex, gatewaybytes, 0, 4);
                        Array.Copy(data, Opto22Device.SUBNETindex, subnetbytes, 0, 4);
                        Array.Copy(data, Opto22Device.DNSindex, dnsbytes, 0, 4);
                        Array.Copy(data, Opto22Device.MACindex, macbytes, 0, 6);
                        Array.Copy(data, Opto22Device.FIRMindex, firmbytes, 0, 4);
                        Array.Copy(data, Opto22Device.UNITindex, unitbytes, 0, 4);

                        IPAddress PacIpAddress = new IPAddress(ipbytes);
                        IPAddress PacGateway   = new IPAddress(gatewaybytes);
                        IPAddress PacSubnet    = new IPAddress(subnetbytes);
                        IPAddress PacDNS       = new IPAddress(dnsbytes);

                        Opto22Device pac = new Opto22Device(PacIpAddress, PacSubnet, PacGateway, PacDNS, macbytes, firmbytes, unitbytes);
                        pac.Gateway = PacGateway;
                        pac.Subnet  = PacSubnet;
                        pac.Dns     = PacDNS;
                        if (!PACList.Exists(x => x.MACAddress == Opto22Device.getMACAddressString(macbytes)))
                        {
                            PACList.Add(pac);
                            pacbs.ResetBindings(false);
                        }
                    }
                    catch
                    {
                        //MessageBox.Show(e.ToString());
                    }
                }
            } while (UDPListener.Available > 0 || !timedout);
        }
        private void DownloadStrategy(string filepath)
        {
            var    rows     = PACGrid.SelectedRows;
            string filename = Path.GetFileName(filepath);

            if (rows.Count == 1)
            {
                Opto22Device pac                  = (Opto22Device)rows[0].DataBoundItem;
                string       controllerName       = pac.IPAddress.ToString();
                string       termclpath           = getTermclPath();
                string       createControllerName = "-a " + controllerName + " tcp " + pac.IPAddress.ToString() + " 22001 3 2000";
                string       uploadargs           = "-d " + controllerName + " \"" + filepath + "\"";
                Console.WriteLine(uploadargs);
                var process = System.Diagnostics.Process.Start(termclpath, createControllerName);
                while (!process.HasExited)
                {
                    ;
                }
                if (process.ExitCode != 0)
                {
                    MessageBox.Show("termCL.exe not properly installed!");
                    return;
                }
                process = System.Diagnostics.Process.Start(termclpath, uploadargs);
                while (!process.HasExited)
                {
                    ;
                }
                if (process.ExitCode != 0)
                {
                    MessageBox.Show("termCL.exe not properly installed!");
                    return;
                }
                process = System.Diagnostics.Process.Start(termclpath, "-r " + controllerName + "");
                while (!process.HasExited)
                {
                    ;
                }
                if (process.ExitCode != 0)
                {
                    MessageBox.Show("termCL.exe not properly installed!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Must select Control Engine from datagrid!");
            }
        }
        private void btnChange_Click(object sender, EventArgs e)
        {
            var rows = PACGrid.SelectedRows;

            if (rows.Count == 1)
            {
                Opto22Device pac        = (Opto22Device)rows[0].DataBoundItem;
                int          port       = 2001;
                int          timeout    = Convert.ToInt32(tbTimeout.Text);
                Form2        changeform = new Form2(this, pac, port, timeout);
                changeform.Show();
                changeform.Activate();
            }
            else
            {
                MessageBox.Show("Must select Control Engine from datagrid!");
            }
        }
        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var rows = PACGrid.SelectedRows;

            if (rows.Count == 1)
            {
                Opto22Device pac            = (Opto22Device)rows[0].DataBoundItem;
                string       controllerName = pac.IPAddress.ToString();
                string       termclpath     = getTermclPath();
                Console.WriteLine(termclpath);
                string createControllerName = "-a " + controllerName + " tcp " + pac.IPAddress.ToString() + " 22001 3 2000";
                var    process = System.Diagnostics.Process.Start(termclpath, createControllerName);
                while (!process.HasExited)
                {
                    ;
                }
                if (process.ExitCode != 0)
                {
                    MessageBox.Show("termCL.exe not properly installed!");
                    return;
                }
                process = System.Diagnostics.Process.Start(termclpath, "-r " + controllerName + "");
                while (!process.HasExited)
                {
                    ;
                }
                if (process.ExitCode != 0)
                {
                    MessageBox.Show("termCL.exe not properly installed!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Must select Control Engine from datagrid!");
            }
        }
 public void RemoveChangedPAC(Opto22Device pac)
 {
     PACList.Remove(pac);
     pacbs.ResetBindings(false);
 }