Sign in to follow this  
Followers 0
Guest jaed

String constants in Melsec Q

10 posts in this topic

Could anyone please explain to me how I use strings in the PLC. I want to communicate (over TCP/IP) with a PC program that uses an ASCII (or really Unicode) character protocol. I have a working solution but I don't like the look of it. Each incoming (16-bit) Unicode/ASCII character has to be converted into a WORD variable and the compared to an ASCII character code. After comparing a number och characters, I can recognise the incoming 'command' and takae action. Is there any way to compare a WORD variable to a character constant, e.g. writing the letter 'A' instead of '16#0041'? Or even better, is there a way to compare an array och WORDs with a STRING constant?? Any help would be greatly appreciated... //j

Share this post


Link to post
Share on other sites
Get Q and QnAS Common Instruction Manual SH(NA)080039 and read chapter 6.1.5 (instruction BKCMP or block compare)

Share this post


Link to post
Share on other sites
Thanks, but BKCMP won't take a character string constant input parameter like "Command". The essence of my question is: How can I convert "Xyz" to a WORD array? (When this is done, I can disunite it into an array of 2-byte Unicode chars and do a BKCMP) Please... //j

Share this post


Link to post
Share on other sites
VAL converts character string to 16 bit data DVAL converts character string to 32 bit data The opposite of these is STR and DSTR

Share this post


Link to post
Share on other sites
Yes, but VAL only operates on numbers... What I'm basically looking for, is a conversion function that converts the STRING constant "AA" to the WORD 16#4141 - or a STRING of any length into an ARRAY of WORDs. //j

Share this post


Link to post
Share on other sites
I see... All you need to do is create data table if you didn't do so already: click on Device Memory in Project Data List ("explorer") then right click and select "New...". Choose "Device Memory" and just hit Enter. Now that you have it, simply open it, click on memory location of your choice, hit SPACE on your keyboard to open text entry box and type in your string. Hope this helps... Edited by panic mode

Share this post


Link to post
Share on other sites
Hi jaed, Was this helpfull or maybe I don't quite understand the question? Do you have problem with: - COMPARING strings? - ENTERING string to be compared with received message? - CONVERTING string format so they match (make comparison possible)? - something else...

Share this post


Link to post
Share on other sites
Thanks for the responses, "Panic". It's like I said in the first post, I have a working solution but I don't like the way it looks. I've made a quick'n'dirty solution where I compare text coming in over the ethernet to known commands. The way I do it now is somwthing like: (Assuming I want to see if the incoming command is "Run") I compare InputArray[0] with 16#0052 (to see if the first char is an 'R') I compare InputArray[1] with 16#0075 (to see if the first char is an 'u') I compare InputArray[2] with 16#006E (to see if the first char is an 'n') I "and" these three "eq's" together and, if TRUE, I have found the command "Run" (and the PLC takes the appropriate action.) [Note: The 00-s in the MSB's above is due to the "Unicoding" of the ACSII chars.] This is tedious and really ugly, if you ask me. Sooner or later (as the parsing handles more and more commands) I will make an error when typing these hex codes... Would be nice if I could use the string functions and write "m" on the input of an EQ instead of 16#006D... Or even better, if I could compare string constants to the input array directly. The solution you suggested with permanent strings in memory would solve the problem, that's true. Maybe I'll try that instead. Btw, I have really tried to get a grip on how the strings work in the Q02 but I must say I'm confused. The built-in conversion functions are a nightmare to understand... I guess I'm too stupid... I'm new to PLC's (as if you hadn't noticed). Normally I use C/C++ so it's something of a new world to me, all this. //j

Share this post


Link to post
Share on other sites
Probaily i did not understund the discussion but.. If you use String comparison instruction with index register ? like: LD$ D0Z1 D100Z1 On D0,D1,...n you receive the input data On D100,D101,..n you have the ascii converted constants (i.e. 'RUN') Then on the top and on bottom you create a FOR-NEXT loop just to increment Z1 nidex. This is for crate an ASCII table block comparing. bye Max

Share this post


Link to post
Share on other sites
FYI: I've found a way to do it. Unfortunately it involves mapping two global vars to the same memory area. I have built a function block, non-macro code, which uses two global vars, aside from the input and output var of the block. One global STRING[16] and one ARRAY [1..16] OF WORD are declared at Dxxx The input string constant of the FB is copied into the global STRING var The global WORD array is disunited into the FB's oputput array (with double length) (And I also add the string length to the FB output.) This way I get the string constant converted inta a unicode string in a WORD array. Ugly, not especially safe nor fast but it works. I dislike it but I see no other way... Btw, if I try using the STRING_MOV built-in function, it crashes for strings longer than 16 characters. So sad... End of discussion. //j

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