CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Tue Oct 22, 2019 1:11 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Thu Apr 25, 2019 9:15 am 
Offline

Joined: Wed Apr 24, 2019 11:54 am
Posts: 1
Hi,

I'm struggling. I'm more of an occasional CoDeSys user rather than a professional.

What I am trying to do is to read serial characters and append them to a string. When I read a character from the input object, it is an ASCII value (g = 103 etc.) So I need to convert that value to the character "g" and then concat it to the string.

I've spent ages Googling and not found an answer so I thought I'd ask here.

Any suggestions very welcome.

Thanks in advance,

Richard


Top
   
PostPosted: Thu Apr 25, 2019 5:38 pm 
Offline
Frequent User
Frequent User

Joined: Sun Apr 15, 2007 1:18 am
Posts: 173
I propose a function :

--------------------------------------------------
FUNCTION F_ASCII_TO_STRING : STRING(1)

VAR_INPUT
InputVal: UINT;
END_VAR

VAR
Caracter:STRING(1);
END_VAR
--------------------------------------------------
CASE InputVal OF
48:Caracter:='0';
49:Caracter:='1';
50:Caracter:='2';
51:Caracter:='3';
52:Caracter:='4';
53:Caracter:='5';
54:Caracter:='6';
55:Caracter:='7';
56:Caracter:='8';
57:Caracter:='9';
65:Caracter:='A';
66:Caracter:='B';
67:Caracter:='C';
68:Caracter:='D';
69:Caracter:='E';
70:Caracter:='F';
71:Caracter:='G';
72:Caracter:='H';
73:Caracter:='I';
74:Caracter:='J';
75:Caracter:='K';
76:Caracter:='L';
77:Caracter:='M';
78:Caracter:='N';
79:Caracter:='O';
80:Caracter:='P';
81:Caracter:='Q';
82:Caracter:='R';
83:Caracter:='S';
84:Caracter:='T';
85:Caracter:='U';
86:Caracter:='V';
87:Caracter:='W';
88:Caracter:='X';
89:Caracter:='Y';
90:Caracter:='Z';
97:Caracter:='a';
98:Caracter:='b';
99:Caracter:='c';
100:Caracter:='d';
101:Caracter:='e';
102:Caracter:='f';
103:Caracter:='g';
104:Caracter:='h';
105:Caracter:='i';
106:Caracter:='j';
107:Caracter:='k';
108:Caracter:='l';
109:Caracter:='m';
110:Caracter:='n';
111:Caracter:='o';
112:Caracter:='p';
113:Caracter:='q';
114:Caracter:='r';
115:Caracter:='s';
116:Caracter:='t';
117:Caracter:='u';
118:Caracter:='v';
119:Caracter:='w';
120:Caracter:='x';
121:Caracter:='y';
122:Caracter:='z';
ELSE
Caracter:='';
END_CASE;

F_ASCII_TO_STRING:=Caracter;
--------------------------------------------------

How to use it ?
--------------------------------------------------
PROGRAM PLC_PRG
VAR
ValNum:UINT;
Result:STRING;
END_VAR
--------------------------------------------------
Result:=F_ASCII_TO_STRING(ValNum);
--------------------------------------------------

Hoping this will meet your needs :D

BR


Top
   
PostPosted: Thu Apr 25, 2019 5:50 pm 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
May not be the best solution but what you could do is:
Code:
VAR
    sTempString : STRING;
    pConvertString : POINTER TO BYTE;
    bySerialByte : BYTE;
END_VAR

//First get the address of the temporary string to store your character
pConvertString := ADR(sTempString);

//Second assign the decimal value to the temporary string
pConvertString^ := bySerialByte;

//Third "sTempString" now equals the ascii character and you can concat it to something else
    //Add your code to concat to another string example: sOriginalString := CONCAT(sOriginalString, sTempString);


You could put this in a function or OSCAT might already have a function to do something like this.


Top
   
PostPosted: Thu Apr 25, 2019 8:01 pm 
Offline

Joined: Thu Aug 04, 2016 4:21 pm
Posts: 40
why you are using workarounds, when the util Lib does, what you are searching for 8)
After conversion do the concat...


You do not have the required permissions to view the files attached to this post.


Top
   
PostPosted: Thu Apr 25, 2019 9:34 pm 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
Is that library available with that function in Codesys version 2.3 where this question was posted? I don't know what libraries are available in 2.3 since I don't use it is why I ask. My answer was a way to accomplish it without caring about what library is available in 2.3.


Top
   
PostPosted: Fri Apr 26, 2019 9:31 pm 
Offline
Frequent User
Frequent User

Joined: Sun Apr 15, 2007 1:18 am
Posts: 173
For D.Kugler
This library shown in the post is available with CODESYS V3.5. :oops:
The library Util available with CoDeSys V2 don't offer this function. :D The only conversion functions are BCD functions (INT_TO_BCD, BCD_TO_INT).


For Cmingback4u :
Your solution is simpler and more elegant than mine. :D

BR


Top
   
PostPosted: Sat Apr 27, 2019 10:17 am 
Offline

Joined: Thu Aug 04, 2016 4:21 pm
Posts: 40
Sorry i'm thinking and working most in V3.5 and only sometimes in V2.3! Forget my post or use it in V3 :D


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 7 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited