Author Topic: Read multiple Can messages same ID  (Read 2029 times)

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Read multiple Can messages same ID
« on: September 15, 2023, 09:42:50 PM »
Dear all,
I have to read a Can message on attached CAN trace. I’m using MC024_130, the problem is that I can receive 8 bytes every loop , my TimeLoop is 5mS, but I tested 1..10..15.
I have to rebuilt a 80 U8 byte Array, but every way I try I can only read first 8 and I’m not able to copy into an array all data I received.
Is there a way to do it in Guide? Looking at timestamp you can see that this messages are sent every 1Sec a 10 packet. D0 is SID, so, for example, D0=109 means that I have to copy all 10 messages that contains D0=109 in a 80 byte array.
I tried many way, using Copy of array, but with no success. Could I try using POU? Have you got an example code ?
Many thanks

Offline FluidPowerTom

  • PLUS+1 Guru
  • *****
  • Posts: 363
  • Karma: +33/-0
Re: Read multiple Can messages same ID
« Reply #1 on: September 18, 2023, 05:14:08 PM »
I'd definitely use a ST POU for that.  The output from the CAN Rx is an array which you could use as an input variable to your POU.  It'd probably take me 20-30 min to type out some code that'd do this though.  Array operations, while nowhere near as powerful as with MATLAB, are still pretty sufficient in ST.
Controls Engineer
Hydra-Power Systems

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Re: Read multiple Can messages same ID
« Reply #2 on: September 18, 2023, 06:06:44 PM »
Thanks for your reply FluidPowerTom.
So youbmean to connect to a POU inputbus, the Array [8] U8 fron CanRx block? I'm just trying to do this,but seems that only first or last packet sent from my the device is cought in MC controller. Basically yes,i have to built an Array larger than 8. If uou will back with an example i'll can see where i'm doing it wrong
Thanks in advance

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Re: Read multiple Can messages same ID
« Reply #3 on: September 19, 2023, 01:16:29 AM »
What i'm doing is this in pictures. Trying Protected T or F, trying to play with ExcecTimeOut with no success. Seems that every messages are absorbed from controller and only first or last are visibles. If it's so i think is a very bad limit for plu+1 controllers.

this is what's in the POU (a very easy test) :

FUNCTION_BLOCK GGA_Decoder
VAR_INPUT
  Data : ARRAY[0..7] OF USINT;
  Length : USINT;
END_VAR
VAR_OUTPUT
  Arr0 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr1 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr2 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr3 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr4 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr5 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr6 : ARRAY[0..7] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
END_VAR
VAR
  Index : USINT;
END_VAR

// Extract Identifier
Index := Data[0];

CASE Index OF
  160, 192, 32, 0, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240:
    Arr0 := Data;
  161, 193, 33, 1, 65, 81, 97, 113, 129, 145, 161, 177, 193, 209, 225, 241:
    Arr1 := Data;
  162, 194, 34, 2, 66, 82, 98, 114, 130, 146, 162, 178, 194, 210, 226, 242:
    Arr2 := Data;
  163, 195, 35, 3, 67, 83, 99, 115, 131, 147, 163, 179, 195, 211, 227, 243:
    Arr3 := Data;
  164, 196, 36, 4, 68, 84, 100, 116, 132, 148, 164, 180, 196, 212, 228, 244:
    Arr4 := Data;
  165, 197, 37, 5, 69, 85, 101, 117, 133, 149, 165, 181, 197, 213, 229, 245:
    Arr5 := Data;
  166, 198, 38, 6, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 246:
    Arr6 := Data;
 
END_CASE

in the recent past I struggled badly with Danfoss displays trying to acceptably capture an NMEA string on RS232,but ExcTimeout always effect the result, now we can't get the positioning data correctly read even on Can.
Once again I'm afraid we should change Hardware. But in 2023, almost 2024, with many blocks for Autonomous Guide developed, at least something to manage the data of a standard GNSS would have expected. It's a real shame, because the displays and controllers are
very good.

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Re: Read multiple Can messages same ID
« Reply #4 on: September 19, 2023, 01:22:32 AM »
And this is the Trace

Offline Matt_Eng

  • PLUS+1 Developer
  • ***
  • Posts: 21
  • Karma: +10/-0
Re: Read multiple Can messages same ID
« Reply #5 on: September 19, 2023, 04:22:03 PM »
Hi,
It can be a little tricky initially to deal with a multi-message data array in GUIDE, I know I struggled with it when I first started working in GUIDE.
There are a few different ways you can go about implementing the reception, but I'll describe the most straight forward method, based on your latest message.

I'll first describe how Protected works, as it is key to getting this working. Imagine you have an list of messages that was received each loop.
When Protected is False, it will only grab the newest message that is available, so if 3 messages were received that match the CAN receiver the oldest 2 are discarded and only the latest is used.
This is useful for receiving something like a joystick message, where the latest data is preferred.
When Protected is True, it will pop off the first message it finds and return that. Then if you have an additional CAN receiver with the same setup and protected True, that 2nd receiver will get the second message that was received.

