DataMatrix Barcode Strings

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MattRHale
Posts: 4
Joined: 17 Jan 2022, 06:23

DataMatrix Barcode Strings

Post by MattRHale » 17 Jan 2022, 06:43

Hi All,

I'm very new to all of this but AHK seems to be the ideal app for my project. I have a cheap 2D barcode scanner for DataMatrix. When I scan one it gives me a string like this:

50-80022090~05210148402011,05210148402051,05210148402026,05210148401743,05210148401734,05210148402028,05210148401964,05210148402041,05210148402040,05210148402141,05210148402125,05210148402124,05210148401963,05210148401957,05210148402137,05210148402138,05210148402066,05210148402131,05210148402134,05210148402121,05210148401965,05210148401518,05210148402116,05210148402119,05210148402140

The first data set is the model number of a product. Then there's a tilde "~" followed by multiple serial numbers delimited by commas.

I need to produce a script that is triggered by the keystroke of the tilde "~". It should ignore the tilde and everything before it, and start adding the delimited values to an array, removing the commas. The quantity of these values is variable. Then the script should send each array value followed by a "Tab" with a 100ms delay before sending the next one. Then clear the array and initialize to accept the next DataMatrix.

So I know the logic of what I need the script to do, but I am really struggling to understand which sets of commands I should be using. I'm pretty sure I need to build an Array and use StrSplit to delimit the values and insert into the array. Unfortunately I'm unfamiliar with the syntax. I'm not asking for a quick fix. Can someone please point me in the right direction?

Many thanks.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: DataMatrix Barcode Strings

Post by mikeyww » 17 Jan 2022, 07:33

Welcome to this AutoHotkey forum.

Code: Select all

str := "50-80022090~"
     . "05210148402011,05210148402051,05210148402026,05210148401743,05210148401734,05210148402028,"
     . "05210148401964,05210148402041,05210148402040,05210148402141,05210148402125,05210148402124,"
     . "05210148401963,05210148401957,05210148402137,05210148402138,05210148402066,05210148402131,"
     . "05210148402134,05210148402121,05210148401965,05210148401518,05210148402116,05210148402119,"
     . "05210148402140"
~::
For each, sn in StrSplit(SubStr(str, Instr(str, "~") + 1), ",") {
 SendInput {Text}%sn%`t
 Sleep, 100
}
Return

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: DataMatrix Barcode Strings

Post by boiler » 17 Jan 2022, 07:53

I was taking it to mean that the ~ in the input string itself is meant to trigger the routine. How would scanning a barcode cause the incoming character stream to be captured in the string you’re parsing?

MattRHale
Posts: 4
Joined: 17 Jan 2022, 06:23

Re: DataMatrix Barcode Strings

Post by MattRHale » 18 Jan 2022, 04:24

mikeyww wrote:
17 Jan 2022, 07:33
Welcome to this AutoHotkey forum.

Code: Select all

str := "50-80022090~"
     . "05210148402011,05210148402051,05210148402026,05210148401743,05210148401734,05210148402028,"
     . "05210148401964,05210148402041,05210148402040,05210148402141,05210148402125,05210148402124,"
     . "05210148401963,05210148401957,05210148402137,05210148402138,05210148402066,05210148402131,"
     . "05210148402134,05210148402121,05210148401965,05210148401518,05210148402116,05210148402119,"
     . "05210148402140"
~::
For each, sn in StrSplit(SubStr(str, Instr(str, "~") + 1), ",") {
 SendInput {Text}%sn%`t
 Sleep, 100
}
Return
This is a really good start. Thank you very much for this. I will edit and amend appropriately.

MattRHale
Posts: 4
Joined: 17 Jan 2022, 06:23

Re: DataMatrix Barcode Strings

Post by MattRHale » 28 Jan 2022, 11:34

I am getting closer to what I want to achieve, but I'm still really struggling. Here's a more accurate explanation of what I'm trying to do with some relevant AHK terminology...

The datamatrix format will always have the same tilde and comma delimiters. Ideally the script should:

InputHook.Start (to capture the entire data string)
InputHook.Wait(1) (to end the input hook after 1 second of inactivity)

Then I need to parse the string to separate out the substrings.

I'm pretty lost and the forums/YouTube videos assume some knowledge of coding for AHK, which I do not have. Can someone please help me to understand the syntax of what I'm trying to do?

Many thanks.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: DataMatrix Barcode Strings

Post by boiler » 28 Jan 2022, 11:43

You should be able to parse the input string the way mikeyww showed by using InputHook.Input in place of str. In the “for” loop, each piece your refer to as a substring is sn, which you can do with what you want if you don’t want to use SendInput as in his example. You can save it to a file, add it as an element of an array, etc. We need to know what you want to do with the parsed data to give more detailed direction.

MattRHale
Posts: 4
Joined: 17 Jan 2022, 06:23

Re: DataMatrix Barcode Strings

Post by MattRHale » 29 Jan 2022, 12:47

boiler wrote:
28 Jan 2022, 11:43
You should be able to parse the input string the way mikeyww showed by using InputHook.Input in place of str. In the “for” loop, each piece your refer to as a substring is sn, which you can do with what you want if you don’t want to use SendInput as in his example. You can save it to a file, add it as an element of an array, etc. We need to know what you want to do with the parsed data to give more detailed direction.
Thanks for the advice. I'll keep playing with it. Ultimately the serial numbers need to be typed into our stock software. Each SN must be sent as keystrokes and enter or tab pressed before the next SN is sent, and so on. I can work with the delays and control keystrokes. Just struggling with coding for logic. Having a good time with it though. I love software tools like this.

User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: DataMatrix Barcode Strings

Post by AlphaBravo » 30 Jan 2022, 13:10

Code: Select all

;------------------------------------------------
; https://www.autohotkey.com/board/topic/31088-store-user-input-frm-keyboard-into-variable-in-ahk/
Loop 10
	Hotkey, % "$~" A_Index-1, BarCodeHandler, on
Hotkey, % "$~~" , BarCodeHandler, on
Hotkey, % "$~," , BarCodeHandler, on
return
;------------------------------------------------
BarCodeHandler:
Accu .= SubStr(A_ThisHotkey, 0)
if (Strlen(Accu) > 15 && A_TimeSincePriorHotkey < 60)
	SetTimer, TheBigShebang, -60

if (A_TimeSincePriorHotkey > 100)
	Accu := SubStr(A_ThisHotkey, 0)
return
;------------------------------------------------
TheBigShebang:
ToolTip % Accu					; for troubleshooting
if !InStr(Accu, "~")
	return

x := StrSplit(str, "~")
y := StrSplit(x.2, ",")
for i, sn in y
{
	SendInput % sn
	Sleep 50
	Send {Tab}
	Sleep, 100
}
return
;------------------------------------------------

Post Reply

Return to “Ask for Help (v1)”