-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cory Todd
committed
Aug 24, 2015
1 parent
7001ce0
commit a1072ec
Showing
20 changed files
with
1,118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Apex7000_BillValidator\bin\* | ||
Apex7000_BillValidator\obj\* | ||
Apex7000_BillValidator_Test\bin\* | ||
Apex7000_BillValidator_Test\obj\* | ||
Apex7000_BillValidator/obj/ | ||
Apex7000_BillValidator_Test/bin/ | ||
Apex7000_BillValidator_Test/obj/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{735220BB-D8E8-4E21-80D9-AEA6E41B13ED}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Apex7000_BillValidator</RootNamespace> | ||
<AssemblyName>Apex7000_BillValidator</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="ApexValidator.cs" /> | ||
<Compile Include="Commands.cs" /> | ||
<Compile Include="DataContracts.cs" /> | ||
<Compile Include="Events.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.IO; | ||
using System.IO.Ports; | ||
using System.Globalization; | ||
|
||
namespace Apex7000_BillValidator | ||
{ | ||
public partial class ApexValidator | ||
{ | ||
private SerialPort port = null; | ||
private byte lastResponse = Response.Idle; | ||
private COMPort comPort = COMPort.COM1; | ||
private readonly object mutex = new object(); | ||
public bool isConnected = false; | ||
private int reconnectAttempts = 0; | ||
private DateTime escrowTimeout = DateTime.MinValue; | ||
private CultureInfo currentCulture; | ||
private CurrencyMap currentCurrencyMap; | ||
|
||
|
||
public ApexValidator(string comm) | ||
{ | ||
comPort = (COMPort)Enum.Parse(typeof(COMPort), comm); | ||
} | ||
|
||
public ApexValidator(COMPort comm) | ||
{ | ||
comPort = comm; | ||
} | ||
|
||
public ApexValidator(COMPort comm, string culture) | ||
{ | ||
comPort = comm; | ||
currentCulture = new CultureInfo(culture); | ||
} | ||
|
||
public ApexValidator(COMPort comm, CultureInfo culture) | ||
{ | ||
comPort = comm; | ||
currentCulture = culture; | ||
} | ||
|
||
public ApexValidator(string comm, CultureInfo culture) | ||
{ | ||
comPort = (COMPort)Enum.Parse(typeof(COMPort), comm); | ||
currentCulture = culture; | ||
} | ||
|
||
|
||
public void Close() | ||
{ | ||
port.Close(); | ||
} | ||
|
||
public void Connect() | ||
{ | ||
//if (port != null) | ||
// port.DataReceived -= port_DataReceived; | ||
|
||
port = new SerialPort(comPort.ToString(), 9600, Parity.Even, 7, StopBits.One); | ||
port.ReadTimeout = 500; | ||
//port.DataReceived += port_DataReceived; | ||
|
||
try | ||
{ | ||
port.Open(); | ||
} | ||
catch(Exception e) | ||
{ | ||
/* Handle this better? */ | ||
if (OnError != null) | ||
{ | ||
OnError(this, ErrorTypes.PortError); | ||
} | ||
else | ||
{ | ||
throw e; | ||
} | ||
} | ||
|
||
byte[] wakeup = Request.BaseMessage; | ||
|
||
lock(mutex) | ||
{ | ||
Write(wakeup); | ||
Read(); | ||
} | ||
|
||
Thread ackThread = new Thread((fn) => | ||
{ | ||
while(true) | ||
{ | ||
lock (mutex) | ||
{ | ||
Write(Request.BaseMessage); | ||
Read(); | ||
} | ||
|
||
Thread.Sleep(200); | ||
} | ||
}); | ||
ackThread.IsBackground = true; | ||
ackThread.Start(); | ||
|
||
} | ||
|
||
private void Reconnect() | ||
{ | ||
lock(mutex) | ||
{ | ||
port = new SerialPort(comPort.ToString(), 9600, Parity.Even, 7, StopBits.One); | ||
port.ReadTimeout = 500; | ||
|
||
try | ||
{ | ||
port.Open(); | ||
} | ||
catch (Exception e) | ||
{ | ||
//return; | ||
if (OnError != null) | ||
{ | ||
OnError(this, ErrorTypes.PortError); | ||
} | ||
} | ||
|
||
byte[] wakeup = Request.BaseMessage; | ||
|
||
lock (mutex) | ||
{ | ||
Write(wakeup); | ||
Read(); | ||
} | ||
} | ||
} | ||
|
||
private void Write(byte[] data) | ||
{ | ||
data = Checksum(data); | ||
|
||
if (port != null) | ||
{ | ||
try | ||
{ | ||
port.Write(data, 0, data.Length); | ||
reconnectAttempts = 0; | ||
} | ||
catch(Exception e) | ||
{ | ||
isConnected = false; | ||
Thread.Sleep(1000); | ||
reconnectAttempts++; | ||
if (reconnectAttempts < 3) | ||
{ | ||
Reconnect(); | ||
Write(data); | ||
} | ||
else | ||
{ | ||
reconnectAttempts = 0; | ||
if (OnError != null) | ||
{ | ||
OnError(this, ErrorTypes.WriteError); | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
if (OnError != null) | ||
{ | ||
OnError(this, ErrorTypes.PortError); | ||
} | ||
} | ||
} | ||
|
||
private byte[] Checksum(byte[] msg) | ||
{ | ||
List<byte> tmp = new List<byte>(msg); | ||
byte checksum = (byte)(msg[1] ^ msg[2]); | ||
for(int i = 3; i < msg.Length - 1; i++) | ||
{ | ||
checksum ^= msg[i]; | ||
} | ||
|
||
tmp.Add(checksum); | ||
return tmp.ToArray(); | ||
} | ||
|
||
private void Read() | ||
{ | ||
if (port.IsOpen) | ||
{ | ||
int waitCount = 0; | ||
while (port.BytesToRead < 11) | ||
{ | ||
waitCount++; | ||
if (waitCount >= 5) | ||
{ | ||
/* Don't report error if validator is in accepting state as this can take some time */ | ||
//if (OnError != null && lastResponse != Response.Accepting) | ||
//{ | ||
OnError(this, ErrorTypes.Timeout); | ||
//} | ||
|
||
return; | ||
} | ||
|
||
Thread.Sleep(100); | ||
} | ||
|
||
byte[] buffer = new byte[11]; | ||
|
||
port.Read(buffer, 0, 11); | ||
|
||
byte t3 = buffer[3]; | ||
byte t4 = buffer[4]; | ||
|
||
if ((t3 & 1) == Response.Idle) | ||
{ | ||
//Only if the box has been recently removed | ||
if ((t4 & 0x10) == Response.CassetteRemoved) | ||
{ | ||
if (lastResponse != Response.CassetteRemoved) | ||
{ | ||
lastResponse = Response.CassetteRemoved; | ||
if (CashboxRemoved != null) | ||
{ | ||
CashboxRemoved(this, null); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
|
||
//Normal idle response | ||
if (lastResponse == Response.CassetteRemoved) | ||
{ | ||
if (CashboxAttached != null) | ||
{ | ||
CashboxAttached(this, null); | ||
} | ||
} | ||
|
||
if (!isConnected) | ||
{ | ||
isConnected = true; | ||
if (PowerUp != null) | ||
{ | ||
PowerUp(this, null); | ||
} | ||
} | ||
|
||
lastResponse = Response.Idle; | ||
} | ||
|
||
Write(Request.Ack); | ||
} | ||
if ((t3 & 2) == Response.Accepting) | ||
{ | ||
if (lastResponse != Response.Accepting) | ||
{ | ||
lastResponse = Response.Accepting; | ||
} | ||
} | ||
if ((t3 & 4) == Response.Escrow) | ||
{ | ||
|
||
if (lastResponse != Response.Escrow) | ||
{ | ||
if (escrowTimeout == DateTime.MinValue) | ||
{ | ||
escrowTimeout = DateTime.Now; | ||
} | ||
else | ||
{ | ||
TimeSpan ts = DateTime.Now - escrowTimeout; | ||
if (ts.TotalSeconds >= 10) | ||
{ | ||
//Let's reconnect and make sure everything is still good | ||
Reconnect(); | ||
Reject(); | ||
} | ||
} | ||
|
||
lastResponse = Response.Escrow; | ||
|
||
byte bill = (byte)((buffer[5] & 0x38) >> 3); | ||
|
||
if (OnEscrow != null) | ||
OnEscrow(this, BillParser.getDenomFromByte(bill, currentCulture)); | ||
} | ||
} | ||
if ((t3 & 8) == Response.Stacking) | ||
{ | ||
if (lastResponse != Response.Stacking) | ||
{ | ||
lastResponse = Response.Stacking; | ||
|
||
if (BillStacked != null) | ||
{ | ||
BillStacked(this, null); | ||
} | ||
} | ||
} | ||
if ((t3 & 0x20) == Response.Returned) | ||
{ | ||
if (lastResponse != Response.Returned) | ||
{ | ||
//This screws with the box when it is removed | ||
//lastResponse = Response.Returned; | ||
Write(Request.Ack); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.