private void RunReadThread() { byte[] cardData = new byte[100]; byte length; byte error; while (_keepAlive) { WBM5000.WBM5000_SensorState(handle, sensorBytes, out error); if (sensorBytes.Count(o => o == 0x31) >= 1 && !_hasCard) { _hasCard = true; WBM5000.WBM5000_MagCardReadData(handle, 0x30, 0x32, out length, cardData); string cardString = Encoding.ASCII.GetString(cardData, 0, length); cardString = cardString.Replace("O", "").Replace("?Y", ""); cardString = Regex.Replace(cardString, @"[^\u0020-\u007E]", string.Empty); // Console.WriteLine(); if (CardInserted != null) { _currentCard = new MagneticCard { Track2 = cardString }; CardInserted(this, new MagneticCardEventArgs(MagneticCardEvent.Insert, _currentCard)); } // WBM5000.WBM5000_SensorState(handle, sensorBytes, out error); PerformForwardEject(); } // we have no sensors active and we believe that we had a card send our card removed else if (sensorBytes.Count(o => o == 0x31) == 0 && _hasCard) { if (_hasCard) { if (CardRemoved != null) { CardRemoved(this, new MagneticCardEventArgs(MagneticCardEvent.Remove, _currentCard)); } _currentCard = null; _hasCard = false; } } Thread.Sleep(500); } }
/// <summary> /// This will process a message and figure out what to do with it once the read thread has finished capturing it /// </summary> /// <param name="messageBytes"></param> private void ProcessMessage(byte[] messageBytes) { Message m = new Message(messageBytes); if (m.Length == 1) { // reporting status or something like that if (EnableDebug) { Console.WriteLine("status reported"); } } else if (m.Length == 4) { // this is likely the results of a request card status operation InsertionStatusMessage insertionStatus = new InsertionStatusMessage(messageBytes); if (_insertedCard != null) { // we have a card if (!insertionStatus.Inserted) { // we didn't see a remove but there is no card so clear it OnCardRemoved(); } } m = insertionStatus; } else { CardReadMessage cardRead = new CardReadMessage(messageBytes); m = cardRead; if (cardRead.ErrorCode == ReadErrorCode.NormalExecution) { if (cardRead.Tracks.Count == 2) { // likely a good insert read _insertedCard = new MagneticCard(); var track1 = cardRead.Tracks.FirstOrDefault(o => o.Track == MagneticTrack.Track1); var track2 = cardRead.Tracks.FirstOrDefault(o => o.Track == MagneticTrack.Track2); var track3 = cardRead.Tracks.FirstOrDefault(o => o.Track == MagneticTrack.Track3); if (track1 != null) { _insertedCard.Track1 = track1.Data; _insertedCard.Track1Status = ConvertToGenericStatus(track1.ErrorCode); } if (track2 != null) { _insertedCard.Track2 = track2.Data; _insertedCard.Track2Status = ConvertToGenericStatus(track2.ErrorCode); } if (track3 != null) { _insertedCard.Track3 = track3.Data; _insertedCard.Track3Status = ConvertToGenericStatus(track3.ErrorCode); } OnCardInserted(); } else if (cardRead.Tracks.Count == 1 && cardRead.Tracks.Any(o => o.Track == MagneticTrack.Track1 && o.ErrorCode == ReadErrorCode.BlankError)) { // this is a card removed send the request of status to confirm _insertedCard = null; // send out message OnCardRemoved(); } } } if (EnableDebug) { Console.WriteLine(m); } }
/// <summary> /// Default ctor /// </summary> /// <param name="cardEvent">type of event</param> /// <param name="card">the card for the event</param> public MagneticCardEventArgs(MagneticCardEvent cardEvent, MagneticCard card) { _magneticCardEvent = cardEvent; _magneticCard = card; }
/// <summary> /// This will process a message and figure out what to do with it once the read thread has finished capturing it /// </summary> /// <param name="messageBytes"></param> private void ProcessMessage(byte[] messageBytes) { if (messageBytes[0] == NORMAL_READ_HEADER) { readData = Encoding.ASCII.GetString(messageBytes); if (EnableDebug) { Console.WriteLine(readData); } return; } Message m = new Message(messageBytes); if (m.Length == 1) { // reporting status or something like that if (EnableDebug) { Console.WriteLine("status reported"); } } else if (m.Length == 3) { // this is likely the results of a request card status operation InsertionStatusMessage1370 insertionStatus = new InsertionStatusMessage1370(messageBytes); if (_insertedCard != null) { // we have a card if (!insertionStatus.Inserted) { // we didn't see a remove but there is no card so clear it OnCardRemoved(); _insertedCard = null; } } else { if (insertionStatus.Inserted) { _insertedCard = new MagneticCard { OverallStatus = string.IsNullOrWhiteSpace(readData) ? MagneticCardStatus.ReadError : MagneticCardStatus.GoodRead, Track2 = readData, Track2Status = string.IsNullOrWhiteSpace(readData) ? MagneticCardStatus.ReadError : MagneticCardStatus.GoodRead }; OnCardInserted(); } } m = insertionStatus; if (EnableDebug) { Console.WriteLine(m); } } }