Join us today!
ASCII conversion - from numeric to text and vice versa
I'm relatively new to TwinCAT development, but I wrote functions to try and convert back and forth between ASCII as numeric vs text.
I do not understand why I am getting a type conversion error almost no matter how I format/convert the variables for this code. This feels intuitively like it should work, but after spending a majority of the afternoon troubleshooting the same "Cannot convert type 'Byte' to type 'String'" and "Cannot convert type 'String(INT#1)' to type 'BYTE'" errors I am asking for help. (I don't understand why there isn't a simpler way to do this? There has to be right?)
TYPE testUnion :
UNION
stTest : STRING;
arTest : ARRAY[0..27] OF USINT;
END_UNION
END_TYPE
FUNCTION_BLOCK ConvertASCArrayToText
VAR_IN_OUT
intArray: ARRAY[*] OF USINT; // Input array of 28 two-digit integers
END_VAR
VAR_OUTPUT
textOutput: STRING[27]; // Output text string of length 28
END_VAR
VAR
dataUnion: testUnion; // array to make all this wrangling easier
i : INT; // loop index
charCode : BYTE; //temporary variable to store the ascii value
END_VAR
//initialize the string part of the union with spaces
dataUnion.stTest := ' ';
// loop through each integer in the array
FOR i := 0 TO 27 DO
charCode := dataUnion.arTest[i]; // get the ascii value from the array
// ensure int is within valie ascii range for printable characters
IF charCode >= 32 AND charCode <= 126 THEN
dataUnion.stTest[i + 1] := CHR(charCode);
ELSE
dataUnion.stTest[i + 1] := ' ';
END_IF
END_FOR
Thanks-
There was no need to do any of this. I'm overcomplicating things as usual
- 17 Forums
- 267 Topics
- 924 Posts
- 1 Online
- 737 Members