Jump to content

String constants in Melsec Q


Recommended Posts

Posted
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

Posted
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
Guest Guest
Posted
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
Posted (edited)
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
Posted
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...
Guest Guest
Posted
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
Posted
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
Posted
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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...