String to Word Conversion (Codesys)

EnterDownload

Member
Join Date
May 2015
Location
NY
Posts
12
Hello All

Im using a FESTO PLC with Codesys.

I have a string, named "Program1", which Im trying to convert to a datatype of Word.

Im not sure if the convert function WString_To_Word is the correct function I should be using. When I use WString_to_Word function,

e.g. myTestWord := WString_to_Word("Program1"), I get an output of "0" for myTestWord

myTestWord is a Word data type.

Can someone tell me what Im doing wrong? and how to fix this so that I may get the Word representation of the String ("Program1")?


thanks very much

J
 
SOLVED : String to Word Conversion

Thanks for pointing me in the right direction danatomega!


VAR
text : STRING[5]:='hello';
char : ARRAY[0..5] OF BYTE;
PT : POINTER TO BYTE;
i : INT;
WordIndex: INT;
ProgramWord: ARRAY[1..5] OF Word;
characterbyteindex: INT;
ProgramWordIndex: INT;
END_VAR


(* This program will take a String and convert it to a Word Data type *)
(* Start by separating the characters from the string into an array *)

pt:=ADR(text); // Get the Address of the text variable named 'hello'
FOR i:=0 TO 4 DO
char:=pt^; // Assign the value of the pointer to a Character
pt:=pt+1; // Advance to the next pointer
END_FOR

characterbyteindex:=1;

(* "hello" has 5 characters...2 characters per word, 3 Words *)
FOR ProgramWordIndex:=1 TO 3 DO // The program name can take up to 3 words

//Get 2 bytes at a time, and store the 2 bytes into a word array,
//e.g. BYTE_TO_WORD char[1], BYTE_TO_WORD char[0], BYTE_TO_WORD char[3], BYTE_TO_WORD char[2], ect.

ProgramWord[ProgramWordIndex]:= SHL(BYTE_TO_WORD(char[characterbyteindex]),8)+BYTE_TO_WORD(char[characterbyteindex-1]);

//Advance the index, to get the next 2 bytes
characterbyteindex:=characterbyteindex+2;

END_FOR // ProgramWord will be a collection of words representing the original string
 

Similar Topics

FactoryTalk ME- Using a"String Input Enable" for Password Entry.How can I place X's Good Morning , FactoryTalk View ME ...... I'm...
Replies
2
Views
2,678
I'm using FTView SE and I'm looking for a way to get my string display to not cut off mid word for my descriptions on my motor faceplates. Some...
Replies
5
Views
2,849
Hello I'm intermediate programmer who is wondering if somebody is able to point me in the right direction in getting a few values in a readable...
Replies
17
Views
7,520
Hello, I am using studio 5000 pro and am trying to figure out the structured text. Here's my scenario: An operator scans a barcode, the barcode...
Replies
15
Views
261
I am creating a global object in FTView SE 13. I want to have a string read from the PLC and display (in this case the tagname). So lets say...
Replies
4
Views
175
Back
Top Bottom