Page 1 of 1

Combining Data for CAN Gateway

Posted: Tue Sep 12, 2023 5:51 am
by hoozamawhatsit
Hi all,

I'm trying to combine 3 bytes of data to manipulate and forward over CAN to 4 bytes of data
the reasoning behind this is device A calculates it's data different to device B, I'm developing a gateway to go between device A & B.

E.g.
B0=f8, B1=ff, B2=ff
Combined = fffff8, how do I do this bit? I Figured this bit out
string0 = 0xff
string1 = 0xff
string2 = 0xf8
ODOIN = ((string0) << 16 | (string1) << 8 | (string2))


I then need to take 0xfffff8 or 16777208 multiply by 25 = 4211081215, then convert back to 4 bytes to send via CAN, I can do this via modulus but then need to combine again to send as hex

4211081215%16 = 15 = F
4211081215/16 = 263192575 %16 = 15 = F
and so on until I end up with f a f f f f f f then to combine as fa ff ff ff, I'm sure the help with combining initial data will help with this bit too.

Thanks,
Mick

Re: Combining Data for CAN Gateway

Posted: Tue Sep 12, 2023 6:36 am
by mnfisher
One way (probably the easiest) is to use the 'type conversions' component - load with data and pull out a 32 bit value.

You can do with shifts.

.x = (.b2 << 8) + .b1
.x = (.x << 8) + .b0

Wil combine 3 bytes into a long word (.x)

For ease - think about the values in hex (base 16) - where each 8 bit value occupies one byte of (in the case of a 4 byte ulong) one of the bytes - you can enter hex numbers using the prefix 0x for example:
.h = 0xf8 (sets .h to F8 (which is 248 (decimal) or 0b11111000 (binary) or 0370 in octal (base 8))

Martin

Re: Combining Data for CAN Gateway

Posted: Tue Sep 12, 2023 7:15 am
by hoozamawhatsit
Thanks Martin,

I had figured it out with shifts just as you had posted, although slightly different to the way you have explained. I will try your way too.

Now for all the calcs then to re-assemble.

thanks again,
Mick

Re: Combining Data for CAN Gateway

Posted: Tue Sep 12, 2023 2:22 pm
by mnfisher
Let us know how you get on.

If you post your code, or a snippet thereof, then someone will surely try and help 😊 - one of the advantages of flowcode is the friendly community.

Martin