So by having multiple receivers with Protected True, you can see all the messages that were received over a loop.
The next part is to choose to have a Receive CAN with Filter for every sequence byte, as shown in my example picture. This allows us to apply a data mask and further limit which message is received. This is a good option if the amount of data being transmitted is less than 10 messages.
More Advanced: If it is more than 10, then I just use 3-5 Receive CAN with ID Filter and use additional logic to place the messages in the correct spot of the combined array.

I hope this helps, Matt_Eng

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Re: Read multiple Can messages same ID
« Reply #6 on: September 19, 2023, 05:26:34 PM »
Matt_Ing, you are the Goat. that's is working perfectly as i want.
I'm making an NMEA2000 GNSS parsing,i will share the project once tested.

Offline FluidPowerTom

  • PLUS+1 Guru
  • *****
  • Posts: 363
  • Karma: +33/-0
Re: Read multiple Can messages same ID
« Reply #7 on: September 19, 2023, 05:33:19 PM »
Matt, that's a good tip which sounds like a possible way to do this in GUIDE graphical.

F.M., should the output arrays be 80 elements instead of 8?  In that case I'd do some index incrementing perhaps like the below when a new message is received with the same Data[0].  I'd recommend using the Rx output from the CANRx block which will turn on for one scan whenever a new message is received:



FUNCTION_BLOCK GGA_Decoder
VAR_INPUT
  Data : ARRAY[0..7] OF USINT;
  Length : USINT;
  CAN_Rx: BOOL;
END_VAR
VAR_OUTPUT
  Arr0 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr1 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr2 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr3 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr4 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr5 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
  Arr6 : ARRAY[0..79] OF USINT := [0, 0, 0, 0, 0, 0, 0, 0];
END_VAR
VAR
  Index : USINT;
  Index_Adder: USINT;
END_VAR


//Increment indexer
IF CAN_Rx AND Index=Data[0] THEN
    Index_Adder:=Index_Adder+8;
ELSIF CAN_Rx AND Index<>Data[0] THEN
    Index_Adder:=0;
END_IF

// Extract Identifier
Index := Data[0];

FOR i = 0 TO 7 DO
    Arr0[i+Index_Adder]:=Data;
END_FOR


I think the above will save 80 elements with each message data in sequence in an 80 element array.  I haven't tested this of course.  You'll need to implement some other logic to fill in the other arrays with different byte 0 values, so some logic needs to be added for that


CASE Index OF
  160, 192, 32, 0, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240:
    Arr0 := Data;
  161, 193, 33, 1, 65, 81, 97, 113, 129, 145, 161, 177, 193, 209, 225, 241:
    Arr1 := Data;
  162, 194, 34, 2, 66, 82, 98, 114, 130, 146, 162, 178, 194, 210, 226, 242:
    Arr2 := Data;
  163, 195, 35, 3, 67, 83, 99, 115, 131, 147, 163, 179, 195, 211, 227, 243:
    Arr3 := Data;
  164, 196, 36, 4, 68, 84, 100, 116, 132, 148, 164, 180, 196, 212, 228, 244:
    Arr4 := Data;
  165, 197, 37, 5, 69, 85, 101, 117, 133, 149, 165, 181, 197, 213, 229, 245:
    Arr5 := Data;
  166, 198, 38, 6, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 246:
    Arr6 := Data;
 
END_CASE
Controls Engineer
Hydra-Power Systems

Offline FluidPowerTom

  • PLUS+1 Guru
  • *****
  • Posts: 363
  • Karma: +33/-0
Re: Read multiple Can messages same ID
« Reply #8 on: September 19, 2023, 05:35:29 PM »
The programming syntax is interfering with part of the code I wrote

FOR i = 0 TO 7 DO
    Arr0[i+Index_Adder]:=Data[''i'']; (remove apostrophes on i)
END_FOR
Controls Engineer
Hydra-Power Systems

Offline F.M.Elettromeccanica

  • PLUS+1 Developer
  • ***
  • Posts: 47
  • Karma: +1/-0
Re: Read multiple Can messages same ID
« Reply #9 on: September 23, 2023, 06:58:03 PM »
FluidPowerTom, I still can't get your code to work. Maybe I'm making a mistake somewhere. I will reply as soon as possible and let you know. Thank you, many thanks for everything.

Offline FluidPowerTom

  • PLUS+1 Guru
  • *****
  • Posts: 363
  • Karma: +33/-0
Re: Read multiple Can messages same ID
« Reply #10 on: September 25, 2023, 05:14:06 PM »
What I entered wasn't really intended as for copy paste.  There are at least one or two errors.  It was more intended as an example to understand and potentially pull from if desired.
« Last Edit: September 25, 2023, 05:25:44 PM by FluidPowerTom »
Controls Engineer
Hydra-Power Systems