Jump to content

Dealing with errors


Recommended Posts

Below is a script which is reading data from a serial card swipe ... The data comes across as %FIRST_NAME^TEN_DIGIT_CARD_NUM^1^1^2F? ... Everything is working good ... I can write the first name and ten digit card number to a csv file, display the person's first name in a label, etc. The problem arises when is the reader mis-reads the card or the card isn't fully slide through the reader. I get garbage or %E? or ;E? or somethings nothing. How do I go about if garbage is read to display an error message then restart the loop so that the person can re-swipe their card?

I was thinking is there is a way to error out and restart if the data that comes over is anything other then %FIRST_NAME^TEN_DIGIT_CARD_NUM^1^1^2F?? I'm just not sure!

Thanks for any help!

MePH

#include <CommMG.au3>
#include <File.au3>
#include <GuiConstants.au3>
HotKeySet("+!d", "_Terminate");Shift-Alt-D

; Read card swipe file
$CardFile = FileOpen(@ScriptDir & "\CardSwipes.csv", 1)
$CardSize = FileGetSize(@ScriptDir & "\CardSwipes.csv")
If $CardSize = 0 Then
    FileWriteLine($CardFile, "CardID,PatronFirstName,DateSwiped,TimeSwiped")
EndIf

; Opens communications port
$sErr = 1
_CommSetport(1,$sErr,9600,8,0,1,0)
_CommGetLine(@CR,0, 0)

; Waits for swipe data from card reader
While 1
    $String = _CommGetLine(@CR,0,0)
    $Pattern1 = '\d{10}';Returns ten digit card number
    $Pattern2 = '\%(.*?)\^';Returns first name
    $SwipeResults1 = StringRegExp($String,$Pattern1, 3)
    $SwipeResults2 = StringRegExp($String,$Pattern2, 3)
    FileWriteLine($CardFile, $SwipeResults1[0] & "," & $SwipeResults2[0] & "," & @MON & "/" & @MDAY & "/" & @YEAR & "," & @HOUR & ":" & @MIN & ":" & @SEC)
    GUICreate("Title",500,100,-1,-1,$WS_POPUP)
    GUISetBkColor(0x7d2e34)
    GUICtrlSetState(-1, $gui_disable)
    GUICtrlCreateLabel($SwipeResults2[0] & ", Thank you for taking part in our drawing!", 5, 5, 490, 90)
    GUICtrlSetFont(-1, 16, 800, -1, "Verdana")
    GUICtrlSetColor(-1, 0xd2ac3f)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUISetState(@SW_SHOW)
    Sleep(5000)
    GUIDelete()
WEnd

; Terminates application
Func _Terminate()
    _Commcloseport()
    FileClose($CardFile)
    Exit 0
EndFunc
Link to comment
Share on other sites

Below is a script which is reading data from a serial card swipe ... The data comes across as %FIRST_NAME^TEN_DIGIT_CARD_NUM^1^1^2F? ... Everything is working good ... I can write the first name and ten digit card number to a csv file, display the person's first name in a label, etc. The problem arises when is the reader mis-reads the card or the card isn't fully slide through the reader. I get garbage or %E? or ;E? or somethings nothing. How do I go about if garbage is read to display an error message then restart the loop so that the person can re-swipe their card?

I was thinking is there is a way to error out and restart if the data that comes over is anything other then %FIRST_NAME^TEN_DIGIT_CARD_NUM^1^1^2F?? I'm just not sure!

Thanks for any help!

MePH

#include <CommMG.au3>
#include <File.au3>
#include <GuiConstants.au3>
HotKeySet("+!d", "_Terminate");Shift-Alt-D

; Read card swipe file
$CardFile = FileOpen(@ScriptDir & "\CardSwipes.csv", 1)
$CardSize = FileGetSize(@ScriptDir & "\CardSwipes.csv")
If $CardSize = 0 Then
    FileWriteLine($CardFile, "CardID,PatronFirstName,DateSwiped,TimeSwiped")
EndIf

; Opens communications port
$sErr = 1
_CommSetport(1,$sErr,9600,8,0,1,0)
_CommGetLine(@CR,0, 0)

; Waits for swipe data from card reader
While 1
    $String = _CommGetLine(@CR,0,0)
    $Pattern1 = '\d{10}';Returns ten digit card number
    $Pattern2 = '\%(.*?)\^';Returns first name
    $SwipeResults1 = StringRegExp($String,$Pattern1, 3)
    $SwipeResults2 = StringRegExp($String,$Pattern2, 3)
    FileWriteLine($CardFile, $SwipeResults1[0] & "," & $SwipeResults2[0] & "," & @MON & "/" & @MDAY & "/" & @YEAR & "," & @HOUR & ":" & @MIN & ":" & @SEC)
    GUICreate("Title",500,100,-1,-1,$WS_POPUP)
    GUISetBkColor(0x7d2e34)
    GUICtrlSetState(-1, $gui_disable)
    GUICtrlCreateLabel($SwipeResults2[0] & ", Thank you for taking part in our drawing!", 5, 5, 490, 90)
    GUICtrlSetFont(-1, 16, 800, -1, "Verdana")
    GUICtrlSetColor(-1, 0xd2ac3f)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUISetState(@SW_SHOW)
    Sleep(5000)
    GUIDelete()
WEnd

; Terminates application
Func _Terminate()
    _Commcloseport()
    FileClose($CardFile)
    Exit 0
EndFunc
You have done nearly all the work I think so all you need to do is test the whole string.

#include <CommMG.au3>
#include <File.au3>
#include <GuiConstants.au3>
HotKeySet("+!d", "_Terminate");Shift-Alt-D

