CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum
Deutsche Version English version russian version 
It is currently Mon Aug 21, 2017 2:47 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jun 13, 2017 2:22 pm 
Offline

Joined: Fri Sep 11, 2015 12:39 pm
Posts: 16
Hi all.

So I am having this strange problem, where I need specific number of digits in my counter. I need the counter to be able to count up to 9999999, but the number of digits always has to be the same. This means, when the counter starts it should be 0000001, then later on it will be 0001021 and so on.

I would prefer to convert the counter into string later on, so maybe there is a solution there? I am completely out of ideas on how to easily realize this.

Please help ;)

Best regards,


Top
   
PostPosted: Fri Jun 16, 2017 1:43 pm 
Offline
Frequent User
Frequent User

Joined: Fri Sep 02, 2011 8:02 pm
Posts: 240
First, make sure your counter supports the high number (you need a dword output). Then you Use DWORD_TO_STRING to get the number as a string. Also define a string '0000000'. Concat the two together and then your answer is MID(togetherstring, LEN(togetherstring)-7,7). Or about that


Top
   
PostPosted: Mon Jun 26, 2017 1:15 pm 
Offline

Joined: Fri Sep 11, 2015 12:39 pm
Posts: 16
I actually made it happen like this, because I am not familiar with repeat of while funcitons as yet :)

Code:
STRINGcounter:=DWORD_TO_STRING(DWORDcounter);
IF LEN(STRINGcounter)=1 THEN
   TEMPcounter:=CONCAT('0', STRINGcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
ELSIF LEN(STRINGcounter)=2 THEN
   TEMPcounter:=CONCAT('0', STRINGcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
   TEMPcounter:=CONCAT('0', TEMPcounter);
.
.
.
ELSIF LEN(STRINGcounter)=7 THEN
   TEMPcounter:=STRINGcounter;
END_IF

I am not proud of it but it works ;)


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

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 2 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