Problem is when one dispenser doesn't have money in it and my program moves to next dispenser, then nothing gets even tried to be dispensed.
However problem seems to be solved by adding at least ~100ms delay before calling PaySpecific.
Code: Select all
static void Main(string[] args)
{
var paylink = new PaylinkCashHandlingDevice();
paylink.Start();
var dispensed = 0;
//Note dispenser is at address 1. It is empty.
dispensed += paylink.Dispense(1, 1); // dispense 1 unit from address 1.
//Hopper is at address 6. This one has coins.
dispensed += paylink.Dispense(1, 6); // dispense 1 unit from address 6
//It should print 1 (one unit should be dispensed from addr 6), but it prints 0.
//Nothing is dispensed.
Console.WriteLine(dispensed);
paylink.Stop();
}
public int Dispense(int count, int dispenserAddress)
{
var oldDispensers = GetDispensers();
var initialStateDispenser = oldDispensers.Where(p => p.UnitAddress == dispenserAddress).FirstOrDefault();
if (initialStateDispenser == null)
{
throw new InvalidOperationException($"Cannot pay out. Dispenser with address {dispenserAddress} was not found.");
}
var index = GetIndex(initialStateDispenser);
var preparationDoubleCheck = AESImhei.SetDispenseQuantity(index, count, initialStateDispenser.Value);
if (preparationDoubleCheck != count * initialStateDispenser.Value)
{
//Clear any pending request.
AESImhei.PaySpecific();
throw new InvalidOperationException($"Cannot pay out. Could not prepare dispenser for pay out. AESImhei.SetDispenseQuantity({index}, {count}, {initialStateDispenser.Value}) is {preparationDoubleCheck}. It should be {count * initialStateDispenser.Value}.");
}
//If I add ~100ms delay here then it works as expected.
//It doesn't seem to work with ~20ms delay.
//Task.Delay(100).Wait();
var totalPayoutAttempt = AESImhei.PaySpecific();
if (totalPayoutAttempt != count * initialStateDispenser.Value)
{
throw new InvalidOperationException($"Paylink just said that it will attempt to pay out something that was not requested. Expected to be paid out: {count * initialStateDispenser.Value}, but AESImhei.PaySpecific() is {totalPayoutAttempt}.");
}
while (AESImhei.LastPayStatus() == AESImhei.PAY_ONGOING)
{
Task.Delay(500).Wait();
}
var newDispensers = GetDispensers();
var newDispenser = newDispensers.Where(p => p.UnitAddress == dispenserAddress).FirstOrDefault();
if (newDispenser == null)
{
throw new InvalidOperationException($"Cannot find out how much was dispensed. Dispenser with address {dispenserAddress} was not found.");
}
var dispensed = newDispenser.Count - initialStateDispenser.Count;
if (dispensed >= 0)
{
return dispensed;
}
else
{
throw new InvalidOperationException($"Unacceptable count was dispensed ({dispensed} units).");
}
}
Without delay:
Code: Select all
Driver Thread Started
15:14:23.747 PC: Driver start up
15:14:23.852 USB: Unit 0, ID: 0x0403 0xde50
15:14:23.852 Description: Genoa USB Hub
15:14:23.852 Serial No: AE000001
15:14:23.868 Type: Revision 4 (USB Powered)
15:14:23.868 Latency: 16
15:14:23.900 Config: Start of download
15:14:23.961 Config: Checked, OK
15:14:23.961 Config: Paylink Checked OK against file "Standard.cfg"
15:14:23.961 Exec: USB task took 60 msec
15:14:23.962 PC: Memory Reset - new protocol
15:14:24.068 DP: ccCoin acceptor 2020103 at ba0 and 3 coins setup
15:14:24.155 DP: CCNet acceptor 120b0e01 at cb0 and 4 coins setup
15:14:24.259 DP: Bill to bill Cassette Hopper S/N 999999, Address 1, Value 500 at df4 setup
15:14:24.297 DP: SCH2-NOENCRYPT Hopper S/N 580328, Address 6, Value 200 at e48 setup
15:14:24.336 DP: SCH2-NOENCRYPT Hopper S/N 580563, Address 3, Value 100 at ea0 setup
15:14:24.371 DP: SCH2-NOENCRYPT Hopper S/N 580376, Address 4, Value 50 at ef8 setup
15:14:24.408 DP: SCH2-NOENCRYPT Hopper S/N 590087, Address 5, Value 50 at f50 setup
15:14:24.445 DP: SCH2-NOENCRYPT Hopper S/N 588208, Address 7, Value 50 at fa8 setup
15:14:24.490 DP: SCH2-NOENCRYPT Hopper S/N 588302, Address 8, Value 50 at 1000 setup
15:14:24.660 DP: Interface memory set up
15:14:24.660 Exec: DP task took 684 msec
15:14:29.728
15:14:29.728 Ver 004.001.012.071-526 Oct 25 2016 11:13:54
15:14:29.728 DP: Application active
15:14:29.728 DP: Interface memory recovery
15:14:29.728 DP: Resending Events
15:14:29.728 DP: Event 000060 for Dispenser 4
15:14:29.728 DP: Turn off barcodes
15:14:29.728 DP: Pay request for 500, (3746205 - 3745705 (2946996060 - 2943249855))
15:14:30.246 DP: 500 to pay, 1 empty
15:14:30.246 DP: 500 left unqueued
15:14:30.246 DP: Cycle End, 0 coins @ 500 status: -1
15:14:30.310 DP: Payout still owes 500, (3746205 - 3745705)
15:14:30.310 DP: Hopper for 500 not available
15:14:30.310 DP: 500 left unqueued
15:14:30.310 DP: Finished - overall status: -1, paid now 3745705
15:14:30.310 Exec: DP task took 66 msec
15:14:30.731 DP: *** DLL has "noticed" pay result!!
15:14:30.755
15:14:30.755 DP: System closedown received
15:14:30.755 DP: No more updates
15:14:30.755
15:14:30.755 DP: Application has gone away
15:14:30.755
15:14:33.615 cctalk: 0 < 00
15:14:33.615 cctalk: 63 > 07
15:14:33.615 cctalk: 12 < 00
15:14:33.615 cctalk: 3 > 07
15:14:33.615 cctalk: Timeout (90) for 163 to ccHopper @ 7
15:14:33.615 ccHopper@7: No response to message
15:14:33.647 ccHopper@7: Re-Identifying hopper
15:14:33.818 ccHopper@7: Hopper count 975 OK
15:14:33.818 DP: SCH2-NOENCRYPT Hopper S/N 588208, Address 7, Value 50 at fa8 updated
Driver Program Closed
15:14:39.755 PC: Driver Exit Requested
15:14:39.828 Genoa Closed
15:14:40.066 Driver Program Closed
15:14:40.066
Driver Thread Exit
Driver Exit
Code: Select all
Driver Thread Started
15:13:29.524 PC: Driver start up
15:13:29.630 USB: Unit 0, ID: 0x0403 0xde50
15:13:29.630 Description: Genoa USB Hub
15:13:29.630 Serial No: AE000001
15:13:29.646 Type: Revision 4 (USB Powered)
15:13:29.646 Latency: 16
15:13:29.678 Config: Start of download
15:13:29.739 Config: Checked, OK
15:13:29.739 Config: Paylink Checked OK against file "Standard.cfg"
15:13:29.739 Exec: USB task took 59 msec
15:13:29.740 PC: Memory Reset - new protocol
15:13:29.846 DP: ccCoin acceptor 2020103 at ba0 and 3 coins setup
15:13:29.935 DP: CCNet acceptor 120b0e01 at cb0 and 4 coins setup
15:13:30.040 DP: Bill to bill Cassette Hopper S/N 999999, Address 1, Value 500 at df4 setup
15:13:30.077 DP: SCH2-NOENCRYPT Hopper S/N 580328, Address 6, Value 200 at e48 setup
15:13:30.114 DP: SCH2-NOENCRYPT Hopper S/N 580563, Address 3, Value 100 at ea0 setup
15:13:30.151 DP: SCH2-NOENCRYPT Hopper S/N 580376, Address 4, Value 50 at ef8 setup
15:13:30.188 DP: SCH2-NOENCRYPT Hopper S/N 590087, Address 5, Value 50 at f50 setup
15:13:30.225 DP: SCH2-NOENCRYPT Hopper S/N 588208, Address 7, Value 50 at fa8 setup
15:13:30.270 DP: SCH2-NOENCRYPT Hopper S/N 588302, Address 8, Value 50 at 1000 setup
15:13:30.441 DP: Interface memory set up
15:13:30.441 Exec: DP task took 686 msec
15:13:40.263
15:13:40.263 Ver 004.001.012.071-526 Oct 25 2016 11:13:54
15:13:40.263 DP: Application active
15:13:40.263 DP: Interface memory recovery
15:13:40.263 DP: Resending Events
15:13:40.263 DP: Event 000060 for Dispenser 1
15:13:40.263 DP: Turn off barcodes
15:13:40.374 DP: Pay request for 500, (3746005 - 3745505 (2946995860 - 2943249855))
15:13:40.895 DP: 500 to pay, 1 empty
15:13:40.895 DP: 500 left unqueued
15:13:40.895 DP: Cycle End, 0 coins @ 500 status: -1
15:13:40.959 DP: Payout still owes 500, (3746005 - 3745505)
15:13:40.959 DP: Hopper for 500 not available
15:13:40.959 DP: 500 left unqueued
15:13:40.959 DP: Finished - overall status: -1, paid now 3745505
15:13:40.959 Exec: DP task took 67 msec
15:13:41.376 DP: *** DLL has "noticed" pay result!!
15:13:41.487 DP: Pay request for 200, (3745705 - 3745505 (2946995560 - 2943249855))
15:13:41.995 DP: Pay 200 (1 @ 200) from SCH2-NOENCRYPT @ 6
15:13:42.043 ccHopper@6: Request to pay 1...
15:13:42.295 ccHopper@6: Final payout status... Event Count 42, Coins: Remaining 0; Paid 1; Unpaid 0
15:13:42.295 DP: 200 paid so far.
15:13:42.295 DP: Cycle End, 1 coins @ 200 status: 1
15:13:42.381 DP: Finished - overall status: 1, paid now 3745705
15:13:42.381 Exec: DP task took 89 msec
15:13:42.826
15:13:42.826 DP: System closedown received
15:13:42.826 DP: No more updates
15:13:42.826
15:13:42.826 DP: Application has gone away
15:13:42.826
15:13:43.946 04:25
Driver Program Closed
15:13:45.757 PC: Driver Exit Requested
15:13:45.832 Genoa Closed
15:13:46.063 Driver Program Closed
15:13:46.063
Driver Thread Exit
Driver Exit