; Read card swipe file
$CardFile = FileOpen(@ScriptDir & "\CardSwipes.csv", 1)
$CardSize = FileGetSize(@ScriptDir & "\CardSwipes.csv")
If $CardSize = 0 Then
    FileWriteLine($CardFile, "CardID,PatronFirstName,DateSwiped,TimeSwiped")
EndIf

; Opens communications port
$sErr = 1
_CommSetport (1, $sErr, 9600, 8, 0, 1, 0)
_CommGetLine (@CR, 0, 0)

; Waits for swipe data from card reader
While 1
    $String = _CommGetLine (@CR, 0, 0)
    If StringRegExp($String,'\%(.*?)\^\d{10}\^1\^1\^2F\?') Then
        $Pattern1 = '\d{10}';Returns ten digit card number
        $Pattern2 = '\%(.*?)\^';Returns first name
        $SwipeResults1 = StringRegExp($String, $Pattern1, 3)
        $SwipeResults2 = StringRegExp($String, $Pattern2, 3)
        FileWriteLine($CardFile, $SwipeResults1[0] & "," & $SwipeResults2[0] & "," & @MON & "/" & @MDAY & "/" & @YEAR & "," & @HOUR & ":" & @MIN & ":" & @SEC)
        GUICreate("Title", 500, 100, -1, -1, $WS_POPUP)
        GUISetBkColor(0x7d2e34)
        GUICtrlSetState(-1, $gui_disable)
        GUICtrlCreateLabel($SwipeResults2[0] & ", Thank you for taking part in our drawing!", 5, 5, 490, 90)
        GUICtrlSetFont(-1, 16, 800, -1, "Verdana")
        GUICtrlSetColor(-1, 0xd2ac3f)
        GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
        GUISetState(@SW_SHOW)
        Sleep(5000)
        GUIDelete()
    Else
        MsgBox(262144, 'ERROR WITH SWIPE', 'Please try again.', 10)
     ;might be worth adding something here to clear the input buffer because the rror reading the card
     ; might have produced more than one line
    EndIf
    
WEnd

; Terminates application
Func _Terminate ()
    _Commcloseport ()
    FileClose($CardFile)
    Exit 0
EndFunc;==>_Terminate

Edit: Added missing first parameter for StringRegExp as pointed out by MePHiTiC below.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

You have done nearly all the work I think so all you need to do is test the whole string.

#include <CommMG.au3>
#include <File.au3>
#include <GuiConstants.au3>
HotKeySet("+!d", "_Terminate");Shift-Alt-D

; Read card swipe file
$CardFile = FileOpen(@ScriptDir & "\CardSwipes.csv", 1)
$CardSize = FileGetSize(@ScriptDir & "\CardSwipes.csv")
If $CardSize = 0 Then
    FileWriteLine($CardFile, "CardID,PatronFirstName,DateSwiped,TimeSwiped")
EndIf

; Opens communications port
$sErr = 1
_CommSetport (1, $sErr, 9600, 8, 0, 1, 0)
_CommGetLine (@CR, 0, 0)

; Waits for swipe data from card reader
While 1
    $String = _CommGetLine (@CR, 0, 0)
    If StringRegExp('\%(.*?)\^\d{10}\^1\^1\^2F\?') Then
        $Pattern1 = '\d{10}';Returns ten digit card number
        $Pattern2 = '\%(.*?)\^';Returns first name
        $SwipeResults1 = StringRegExp($String, $Pattern1, 3)
        $SwipeResults2 = StringRegExp($String, $Pattern2, 3)
        FileWriteLine($CardFile, $SwipeResults1[0] & "," & $SwipeResults2[0] & "," & @MON & "/" & @MDAY & "/" & @YEAR & "," & @HOUR & ":" & @MIN & ":" & @SEC)
        GUICreate("Title", 500, 100, -1, -1, $WS_POPUP)
        GUISetBkColor(0x7d2e34)
        GUICtrlSetState(-1, $gui_disable)
        GUICtrlCreateLabel($SwipeResults2[0] & ", Thank you for taking part in our drawing!", 5, 5, 490, 90)
        GUICtrlSetFont(-1, 16, 800, -1, "Verdana")
        GUICtrlSetColor(-1, 0xd2ac3f)
        GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
        GUISetState(@SW_SHOW)
        Sleep(5000)
        GUIDelete()
    Else
        MsgBox(262144, 'ERROR WITH SWIPE', 'Please try again.', 10)
    EndIf
    
WEnd

; Terminates application
Func _Terminate ()
    _Commcloseport ()
    FileClose($CardFile)
    Exit 0
EndFunc;==>_Terminate
Martin, thanks for the quick reply :)

The script loads up fine, when I swipe a card however I get the following error:

Test.au3 (21) : ==> Incorrect number of parameters in function call.: 
If StringRegExp('\%(.*?)\^\d{10}\^1\^1\^2F\?') Then 
If ^ ERROR
>Exit code: 1   Time: 4.138

Thanks again!

NM ... Needed to tweak:

If StringRegExp($String, '\%(.*?)\^\d{10}\^1\^1\^2F\?') Then

:( ... Looks like this going to work! Thanks for the help!

MePH

Edited by MePHiTiC
Link to comment
Share on other sites

Martin, thanks for the quick reply :)

The script loads up fine, when I swipe a card however I get the following error:

Test.au3 (21) : ==> Incorrect number of parameters in function call.: 
If StringRegExp('\%(.*?)\^\d{10}\^1\^1\^2F\?') Then 
If ^ ERROR
>Exit code: 1   Time: 4.138

Thanks again!

NM ... Needed to tweak:

If StringRegExp($String, '\%(.*?)\^\d{10}\^1\^1\^2F\?') Then

:( ... Looks like this going to work! Thanks for the help!

MePH

Oops, sorry. But your correction is correct!
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...