Solved

Magnetic Stripe Reader/Writer?

Posted on 2001-07-04
13
1,098 Views
Last Modified: 2008-02-01
Can anybody give me a instruction or a sample code on reading and writing to a card using magnetic Stripe Reader/Writer model MSE-630HL using Visual Basic MSCOMM control. I have the command but my problem is how to wait until such time that the user swiped the card.

im willing to add more points.

thanks in advance
0
Comment
Question by:lansing123
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6255842
If you are using the MSComm control then you just need to use the OnComm event:

Private Sub MSComm_OnComm ()
   Select Case MSComm1.CommEvent
   ' Handle each event or error by placing
   ' code below each case statement

   ' Errors
      Case comEventBreak   ' A Break was received.
      Case comEventFrame   ' Framing Error
      Case comEventOverrun   ' Data Lost.
      Case comEventRxOver   ' Receive buffer overflow.
      Case comEventRxParity   ' Parity Error.
      Case comEventTxFull   ' Transmit buffer full.
      Case comEventDCB   ' Unexpected error retrieving DCB]

   ' Events
      Case comEvCD   ' Change in the CD line.
      Case comEvCTS   ' Change in the CTS line.
      Case comEvDSR   ' Change in the DSR line.
      Case comEvRing   ' Change in the Ring Indicator.
      Case comEvReceive   ' Received RThreshold # of
                        ' chars.
      Case comEvSend   ' There are SThreshold number of
                     ' characters in the transmit
                     ' buffer.
      Case comEvEof   ' An EOF charater was found in
                     ' the input stream
   End Select
End Sub

In the comEvReceive section you can then handle the data received.  You don't have to worry about anything else as this event will be fired when there is data in the receive buffer. Of course I would also make sure that you set the RThreshold property to the length of the expected read so that you get it in one receipt otherwise you may have to handle receiving the data in more than one event.
0
 
LVL 1

Expert Comment

by:ScottNero
ID: 6256688
Except there is no "expected read length" with magstripe cards. It could be 1 digit, it could be 20 digits. Better off having a timer that starts when the first character is received. If 100-250 ms goes by and no more characters received, you can assume that's the end of the stream. The magstripe reader will not send any data until it has ALL of the data to send, all in one block.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6256761
True Scott, but I am assuming (possibly wrongly) that this is for an application such as an in-house security or identification system. In this case it is likely that the Data on the card will be of fixed length.
0
 
LVL 1

Author Comment

by:lansing123
ID: 6258312
ok here is my problem.

I have a READ command button on my form. What I want is when I click the READ button, the magnetic stripe reader/writer device will wait for the card to be swiped.

how can i do that in VB?
0
 
LVL 1

Accepted Solution

