Combining Data for CAN Gateway

For general Flowcode discussion that does not belong in the other sections.
Post Reply
hoozamawhatsit
Posts: 8
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 2:30 am
Has thanked: 2 times

Combining Data for CAN Gateway

Post 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
Last edited by hoozamawhatsit on Tue Sep 12, 2023 7:11 am, edited 1 time in total.

mnfisher
Valued Contributor
Posts: 981
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 105 times
Been thanked: 516 times

Re: Combining Data for CAN Gateway

Post 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

hoozamawhatsit
Posts: 8
Joined: Sun Dec 20, 2020 2:30 am
Has thanked: 2 times

Re: Combining Data for CAN Gateway

Post 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

mnfisher
Valued Contributor
Posts: 981
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 105 times
Been thanked: 516 times

Re: Combining Data for CAN Gateway

Post 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

Post Reply