// 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, int interval) { // setup timer reconnectionTimer.Interval = interval * 1000; // ms // run for number of attempts specified for (int i = 0; i < attempts; i++) { // close com port in case it was open Payout.SSPComms.CloseComPort(); // turn encryption off for first stage Payout.CommandStructure.EncryptionStatus = false; // if the key negotiation is successful then set the rest up if (Payout.OpenComPort(textBox1) && Payout.NegotiateKeys(textBox1)) { Payout.CommandStructure.EncryptionStatus = true; // now encrypting // find the max protocol version this validator supports byte maxPVersion = FindMaxProtocolVersion(); if (maxPVersion >= 6) { Payout.SetProtocolVersion(maxPVersion, textBox1); } else { MessageBox.Show("This program does not support slaves under protocol 6!", "ERROR"); return(false); } // get info from the validator and store useful vars Payout.SetupRequest(textBox1); // check this unit is supported if (!IsUnitValid(Payout.UnitType)) { MessageBox.Show("Unsupported type shown by SMART Payout, this SDK supports the SMART Payout only"); Application.Exit(); return(false); } // inhibits, this sets which channels can receive notes Payout.SetInhibits(textBox1); // enable, this allows the validator to operate Payout.EnableValidator(textBox1); // enable the payout system on the validator Payout.EnablePayout(textBox1); return(true); } // Set 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() { this.Enabled = true; btnRun.Enabled = false; btnHalt.Enabled = true; // Connect to the validators (non-threaded for initial connect) ConnectToSMARTPayout(textBox1); ConnectToHopper(textBox1); // Enable validators Payout.EnableValidator(); Hopper.EnableValidator(); // While app 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 && !hopperConnecting && !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 (!payoutRunning) { LibraryHandler.ClosePort(); } hopperRunning = false; // Create and start a reconnection thread, this allows this loop to continue executing // and polling the other validator tHopRec = new Thread(() => ReconnectHopper()); tHopRec.Start(); } // Same as above but for the Payout if (payoutRunning && !payoutConnecting && !Payout.DoPoll(textBox1)) { textBox1.AppendText("Lost connection to SMART Payout\r\n"); // If the other unit isn't running, refresh the port by closing it if (!hopperRunning) { LibraryHandler.ClosePort(); } payoutRunning = false; // Create and start a reconnection thread, this allows this loop to continue executing // and polling the other validator tSPRec = new Thread(() => ReconnectPayout()); tSPRec.Start(); } UpdateUI(); timer1.Enabled = true; while (timer1.Enabled) { Application.DoEvents(); } } btnRun.Enabled = true; btnHalt.Enabled = false; }