// 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(int attempts) { // setup timer System.Windows.Forms.Timer reconnectionTimer = new System.Windows.Forms.Timer(); reconnectionTimer.Tick += new EventHandler(reconnectionTimer_Tick); reconnectionTimer.Interval = 3000; // ms // run for number of attempts specified for (int i = 0; i < attempts; i++) { // close com port in case it was open NV11.SSPComms.CloseComPort(); // turn encryption off for first stage NV11.CommandStructure.EncryptionStatus = false; // if the key negotiation is successful then set the rest up if (NV11.NegotiateKeys(textBox1) == true) { NV11.CommandStructure.EncryptionStatus = true; // now encrypting // find the max protocol version this validator supports byte maxPVersion = FindMaxProtocolVersion(); if (maxPVersion >= 6) { NV11.SetProtocolVersion(maxPVersion, textBox1); } else { MessageBox.Show("This program does not support validators under protocol 6!", "ERROR"); return(false); } // get info from the validator and store useful vars NV11.SetupRequest(textBox1); // inhibits, this sets which channels can receive notes NV11.SetInhibits(textBox1); // enable, this allows the validator to operate NV11.EnableValidator(textBox1); // value reporting, set whether the validator reports channel or coin value in // subsequent requests NV11.SetValueReportingType(false, textBox1); // check for notes already in the float on startup NV11.CheckForStoredNotes(textBox1); // return(true); } // reset timer reconnectionTimer.Enabled = true; while (reconnectionTimer.Enabled) { Application.DoEvents(); } } 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() { btnRun.Enabled = false; btnHalt.Enabled = true; Thread tNV11Rec = null, tHopRec = null; // Connect to the validators ConnectToNV11(textBox1); ConnectToHopper(textBox1); NV11.EnableValidator(); Hopper.EnableValidator(); // While application is still active 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"); // If the other unit isn't running, refresh the port by closing it if (!NV11Running) { LibraryHandler.ClosePort(); } hopperRunning = false; tHopRec = new Thread(() => ReconnectHopper()); tHopRec.Start(); } // If the NV11 is supposed to be running but the poll fails if (NV11Running && !NV11.DoPoll(textBox1)) { textBox1.AppendText("Lost connection to NV11\r\n"); // If the other unit isn't running, refresh the port by closing it if (!hopperRunning) { LibraryHandler.ClosePort(); } NV11Running = false; tNV11Rec = new Thread(() => ReconnectNV11()); tNV11Rec.Start(); } UpdateUI(); timer1.Enabled = true; while (timer1.Enabled) { Application.DoEvents(); Thread.Sleep(1); // Yield so windows can schedule other threads to run } } //close com port LibraryHandler.ClosePort(); btnRun.Enabled = true; btnHalt.Enabled = false; }