Sign in to follow this  
Followers 0
Funkmaster

Is this possible?

5 posts in this topic

At my workplace we use a barcode scanner which works by identifying itself as a keyboard and dumping the barcode data as plaintext (followed by a return). We use this primarily to scan VIN numbers from vehicles, and it works great for that. However, I'd like to also be able to scan UPC codes for products we sell and have the system convert them to service codes. The difficult part of this is handled by our software already - when scanning a barcode into the POS system at the appropriate time it will treat the text as a service code. However, our codes are, obviously, not the same as the prodcuts' UPC codes.

What I'd like to be able to do is create a script which will compare incoming text to a list (preferable stored in an easily-edited text file) and, if a match is found, replace the text with a different bit of text, either stored on another line in the text file or the matching line in a second file. Otherwise, the data would be sent unedited. Is this something that can be achieved with autoit? If so, does anyone have a good starting point for working towards this? I'd like to know that it can be done at all before spending hours and hours learning a scripting language for a silly work project and realizing I'm approaching the situation wrong in the first place.

Share this post


Link to post
Share on other sites



You can use an ini file and store the service code as the key and the text you want as the value. (Or however you want to set the data)

Local $scanned[] = ["PEWP", "FFFF", "ABCD", "WOLLY"]

For $i = 0 to UBound($scanned) - 1
    Local $value = IniRead(@ScriptDir & "\Data.ini", "Example", $scanned[$i], 0)

    If ($value <> 0) Then
        MsgBox("", "New Code", "Code for " & $scanned[$i] & " is " & $value)
    Else
        MsgBox("", "Value not found", "Scanned item " & $scanned[$i] & " not set in ini file")
    EndIf
Next

This is the very simple approach. There's must better ways of accessing data files but if you want simple, that's it. I know in the AutoIt beta version they're adding maps so it'll make it even easier to access values in arrays when that comes.

Share this post


Link to post
Share on other sites

Okay, I've done some messing around and come up with a (very simple) script to start with, but hit a snag.

I started with something like this:

while 1
hotkeyset("g", "servicecode")
WEnd
func servicecode()
   send("t")
EndFunc

This was a simple test, and it works fine: While the script is running, pressing g instead types a t. Good start. Next I created two text files, each with a single entry, and moved on to this:

while 1
   fileopen("test.txt")
   fileopen("test2.txt")
hotkeyset(filereadline("test.txt", 1), "servicecode")
WEnd
func servicecode()
   send(filereadline("test2.txt", 1))
EndFunc

With the first file simply containing "g" and the second simply containing "t", and still the script works and g is replaced with t whenever I type it. In addition, I can make test2.txt contain any string and it seems to work as intended (for example, I can change any g press to "it worked").

The next step, however, was to plug in a UPC code in test.txt and the associated service code in test2.txt, then scan the UPC with the barcode while the script runs, and this is where I encountered a problem. It... sort of works, but the output was what I thought at first to be a random jumble of characters. After messing around a bit, I discovered that only the first character of test.txt is important; if test.txt contains "SSS4" and test2.txt contains "itworked," scanning a barcode for SSS4 gives me "4itworkeditworkeditworked," because it's only matching the S and thus finding three copies of it (and the 4 ends up first even though it's the last character, presumably because it takes some tiny amount of time to convert the S, and the 4 doesn't need anything done to it). I find that also typing an S would cause a match.

Is there some way to make hotkeyset use a full string and then only match when the string is entered all at once (as the barcode scanner would do), rather than always matching the first character?

Share this post


Link to post
Share on other sites

Yea, you want to use this UDF

 

Just a heads up, when you use FileOpen you need to store the return value into a variable. The return value is a handle to the opened file and you need to close the file using that handle. It's resource management, if your program crashes it could corrupt, or wipe, the file it opened and then your data will be lost.

Also, you're calling FileOpen (twice) in a while loop, so it's infinitely opening both files and setting the hotkey to that read line.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

4 hours ago, InunoTaishou said:

Yea, you want to use this UDF

 

Just a heads up, when you use FileOpen you need to store the return value into a variable. The return value is a handle to the opened file and you need to close the file using that handle. It's resource management, if your program crashes it could corrupt, or wipe, the file it opened and then your data will be lost.

Also, you're calling FileOpen (twice) in a while loop, so it's infinitely opening both files and setting the hotkey to that read line.

Hotstrings looks like exactly what I need, thanks!

The fileopen call is in the loop on accident, actually, and I noticed/fixed it after making the post. Thanks for the rest of the info, though. I'm actually learning the language by looking at snippets and messing around in the editor to see how certain functions behave, I'm otherwise not familiar with it and adapting knowledge from some very old game scripting languages I've used. The goal is to get the basic functionality working in the absolute most simple way, and then go back and figure out what is done poorly or incorrectly and slowly start fixing everything.

Thanks for the earlier suggestion about using INI files for the data storage as well, by the way. I didn't ignore it! I'm working with .txt files at first just to get the basics hammered out.

edit: A quick test shows that hotstrings will append the new text at the end of the original, rather than gagging the input. For example, plugged in to my existing script with no changes, typing g using a hotkey returns "It worked" but the same script with hotstring returns "gIt worked". When  I stop and think about how this works, this seems obvious. Is there a workaround for this? The barcode scanner will input the entire string at once, very unlike how a human would type a string onto a keyboard, which seems like it would make it easy to separate from normal input but I'm not entirely sure where to start in trying to do that.

edit 2: I see some people getting around this using backspaces, but that won't work for what I'm trying to do since the scanner will always send an {ENTER} after the content of the code, which will pop up a "not found" error in our software and remove focus from the input box, sending those backspaces as well as the replacement text down the drain.

Edited by Funkmaster

Share this post


Link to post
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
Sign in to follow this  
Followers 0