// This function opens the com port and attempts to connect with the validator. It then negotiates // the keys for encryption and performs some other setup commands. private bool ConnectToValidator() { // setup the timer reconnectionTimer.Interval = reconnectionInterval * 1000; // for ms // run for number of attempts specified for (int i = 0; i < reconnectionAttempts; i++) { // reset timer reconnectionTimer.Enabled = true; // close com port in case it was open Validator.SSPComms.CloseComPort(); // turn encryption off for first stage Validator.CommandStructure.EncryptionStatus = false; // open com port and negotiate keys if (Validator.OpenComPort(textBox1) && Validator.NegotiateKeys(textBox1)) { Validator.CommandStructure.EncryptionStatus = true; // now encrypting // find the max protocol version this validator supports byte maxPVersion = FindMaxProtocolVersion(); if (maxPVersion > 6) { Validator.SetProtocolVersion(maxPVersion, textBox1); } else { MessageBox.Show("This program does not support units under protocol version 6, update firmware.", "ERROR"); return(false); } // get info from the validator and store useful vars Validator.SetupRequest(textBox1); // check this unit is supported by this program if (!IsUnitTypeSupported(Validator.UnitType)) { MessageBox.Show("Unsupported unit type, this SDK supports the BV series and the NV series (excluding the NV11)"); Application.Exit(); return(false); } // inhibits, this sets which channels can receive notes Validator.SetInhibits(textBox1); // enable, this allows the validator to receive and act on commands Validator.EnableValidator(textBox1); return(true); } while (reconnectionTimer.Enabled) { Application.DoEvents(); // wait for reconnectionTimer to tick } } return(false); }
// The main program loop, this is to control the validator, it polls at // a value set in this class (pollTimer). public void MainLoop() { Enabled = true; // Connect to the validators ConnectToNoteValidator(textBox1); ConnectToHopper(textBox1); // Enable the validators Validator.EnableValidator(textBox1); Hopper.EnableValidator(textBox1); // While either one is still running while (!CHelpers.Shutdown) { // Setup form layout on first run if (!FormSetup) { SetupFormLayout(); FormSetup = true; } // If the hopper is supposed to be running but the poll fails if (hopperRunning && !Hopper.DoPoll(textBox1)) { textBox1.AppendText("Lost connection to SMART Hopper\r\n"); hopperRunning = false; // If the other device has also stopped, close the port if (!validatorRunning) { LibraryHandler.ClosePort(); } // Create a reconnection thread, this allows this loop to continue executing // and polling the other validator Thread t = new Thread(ReconnectHopper); t.Start(); } // If the validator is supposed to be running but the poll fails if (validatorRunning && !Validator.DoPoll(textBox1)) { textBox1.AppendText("Lost connection to note validator\r\n"); validatorRunning = false; // If the other device has also stopped, close the port if (!hopperRunning) { LibraryHandler.ClosePort(); } // Create a reconnection thread, this allows this loop to continue executing // and polling the other validator Thread t = new Thread(ReconnectValidator); t.Start(); } UpdateUI(); timer1.Enabled = true; while (timer1.Enabled) { Application.DoEvents(); } } LibraryHandler.ClosePort(); btnRun.Enabled = true; btnHalt.Enabled = false; }