-
Notifications
You must be signed in to change notification settings - Fork 3
/
MSR100Controller.cs
59 lines (56 loc) · 1.8 KB
/
MSR100Controller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.IO.Ports;
namespace Repos.MSR100Controller
{
public delegate void CardHandler(MagneticCardInfo cardinfo);
public class MSR100Controller : IDisposable
{
public MSR100Controller(string PortName, int baudRate)
{
_SerialPort = new SerialPort(PortName, baudRate);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
public MSR100Controller()
{
_SerialPort = new SerialPort("COM1", 9600);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
private readonly SerialPort _SerialPort;
public event CardHandler OnCardSwiped;
void DataRecived(object sender, SerialDataReceivedEventArgs e)
{
var sp = (SerialPort)sender;
var data = sp.ReadLine();
var cardinfo = ParseData(data);
OnCardSwiped?.Invoke(cardinfo);
}
public static MagneticCardInfo ParseData(string data)
{
data = data.Replace("\r", null);
var cardinfo = new MagneticCardInfo
{
Raw = data
};
var tracks = data.Split('?'); //split end sentinel
if (tracks.Length > 0)
{
cardinfo.Track1.ParseTrack01(tracks[0]);
if (tracks.Length > 1)
{
if (tracks[1].Length <= 40 && tracks[1].Length > 1)
{
cardinfo.Track1.DiscretionaryData = cardinfo.Track2.ParseTrack02(tracks[1]);
}
}
}
return cardinfo;
}
public void Dispose()
{
GC.SuppressFinalize(this);
_SerialPort.Close();
}
}
}