Jump to content

How to compare strings in unilogic Ladder


Recommended Posts

Hi all,

I've been spending almost one day looking how I could compare to ASCII strings without success unfortunately.  The idea is to compare a string from the HMI (username) with a String inside a user account data table loaded into a struct row after row.

This is just to be able to make a kind of login function with user and password protection.



Link to comment
Share on other sites

Hi, first of all, thank you for your feedback.


A password element has been implemented and it will be in version 1.4 of UniLogic, so that saves you the trouble of comparing strings just for password. (The new HMI password element lets you define a set of actions for Fail and/or success of password entry).



As for comparing strings in ladder, you are right, there is no built in function for comparing 2 strings (So I'll ask for this feature), but it does not mean that you can't do it...

Assuming that you have 2 strings then there are 2 solution:


Solution 1:

* Compare strings length

* if lengths are equal then use "Find Substring" of 1 string on another. If the result is 0 (meaning string1 was found on index 0 on string2) then both strings are equal.... I don't see any option that this number will be possitive. It can only be 0 or -1 (meaning not found), since boths strings has the same length



Solution 2:

* Use Find Substring of String1 on String2, and then use Find Sonstring of String2 on String1.... If both results are 0, then strings are equal. (If the first result gives you a negative number, or any number greater than 0 then strings are not equal, and you don't need to do the second comparison).

  • Upvote 2
Link to comment
Share on other sites

OK, Thanks for this solution, It's not perfect but it should work and I don't need to compare a lot of strings in my project. 


I already tried to used the password widget in the HMI but i did not find any way to compare the string with a data table string. Because you have to compare the string with all row of the data table. The only way is to load the row one by one into a struct and compare them with the HMI string: so how could I do that from the HMI….


Another problem I have which is more important at this stage of the project:

It appears that after downloading the app to the PLC, from time to time some tags disappeared from unilogic (especially the timers tags). All is ok inside the unistream (all tags present in uniapps) but some of them might be removed after download from unilogic and I have to create them back manually. I did not succeed to find which action make this tags disappeared. I'm currently using unilogic V1.2.5 from your website. Does anyone got the same problem ?


Best Regards


Link to comment
Share on other sites



You cannot set the password data source to be a data table (and even if you could, you would have still needed to give it the index of the row, which is just like reading a row and then comparing the strings).


As I understand from your post, your table has a list of passwords and you iterate the table in order to find a match... For the experience of Web and also applications that requires logon, passwords are usually also come with a user name (You enter a username and then you enter its only, and only one, matching password).


This gives you a way of reading the right row from the Data Table in order to compare it with the password… but it might be a different concept from what you are trying to achieve.

Link to comment
Share on other sites

HI again, 


Yes, this is approximately what i want to do. to be more clear:

I have a data table containing username and password (10 row: 10 username with 1 password for each username)


What i Want to do now is:

-have an HMI on which you type in your username and password and press a button to start the check procedure

-it load the first row of the data table into a struct and try first to find a match of username. 

-if there is a match of username, the idea is to check the password and give a feedback to the user depending on the result: (username not exist, wrong password …..)

-if there is no match with the first row, it load the second row into the struct and do the same procedure. 


It does not sound very complex but really the "compare string" function would be perfect for this job. 


In the meantime, I figure out what was going on with the disappearing tags, I upgraded unlogic to 1,3, rev10 and all tags came back automatically. 


Can you let me know when the 1,4 version of unlogic will be available and where I can download the latest version available. only 1.2.5 is available on this website but I got the 1.3 rev 10 from my PLC supplier. 


Best Regards


Link to comment
Share on other sites

I would do it differently... On the Data Entry Complete of the username textbox, I will go and read the matching password from the Data Table.

This way I'm not doing the itaration when the user presses the buttuon, but when he enters the username.


The only drawback is that he must enter the username first and only then enter the password (or else the password box will compare it with the wrong password).



Version 1.4 is not yet available, but I will let you know (or your supplier will) as soon as it will be.


As for the probelm with the missing tags.


Please note that since the missing tags have came back, then there is a chance that if you have created, while using version 1.2.5, tags with the same name as the tags that disappeared, then:

When using version 1.3, you will experience Tags with duplicated names (2 or more tags with the same name): the old one that disappeared and the replacement you have created.


In this case, if you click on Compile, then you will get some "weird" compile errors (Please test  your application).

In this situation, just go and delete the duplicated tags.

If you sort the tags by name then you will be able to see better what names are duplicated (Also do it on the timers, where you said that you had some timers gone).

You should be able to delete/remove all the duplicated tags except the one that is being used by the program.



Sorry for the inconvenience

Link to comment
Share on other sites

@ Cara, that's a good news. let see how long it takes to implement this feature. 

@R&D Guru: that's a very good idea ! I succeed to make it work already with the solution 1 explained above so it is one the good way. I will try to make it more simple creating a UDFB to compare strings. 


Regarding the tags, I don't really know what happened but this is OK with the new version so it's fine for me. 

Thanks one more time for your help and very quick reply. 



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...