Join us today!
Find 5 strings with one fonction
I need fonction or méthode tips for verify New string with other 5 strings
Simplest and fasted way to do this is:
FUNCTION F_ContainsString : BOOL VAR_IN_OUT StringArray : ARRAY [*] OF T_MaxString; END_VAR VAR_IN_OUT CONSTANT Value : T_MaxString; END_VAR VAR i : DINT; END_VAR ============================================================= FOR i := LOWER_BOUND(StringArray, 1) TO UPPER_BOUND(StringArray,1) DO F_ContainString := StringArray[i] = Value; IF F_ContainString THEN RETURN; END_IF END_FOR
I have also library called TwinCAT Dynamic Collections.
There's a data structure called a Set, it only stores unique values. You can use the hash set (FB_Hash_Set) or tree set (FB_Tree_Set) to store existing QR codes then check of they exist in the set. It's dynamic so you can store as many QR codes as you need for your application. Heck, you can use it to store all the QR codes in your inventory (as long as you have less than 2 billion items) at run-time and find then extremely fast.
Here's some example code:
PROGRAM MAIN VAR CONSTANT QR_CODE_LENGTH : __UXINT := 4; END_VAR VAR eState : (E_INIT, E_RUN); bDoesQrCodeExist : BOOL; sNewQrCode : STRING(QR_CODE_LENGTH) := '0011'; fbQrCodeSet : TcDynCollections.FB_Hash_Set(0); arsQrCodes : ARRAY[0..4] OF STRING(QR_CODE_LENGTH) := ['1001', '0110', '1101', '1110', '0101']; i : TcDynCollections.T_Position; END_VAR ============================================================= CASE eState OF E_INIT: // Add existing QR codes to set. FOR i := 0 TO 4 DO fbQrCodeSet.Insert(arsQrCodes[i]); END_FOR eState := E_RUN; E_RUN: // Check if QR Code is new by checking of it's contained in the set. bDoesQrCodeExist := fbQrCodeSet.Contains(sNewQrCode); END_CASE
You can even take advantage of a Map/Dictionary/Associative Array (FB_Hash_Map or FB_Tree_Map) in the library to store metadata along with your QR code in the sense that Key = QR Code and Value = Struct/Function Block containing metadata.
Hello,
Perhaps I miss something, could you be a little more specific ?
Alex
In the programme i receive a New QR CODE type String , and need to compare with 4 existing QR code , are same or not
I need fonction or méthode can Do this tips liké fin(;)
Hello, It's just a function which do the job 🙂
If it's only 4/5 string then :
FUNCTION StringCompare : BOOL VAR_INPUT StrComp :STRING(8); END_VAR VAR Str1 :STRING(8) :='01234567'; Str2 :STRING(8) :='12345678'; Str3 :STRING(8) :='23456789'; Str4 :STRING(8) :='34567890'; Str5 :STRING(8) :='45678901'; END_VAR IF StrComp = str1 THEN StringCompare:=TRUE; ELSIF StrComp = str2 THEN StringCompare:=TRUE; ELSIF StrComp = str3 THEN StringCompare:=TRUE; ELSIF StrComp = str4 THEN StringCompare:=TRUE; ELSIF StrComp = str4 THEN StringCompare:=TRUE; ELSE StringCompare:=FALSE; END_IF
In you main call the function :
IF Compare THEN Compare:=FALSE; Result := StringCompare(StrComp:=Str2comp); END_IF
where Str2comp is your QR code
my 2 cts,
Hello Alex,
https://github.com/runtimevic
https://github.com/TcMotion
https://www.youtube.com/playlist?list=PLEfi_hUmmSjFpfdJ6yw3B9yj7dWHYkHmQ
https://github.com/VisualPLC
- 17 Forums
- 267 Topics
- 924 Posts
- 1 Online
- 737 Members