Codesys string length weirdness

dwoodlock

Lifetime Supporting Member
Join Date
Nov 2012
Location
Cincy/nKY
Posts
568
Just wanted to see if anyone can see anything wrong with this as my mind is being blown. Basically, I have created a TCP server string decoder to break down the received string into smaller pieces which I can then use.

for whatever reason I'm finding if I send a 8-character string it always shows as 9. I just tested and found that if I send a 7-char string it also shows as 9. If I send a 2-character string, then it just shows 2 characters as well, which is additionally confusing. Length seems to be correct up to 3 characters, when I add a 4th, it jumps up to 5.

not a huge deal, I have found that when I convert the various strings into int's it truncates the mystery characters, but I'm more aggravated about the why. I expect it's a matter of ignorance on my part.

Thanks

Length.png Strings.png
 
Last edited:
Code:
VAR_INPUT
	Start:					BOOL;	
END_VAR
VAR_IN_OUT
	RawCommandString:		STRING(40);
END_VAR
VAR_OUTPUT
	DecodeDone:				BOOL;
	seqStep:				INT;
	CommandValue:			STRING(2);
	CommandArg1:			INT;
	CommandArg2:			INT;
END_VAR
VAR
	iStrLength:				INT;
	iCommaLoc:				INT;
	sLastString:			STRING(40);
	sCommandArg1:			STRING(20);
	sCommandArg2:			STRING(20);
END_VAR

CASE seqStep OF
	0://wait for the start signal
		IF Start THEN
			iStrLength := 0;
			CommandValue := '';
			sCommandArg1 := '';
			sCommandArg2 := '';
			CommandArg1 := -1000;
			CommandArg2 := -1000;
			DecodeDone := FALSE;
			seqStep := seqStep + 1;
		END_IF
	1://Store the length of our command string
		iStrLength := LEN(RawCommandString);
		seqStep := seqStep + 1;
		
	2://first 2 characters will always be our command value, so store it.
		CommandValue := MID(RawCommandString,2,1);
		seqStep := seqStep + 1;
		
	3://check our command string length and determine what to do next
		//if string length is 2 then we end the sequence
		IF (iStrLength = 2) THEN
			seqStep := 8;
		//if string length is > 2 then we proceed in the sequence	
		ELSIF (iStrLength > 2) THEN
			seqStep := seqStep + 1;		
		END_IF
	
	4://find the location of the comma in our command string, we know that our first argument begins after the space in our 
             command string which is pos 3
		iCommaLoc := FIND(RawCommandString, ',');
		seqStep := seqStep + 1;
		
	5://store the first argument
		sCommandArg1 := MID(RawCommandString, iCommaLoc - 4, 4);
		seqStep := seqStep + 1;
		
	6://store the second argument, we know it begins after the comma up until the end of the string
		sCommandArg2 := MID(RawCommandString, iStrLength - (iCommaLoc + 1), iCommaLoc + 2);
		seqStep := seqStep + 1;
		
	7://convert arguments to int
		CommandArg1 := STRING_TO_INT(sCommandArg1);
		CommandArg2 := STRING_TO_INT(sCommandArg2);
		seqStep := seqStep + 1;
		
	8://indicate completion and return to initial state
		DecodeDone := TRUE;
		seqStep := 0;
END_CASE
 
Additionally, this is on a CtrlX core.

Have used several of the string length calculation tools, result is always the same.
 
I just use it as an output variable so that if I run into an issue that I can see from my main routine instead of opening the block.

Like a high-level overview. It wouldn't mean anything to someone besides myself since I wrote it and know what each step is doing.

In final form I'd probably keep it as a local variable or create an output string to explain the current sequence step. Ideally for something this simple wouldn't be necessary to explain why it went wrong, because it wouldn't (my perfect world scenario).
 
Just tried some trials with your suggestion, and the result is erratic.

when I get the bogus characters it's because that's what I'm submitting to my decoding block.

From that it seems the block is working fine, its more how is the data the server reads being handled.

it's a from the provided library, so perhaps I'll try something else on the server side.

pics below.

length11.png length12.png length13.png
 
Update:

Got a response from a contact at Rexroth as to how this should be done. Its like 10X more complex.

Since what I've done already works in combination with my decode(parse) block, I'm debating whether I'll implement the more complex suggestions.
 

Similar Topics

Codesys 3.5 SP15: So I created a screen with a Combo Box the combo's value is set to a Instance of an ENUM, I have a textlist configured too...
Replies
1
Views
1,444
I'm having trouble locating string data to %MW registers. This does not appear to work: site_name AT %MW200: STRING[20];
Replies
3
Views
1,559
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...
Replies
4
Views
12,495
Hey guys! Trying to solve a problem regarding a protocoll i want to create. I´ve managaed to create a program that writes the current values on...
Replies
2
Views
8,321
Hello, I am using a Hitachi Micro EHV+ for a small project, and I wanted to have a Web visu, done with Codesys V3.5 SP13 Patch 2. I test the...
Replies
6
Views
294
Back
Top Bottom