by:
ScottNero earned 200 total points
ID: 6259997
I went to the web site for the unit you are using and downloaded the interface specs. I then wrote the code below to run with it. I don't have the hardware connected, so I can't be 100% sure it will work properly (i.e. it's not tested) but based on their specs it should work.

Open a blank VB project and paste this into the form code. I am assuming you have a Microsoft COMM control named MSComm and a command button named cmdSwipe on the form. I'm also assuming you are using COM port 1. When you click the button on the form, it resets the reader, then turns the green LED on. You should then swipe the card. It should then pop up a message box containing the Track 1 2 and 3 data contents.

Good luck!


Option Explicit

Private Sub Form_Load()

  MSComm.CommPort = 1
  MSComm.Settings = "9600,n,8,1"
  MSComm.DTREnable = True
  MSComm.RTSEnable = True
  MSComm.Handshaking = comNone
  MSComm.PortOpen = True

End Sub


Private Sub cmdSwipe_Click()

  Dim CardData As String
  Dim FindPos As Integer
  Dim EndPos As Integer
  Dim Track1 As String
  Dim Track2 As String
  Dim Track3 As String

  ' Reset the magstripe reader
  MSComm.Output = Chr$(&H1B) & Chr$(&H61)
  WaitResponse 1 ' Wait 1 second
 
  ' Turn the green LED on
  MSComm.Output = Chr$(&H1B) & Chr$(&H83)
  WaitResponse 1 ' Wait 1 second
 
  ' Tell it to read a card
  MSComm.Output = Chr$(&H1B) & Chr$(&H72)
  CardData = WaitResponse(15) ' Wait 15 seconds for card swipe
 
  FindPos = InStr(1, CardData, Chr$(&H3F) & Chr$(&H1C) & Chr$(&H1B), vbBinaryCompare)
  If FindPos Then
    Select Case Asc(Mid$(CardData, FindPos + 3, 1))
      Case 0
        ' Everything OK, no error
      Case 1
        MsgBox "Card Read Error"
        Exit Sub
      Case 2
        MsgBox "Command Format Error"
        Exit Sub
      Case 4
        MsgBox "Invalid Command Error"
        Exit Sub
      Case 9
        MsgBox "Write error only, this shouldn't happen"
        Exit Sub
    End Select
   
    CardData = Left$(CardData, FindPos - 1)
    FindPos = InStr(1, CardData, Chr$(&H1B) & Chr$(&H73), vbBinaryCompare)
    If FindPos Then
      CardData = Mid$(CardData, FindPos + 2)
       
      ' If we get this far then the swipe happened fine
     
      ' Look for the Track 1 marker
      FindPos = InStr(1, CardData, Chr$(&H1B) & Chr$(&H1), vbBinaryCompare)
      If FindPos Then ' We found Track 1 data
        EndPos = InStr(FindPos + 1, CardData, Chr$(&H1B), vbBinaryCompare)
        If EndPos = 0 Then EndPos = Len(CardData) + 1
        Track1 = Mid$(CardData, FindPos + 2, (EndPos - FindPos) - 2)
      End If
 
      ' Look for the Track 2 marker
      FindPos = InStr(1, CardData, Chr$(&H1B) & Chr$(&H2), vbBinaryCompare)
      If FindPos Then ' We found Track 2 data
        EndPos = InStr(FindPos + 1, CardData, Chr$(&H1B), vbBinaryCompare)
        If EndPos = 0 Then EndPos = Len(CardData) + 1
        Track2 = Mid$(CardData, FindPos + 2, (EndPos - FindPos) - 2)
      End If
 
      ' Look for the Track 3 marker
      FindPos = InStr(1, CardData, Chr$(&H1B) & Chr$(&H3), vbBinaryCompare)
      If FindPos Then ' We found Track 2 data
        EndPos = InStr(FindPos + 1, CardData, Chr$(&H1B), vbBinaryCompare)
        If EndPos = 0 Then EndPos = Len(CardData) + 1
        Track3 = Mid$(CardData, FindPos + 2, (EndPos - FindPos) - 2)
      End If
   
      MsgBox "Track 1: " & Track1 & " Track 2: " & Track2 & " Track 3" & Track3
   
    End If
 
  End If
 
End Sub


Function WaitResponse(ByVal TimeOut As Long) As String

  Dim ResponseChars As String
 
  TimeOut = Timer + TimeOut

  Do While TimeOut > Timer
    DoEvents
    ResponseChars = ResponseChars & MSComm.Input
    ' Check to see if we got a response
    If InStr(1, ResponseChars, Chr$(&H3F) & Chr$(&H1C) & Chr$(&H1B), vbBinaryCompare) Then Exit Do
  Loop
 
  WaitResponse = ResponseChars
 
End Function


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

  MSComm.PortOpen = False

End Sub

0
 
LVL 1

Author Comment

by:lansing123
ID: 6261295
thanks scot but it still not working, its just keep on looping. no response when i swipe the card.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:ScottNero
ID: 6261313
Does the green light come on the reader when you click the form button?
0
 
LVL 1

Author Comment

by:lansing123
ID: 6263564
actually when i clicked the button, the yellow and green light turned off then turned on again.
0
 
LVL 1

Expert Comment

by:ScottNero
ID: 6263656
Well, that shows it's at least doing the reset. Perhaps you might try the "Wait 1 second" timeouts to 3 or 4 seconds? Then click it, wait for the light to come on, then swipe a card.
0
 
LVL 1

Author Comment

by:lansing123
ID: 6263885
is it normal that all the light(red, yellow and green) are all on?
0
 
LVL 1

Expert Comment

by:ScottNero
ID: 6265565
I don't know, I've never actually seen that reader. I don't know if the lights are actually controlled by the unit to indicate bad reads etc. or if they are manually controlled only by the software.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7148084
Hi lansing123,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept ScottNero's comment(s) as an answer.
    *** heroic effort, then asker quit responding alas!

lansing123, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7182073
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now