Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1200
  • Last Modified:

Magnetic Stripe Reader/Writer?

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
lansing123
Asked:
lansing123
  • 5
  • 4
  • 2
  • +2
1 Solution
 
TimCotteeHead of Software ServicesCommented:
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
 
ScottNeroCommented:
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
 
TimCotteeHead of Software ServicesCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
lansing123Author Commented:
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
 
ScottNeroCommented:
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
 
lansing123Author Commented:
thanks scot but it still not working, its just keep on looping. no response when i swipe the card.
0
 
ScottNeroCommented:
Does the green light come on the reader when you click the form button?
0
 
lansing123Author Commented:
actually when i clicked the button, the yellow and green light turned off then turned on again.
0
 
ScottNeroCommented:
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
 
lansing123Author Commented:
is it normal that all the light(red, yellow and green) are all on?
0
 
ScottNeroCommented:
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
 
DanRollinsCommented:
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
 
Computer101Commented:
Comment from expert accepted as answer

Computer101
E-E Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now