HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Wed Apr 27, 2016 10:05 am

Good guys days, I need to create an array of variables of 4 bytes.
I've never worked with arrays of data, information've searched forum but it is not me how to create the structure.
Some partner can help. thank you very much.

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Wed Apr 27, 2016 12:46 pm

tello wrote:Good guys days, I need to create an array of variables of 4 bytes.
I've never worked with arrays of data, information've searched forum but it is not me how to create the structure.
Some partner can help. thank you very much.


welcome,
please read first by clicking here



create a new variable over projectexplorer
select type Byte
name it like you want
after name append [4]
now you have a byte array with place for 4 elements


in c code showing:

Code: Select all

//
byte myBArr[4];


you can set the myBArr by selecting by index where idx start form 0..3 for each Value of this container

myBArr[0] = 60;
myBArr[1] = 50;
myBArr[2] = 129;
myBArr[3] = 222;

now your Byte Array contains : 60 50 129 222

..
These users thanked the author Rudi for the post (total 3):
tello (Wed Apr 27, 2016 1:26 pm) • medelec35 (Wed Apr 27, 2016 1:28 pm) • jgu1 (Wed Apr 27, 2016 6:23 pm)
Rating: 15.79%
 

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Wed Apr 27, 2016 1:46 pm

Thanks Rudi.
I am an active user forum with FC5 and FC6 license but I do not know how I can change my status FC5 to FC6 member.

I followed the process that you have shown me but can not get the expression is accepted in the Modbus block component, there are some step I'm not doing well.
You have used the Modbus Master algunan project component ??

Thanks again for your help
Attachments
ARRAY_REGISTER.jpg
ARRAY_REGISTER.jpg (125.37 KiB) Viewed 7884 times

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: how can i do an array of variables 4 bytes

Postby Rudi » Wed Apr 27, 2016 2:40 pm

hi

simple change the type from byte to uint for your variable
you can edit the variable type by call the variable manager..

uintArr.png


best wishes
rudi ;-)

edit:
and be sure, your size of array is the same you need for the RegValue
These users thanked the author Rudi for the post (total 4):
LeighM (Wed Apr 27, 2016 2:52 pm) • jgu1 (Wed Apr 27, 2016 6:23 pm) • tello (Thu Apr 28, 2016 7:38 am) • medelec35 (Fri Apr 29, 2016 6:27 am)
Rating: 21.05%
 

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Thu Apr 28, 2016 7:56 am

Many many Thanks Rudi.
I feel stupid, it is clear that the format of the variable was was not Byte if not uint.

Thanks again
These users thanked the author tello for the post:
Rudi (Thu Apr 28, 2016 8:53 am)
Rating: 5.26%
 

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Thu Apr 28, 2016 1:57 pm

ok, but also need to convert 2 bytes registers Uint format to send it via Modbus, the Modbus send in Hex.
I tried several examples in simulation but do not work properly.

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Thu Apr 28, 2016 3:14 pm

tello wrote:ok, but also need to convert 2 bytes registers Uint format to send it via Modbus, the Modbus send in Hex.
I tried several examples in simulation but do not work properly.


not sure, whats the problem exact is, so feel free to lookup here onetime:

Hex
Data Stored
BYte Order
registers

depth..

Each table has 9999 values.
Each coil or contact is 1 bit and assigned a data address between 0000 and 270E.
Each register is 1 word = 16 bits = 2 bytes and also has data address between 0000 and 270E.

RegValue is uint, there are 2 container for each of your Values
if the regvalue is send -
myUintArr[0] : first register container
myUintArr[1] : second register container

first register example 00 80 = 0x0080
second register example 22 30 = 0x2230

if you know the regvalue adresse, so write in

myUintArr[0] = 0x0080
myUintArr[1] = 0x2230

hope this helps,
you can post your flowchart too.

best wishes
rudi ;-)
These users thanked the author Rudi for the post (total 3):
petesmart (Thu Apr 28, 2016 3:33 pm) • medelec35 (Fri Apr 29, 2016 6:28 am) • tello (Fri Apr 29, 2016 7:22 am)
Rating: 15.79%
 

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Fri Apr 29, 2016 8:03 am

Good guys days.

