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 Mon Sep 16, 2019 3:09 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Convert Byte into Array
PostPosted: Thu Jan 17, 2019 11:33 am 
Offline

Joined: Wed Jan 16, 2019 6:15 pm
Posts: 1
Hello all,

Have you got any ideas of how i can convert byte into an array in order to read and access to the datas ?

Thanks for your help


Top
   
PostPosted: Thu Jan 17, 2019 3:09 pm 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 151
Byte into an bit array ? Then declare a union datatype with Byte and your bits (named into a DUT or in array)


Top
   
PostPosted: Thu Jan 17, 2019 7:58 pm 
Offline

Joined: Sun Jun 28, 2015 12:23 pm
Posts: 86
I don't think that will work.
You can't make an array of BIT, and a BOOL occupies 8 bits, so it won't align.

There's probably an OSCAT block, but I can never find anything I want in OSCAT :)

I'd just:
Code:
Bit0 := (MyByte AND 16#1) <> 0;
Bit1 := (MyByte AND 16#2) <> 0;
Bit2 := (MyByte AND 16#4) <> 0;


Top
   
PostPosted: Thu Jan 17, 2019 8:08 pm 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
A union would be nice but believe this doesn't work because of what Le_Baloo said. Been a while since I have tested it though.

What if you just use the UNPACK function. Input would be your byte and the output could tie each bit to your array.
https://help.codesys.com/webapp/unpack;product=codesys;version=3.5.11.0

Or you could do it by hand.
Code:
    BoolArray[0] := Byte.0;
    BoolArray[1] := Byte.1;
    BoolArray[2] := Byte.2;
    BoolArray[3] := Byte.3;
    BoolArray[4] := Byte.4;
    BoolArray[5] := Byte.5;
    BoolArray[6] := Byte.6;
    BoolArray[7] := Byte.7;


Top
   
PostPosted: Fri Jan 18, 2019 10:05 am 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 151
So if you want to name it, declarations :

Code:
TYPE MyPumpCmd_STRUCT :
STRUCT
   Bit0 : BIT;
   Bit1 : BIT;
   Bit2 : BIT;
   Bit3 : BIT;
   Bit4 : BIT;
   Bit5_Run : BIT;
   Bit6_Reverse : BIT;
   Bit7_QuickStop : BIT;
END_STRUCT
END_TYPE

Code:
TYPE MyPumpCmd_UNION :
UNION
   aByte : BYTE;
   aStruct : MyPumpCmd_STRUCT;
END_UNION
END_TYPE

Code:
VAR_GLOBAL
   MyPumpCmd : MyPumpCmd_UNION;
END_VAR


Then, usage :

Code:
MyPumpCmd.Bit5_Run := TRUE
%OB5 := MyPumpCmd.aByte


EDIT 2019/01/21: Correct "Bit3 : BOOL;" to "Bit3 : BIT;"


Last edited by dFx on Mon Jan 21, 2019 10:43 am, edited 1 time in total.

Top
   
PostPosted: Fri Jan 18, 2019 6:54 pm 
Offline
Frequent User
Frequent User

Joined: Tue Dec 03, 2013 11:52 pm
Posts: 120
Thanks dFx. I learned something new but there was one issue I ran into when testing this. When you specified BOOL for Bit3 it causes it to not work. All data types appear to have to be BIT in order for the union to work.


Top
   
PostPosted: Mon Jan 21, 2019 10:41 am 
Offline
Frequent User
Frequent User

Joined: Fri Feb 23, 2018 3:41 pm
Posts: 151
Just a typo mistake. Edited my previous post.

BIT datatype is a bit length in memory, while BOOL is 8 bit length (a Byte).


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 7 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