public void ConnectToHopper(TextBox log = null) { // setup timer System.Windows.Forms.Timer reconnectionTimer = new System.Windows.Forms.Timer(); reconnectionTimer.Tick += new EventHandler(reconnectionTimer_Tick); reconnectionTimer.Interval = 1000; // ms int attempts = 10; // Setup connection info Hopper.CommandStructure.ComPort = Global.ComPort; Hopper.CommandStructure.SSPAddress = Global.Validator2SSPAddress; Hopper.CommandStructure.BaudRate = 9600; Hopper.CommandStructure.Timeout = 1000; Hopper.CommandStructure.RetryLevel = 3; // Run for number of attempts specified for (int i = 0; i < attempts; i++) { if (log != null) { log.AppendText("Trying connection to SMART Hopper\r\n"); } // turn encryption off for first stage Hopper.CommandStructure.EncryptionStatus = false; // if the key negotiation is successful then set the rest up if (Hopper.OpenPort() && Hopper.NegotiateKeys(log)) { Hopper.CommandStructure.EncryptionStatus = true; // now encrypting // find the max protocol version this validator supports byte maxPVersion = FindMaxHopperProtocolVersion(); if (maxPVersion >= 6) { Hopper.SetProtocolVersion(maxPVersion, log); } else { MessageBox.Show("This program does not support slaves under protocol 6!", "ERROR"); return; } // get info from the validator and store useful vars Hopper.SetupRequest(log); // inhibits, this sets which channels can receive notes Hopper.SetInhibits(log); // set running to true so the hopper begins getting polled hopperRunning = true; return; } // reset timer reconnectionTimer.Enabled = true; while (reconnectionTimer.Enabled) { if (CHelpers.Shutdown) { return; } Application.DoEvents(); } } }