I explain what I need.
In my project, I have a micro with 2 UART, the first communicates with a microchip energy analyzer MCP39F511. My micro is always asking the Voltage, Current, Power and Reactive active variables, etc ..
and stores them in a record. Communication is in Bytes records but the variables are unsigned 16 bit, unsigned 32-bit and 64-bit unsigned. I need to rebuild those variables that I get the UART 1 for Modbus send through the UART 2.

  In the attachment it is the first part of the plot Uart 1 Response of Energy monitor.

I do not know if I explained well

Thank you very much
Attachments
Trama.jpg
Trama.jpg (113.18 KiB) Viewed 7811 times

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Fri Apr 29, 2016 10:39 am

RegIDX
--------------------------------------
0x0006 Byte1 Voltage RMS BYTE_L
0x0007 Byte2 Voltage RMS BYTE_H
..
..
0x000E Byte9 Current RMS BYTE_L
0x000F Byte10 Current RMS
0x0010 Byte11 Current RMS
0x0011 Byte12 Current RMS BYTE_H

---------------------------------------
Total Bytes 6

simple 'byte'Arr doings:

MyByteArr[0] contains the byte1 from 0x0006 BYTE_L
MyByteArr[1] contains the byte1 from 0x0007 BYTE_H
--------------------------------------------------
MyByteArr[2] contains the byte1 from 0x000E BYTE_L
MyByteArr[3] contains the byte1 from 0x000F
MyByteArr[4] contains the byte1 from 0x0010
MyByteArr[5] contains the byte1 from 0x0011 BYTE_H

The Uart0 Records the RegValis
The Uart1 Sends the Stored RegValis

Like it need, MSB or LSB
means Byte_H or Byte_L at first

// Send Voltage RMS

