Solved

Magnetic Stripe Reader/Writer?

Posted on 2001-07-04
13
1,088 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

19 Experts available now in Live!

Get 1:1 Help Now