-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compatible with .net 5 and .net standard 2.0
- Loading branch information
Showing
8 changed files
with
161 additions
and
139 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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
namespace Repos.MSR100Controller; | ||
public interface ITRack | ||
namespace Repos.MSR100Controller | ||
{ | ||
string Raw { get; set; } | ||
string PAN { get; set; } | ||
DateTime ExpirationDate { get; set; } | ||
string ServiceCode { get; set; } | ||
string DiscretionaryData { get; set; } | ||
public interface ITRack | ||
{ | ||
string Raw { get; set; } | ||
string PAN { get; set; } | ||
System.DateTime ExpirationDate { get; set; } | ||
string ServiceCode { get; set; } | ||
string DiscretionaryData { get; set; } | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,57 +1,59 @@ | ||
global using System; | ||
using System; | ||
using System.IO.Ports; | ||
namespace Repos.MSR100Controller; | ||
public delegate void CardHandler(MagneticCardInfo cardinfo); | ||
public class MSR100Controller : IDisposable | ||
namespace Repos.MSR100Controller | ||
{ | ||
public MSR100Controller(string PortName, int baudRate) | ||
public delegate void CardHandler(MagneticCardInfo cardinfo); | ||
public class MSR100Controller : IDisposable | ||
{ | ||
_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 | ||
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) | ||
{ | ||
Raw = data | ||
}; | ||
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 | ||
var tracks = data.Split('?'); //split end sentinel | ||
|
||
if (tracks.Length > 0) | ||
{ | ||
cardinfo.Track1.ParseTrack01(tracks[0]); | ||
|
||
if (tracks.Length > 1) | ||
if (tracks.Length > 0) | ||
{ | ||
if (tracks[1].Length <= 40 && tracks[1].Length > 1) | ||
cardinfo.Track1.ParseTrack01(tracks[0]); | ||
|
||
if (tracks.Length > 1) | ||
{ | ||
cardinfo.Track1.DiscretionaryData = cardinfo.Track2.ParseTrack02( tracks[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(); | ||
} | ||
return cardinfo; | ||
} | ||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
_SerialPort.Close(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
namespace Repos.MSR100Controller; | ||
public class MagneticCardInfo | ||
using MSR_100_Magnetic_Stripe_Parser; | ||
|
||
namespace Repos.MSR100Controller | ||
{ | ||
public MagneticCardInfo() | ||
public class MagneticCardInfo | ||
{ | ||
Track1 = new Track01Info(); | ||
Track2 = new Track02Info(); | ||
public MagneticCardInfo() | ||
{ | ||
Track1 = new Track01Info(); | ||
Track2 = new Track02Info(); | ||
} | ||
public Track01Info Track1 { get; set; } | ||
public Track02Info Track2 { get; set; } | ||
public string Raw { get; set; } | ||
} | ||
public Track01Info Track1 { get; set; } | ||
public Track02Info Track2 { get; set; } | ||
public string Raw { get; set; } | ||
} |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
namespace Repos.MSR100Controller; | ||
public class Track01Info : ITRack | ||
using Repos.MSR100Controller; | ||
namespace MSR_100_Magnetic_Stripe_Parser | ||
{ | ||
public char FormatCode { get; set; } | ||
public string LastName { get; set; } | ||
public string FirstName { get; set; } | ||
public string Name { get; set; } | ||
public string Raw { get; set; } | ||
public string PAN { get; set; } | ||
public DateTime ExpirationDate { get; set; } | ||
public string ServiceCode { get; set; } | ||
public string DiscretionaryData { get; set; } | ||
public class Track01Info : ITRack | ||
{ | ||
public char FormatCode { get; set; } | ||
public string LastName { get; set; } | ||
public string FirstName { get; set; } | ||
public string Name { get; set; } | ||
public string Raw { get; set; } | ||
public string PAN { get; set; } | ||
public System.DateTime ExpirationDate { get; set; } | ||
public string ServiceCode { get; set; } | ||
public string DiscretionaryData { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
namespace Repos.MSR100Controller; | ||
public class Track02Info : ITRack | ||
using Repos.MSR100Controller; | ||
namespace MSR_100_Magnetic_Stripe_Parser | ||
{ | ||
public string Raw { get; set; } | ||
public string PAN { get; set; } | ||
public DateTime ExpirationDate { get; set; } | ||
public string ServiceCode { get; set; } | ||
public string DiscretionaryData { get; set; } | ||
public class Track02Info : ITRack | ||
{ | ||
public string Raw { get; set; } | ||
public string PAN { get; set; } | ||
public System.DateTime ExpirationDate { get; set; } | ||
public string ServiceCode { get; set; } | ||
public string DiscretionaryData { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,27 +1,30 @@ | ||
namespace Repos.MSR100Controller; | ||
public static class TrackParsers | ||
using MSR_100_Magnetic_Stripe_Parser; | ||
namespace Repos.MSR100Controller | ||
{ | ||
public static void ParseTrack01(this Track01Info cardinfoTrack1, string tracks0) | ||
public static class TrackParsers | ||
{ | ||
cardinfoTrack1.Raw = tracks0; | ||
if (tracks0.Length <= 79) | ||
public static void ParseTrack01(this Track01Info cardinfoTrack1, string tracks0) | ||
{ | ||
var fieldsTrack01 = tracks0.Substring(2).Split('^'); | ||
cardinfoTrack1.FormatCode = tracks0[1]; | ||
cardinfoTrack1.PAN = fieldsTrack01[0]; | ||
DataUtils.SetNameData(ref cardinfoTrack1, fieldsTrack01[1]); | ||
DataUtils.SetDiscretionaryData(ref cardinfoTrack1, fieldsTrack01); | ||
cardinfoTrack1.Raw = tracks0; | ||
if (tracks0.Length <= 79) | ||
{ | ||
var fieldsTrack01 = tracks0.Substring(2).Split('^'); | ||
cardinfoTrack1.FormatCode = tracks0[1]; | ||
cardinfoTrack1.PAN = fieldsTrack01[0]; | ||
DataUtils.SetNameData(ref cardinfoTrack1, fieldsTrack01[1]); | ||
DataUtils.SetDiscretionaryData(ref cardinfoTrack1, fieldsTrack01); | ||
} | ||
} | ||
public static string ParseTrack02(this Track02Info cardinfoTrack2, string tracks1) | ||
{ | ||
cardinfoTrack2.Raw = tracks1; | ||
var fieldsTrack02 = tracks1.Substring(1).Split('='); | ||
cardinfoTrack2.PAN = fieldsTrack02[0]; | ||
var year = System.Convert.ToInt16(fieldsTrack02[1].Substring(0, 2)) + 2000; | ||
var month = System.Convert.ToInt16(fieldsTrack02[1].Substring(2, 2)); | ||
cardinfoTrack2.ExpirationDate = new System.DateTime(year, month, 1); | ||
cardinfoTrack2.ServiceCode = fieldsTrack02[1].Substring(4, 3); | ||
return fieldsTrack02[1].Substring(7); | ||
} | ||
} | ||
public static string ParseTrack02(this Track02Info cardinfoTrack2, string tracks1) | ||
{ | ||
cardinfoTrack2.Raw = tracks1; | ||
var fieldsTrack02 = tracks1.Substring(1).Split('='); | ||
cardinfoTrack2.PAN = fieldsTrack02[0]; | ||
var year = Convert.ToInt16(fieldsTrack02[1].Substring(0, 2)) + 2000; | ||
var month = Convert.ToInt16(fieldsTrack02[1].Substring(2, 2)); | ||
cardinfoTrack2.ExpirationDate = new DateTime(year, month, 1); | ||
cardinfoTrack2.ServiceCode = fieldsTrack02[1].Substring(4, 3); | ||
return fieldsTrack02[1].Substring(7); | ||
} | ||
} |