RS232.SendChar(MyByteArr[1]
RS232.SendChar(MyByteArr[0]


// Send Current RMS

RS232.SendChar(MyByteArr[5]
RS232.SendChar(MyByteArr[4]
RS232.SendChar(MyByteArr[3]
RS232.SendChar(MyByteArr[2]

..



depth thinking

read this
FlowChart sendStructs

If you use your ByteRecord
think you created like this:

Code: Select all

struct RecordFormat {

   byte VoltageRMS[2];
   byte CurrentRMS[4];   
   //.more.   
   //.more.
} myMCPData;


you can give your struct elements your read valis by pointer, example,
like the example with Struct Send,or you can simple record
without pointer and use the Variable in ModBus Component
like it is, because you do simulation too,
because send structs just in time not supported to simulate i think.


// without pointer

we have Uint, we need 2 bytes for VoltageRMS and 4 Bytes for CurrentRMS
so you can store in an uintArr[6] all valis you need

like you need to catch the MODBOS Data in this steps,
you get in one read 2 bytes as Value for Voltage RMS if you querry
by the start address (RegAddress VoltageRMS) and read 2 counts,
then you need a uintarr[2] to point RegValue to

myVoltageRMS[2]

and if you querry by the start address (RegAddress CurrentRMS)
you read 4 counts, so you need here a uintarr[4]
myCurrentRMS[4]

because the Regadress index is not ++
so you can not read in one step all two data
in one uintArr[6] so must help like this:

example
your first data lies on RegAddress
0x0006
and your last data lies on RegAddress
0x0012

you know
LSB from Voltage RMS is the Byte1
MSB from Voltage RMS is the Byte2

LSB from Current RMS is the Byte9
MSB from Current RMS is the Byte12

read with modbus component at adress 0x0006
12 counts in your totaluintArr[12]

then you have for
totaluintArr[0] Byte 1 LSB Voltage RMS
totaluintArr[1] Byte 2 MSB Voltage RMS

totaluintArr[8] Byte 1 LSB Current RMS
totaluintArr[9] Byte 2 Current RMS
totaluintArr[10] Byte 3 Current RMS
totaluintArr[11] Byte 4 MSB Current RMS

then send the valis with Uart from the stored elements
with simple

Code: Select all


// Voltage RMS MSB
RS232.SendChar(totaluintArr[1])
RS232.SendChar(totaluintArr[0])

// Current RMS MSB
RS232.SendChar(totaluintArr[11])
RS232.SendChar(totaluintArr[10])
RS232.SendChar(totaluintArr[9])
RS232.SendChar(totaluintArr[8])





with structs possible too


Code: Select all

// MSB
// Voltage RMS
RS232.SendChar(myMCP.VoltageRMS[1]);
RS232.SendChar(myMCP.VoltageRMS[0]);

// MSB
// Current RMS
RS232.SendChar(myMCP.CurrentRMS[3]);
RS232.SendChar(myMCP.CurrentRMS[2]);
RS232.SendChar(myMCP.CurrentRMS[1]);
RS232.SendChar(myMCP.CurrentRMS[0]);





hope this helps
happy weekend!


best wishes
rudi ;-)
These users thanked the author Rudi for the post:
tello (Fri Apr 29, 2016 10:58 am)
Rating: 5.26%
 

tello
Posts: 88
Joined: Wed Jan 16, 2013 11:37 am
Has thanked: 55 times
Been thanked: 12 times
Contact:

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby tello » Fri Apr 29, 2016 2:04 pm

I understand, all excepting one thing, I get Analyzer energy in Rs232 in Bytes but I eque send Multiple Registers FC16 Modbus and the data is Uintarray.
how can I convert 2 bytes to 1 Uint to put it in the array ??,

I feel not knowing clearly explained

Thanks
Attachments
Read Uart0 Write Uart1.jpg
(232.6 KiB) Downloaded 2323 times

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Fri Apr 29, 2016 3:41 pm

you ask, how can I convert 2 bytes to 1 Uint to put it in the array ??,


EDIT:
UPS! where becomes << 8 the place
now think you can work ( think you have found the mistake ) :mrgreen:


you have

Code: Select all

UintDataArray[]


and you have

Code: Select all

STR_Voltage_RMS[3]
STR_Voltage_RMS[2]


you can shift ( bit manipulation )
with a "c code Icon" example like this

this start with simply we think we have no array,
so the things would be lighter for begin:
your data comes from

STR_Voltage_RMS[3]
STR_Voltage_RMS[2]

so, this would be know, that each are a Byte
byte is 8 bit long look like 11111111
uint is 16 bit long look like 00000000 00000000

all to do is, pack the first data byte in the uint packet
and pack the next data byte in the uint packet

you must know, which data must comes as MSB or LSB
this is nammed Endianness ( big-endian, liitle-endian ) too

Code: Select all

// myUintData    before                 after
//       00000000 00000000
myUintData    = myByteContainer[3] << 8 ; //    11111111 00000000
myUintData  |= myByteContainer[2];         //    11111111 11111111



example,
your first byte was 255
your second byte was 255


Code: Select all


unsigned int Voltage_RMS = 0;

Voltage_RMS    = STR_Voltage_RMS[3] << 8  ;  // now V..RMS would be 11111111 00000000
Voltage_RMS  |= STR_Voltage_RMS[2] ;          // now V..RMS would be 11111111 11111111




now we know the base, so here the uintArr is the same way:
select your idx of container that you would want to fill with your bytes

Code: Select all


unsigned int Voltage_RMS[6] = 0;

UintDataArray[0]    = STR_Voltage_RMS[3] << 8 ;  // 11111111 00000000
UintDataArray[0]  |= STR_Voltage_RMS[2] ;         // 11111111 11111111




if you want from uint to each byte, there are bit manipulation with shift out
like >> and ....more ..

hope this helps
best wishes
rudi ;-)

edit version!
Sorry.

You can do short :

Code: Select all


uint16_t UintDataArray[6];
byte STR_Voltage_RMS[4];

UintDataArray[0] = ((uint16_t) STR_Voltage_RMS[3] << 8) | STR_Voltage_RMS[2] ; // HIGH BYTE .. LOW BYTE

These users thanked the author Rudi for the post:
tello (Tue May 03, 2016 9:09 am)
Rating: 5.26%
 

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Mon May 02, 2016 12:18 pm

sol4.png
..
These users thanked the author Rudi for the post:
tello (Tue May 03, 2016 9:09 am)
Rating: 5.26%
 

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Re: HOW CAN I DO AN ARRAY OF VARIABLES 4 bytes

Postby Rudi » Mon May 02, 2016 12:24 pm

the function you get there:

sol5.PNG


btw

be sure which system you have, litle endian - big endian
look this:

Code: Select all

// unsigned short myUint16/word
// byte ...low
// byte ...high

myUint16 = low | (high << 8);
..
word     = low | (high << 8);



works allways.

best wishes
rudi ;-)
These users thanked the author Rudi for the post:
tello (Tue May 03, 2016 9:09 am)
Rating: 5.26%