SI message port: WIKI error?

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: SI message port: WIKI error?

#11 Post by jph »

Hi Tony, according to wiki, it can be used with named hardware (as far as I can see)

Would also love to see full two way comms working code (with multiple options for payload and number of elements in such etc) -
Although the MOST SIMPLISTIC code - as way to test on way - arduino to AM - (that works) that I posted above Is ok in AM4.x (But not in 3.x !) - I 'think' it would work ok in am 3.x WITH named hardware.. baffled. :| - I will test later with named in 3.x.

It would be REALLY good to have a whole section on Message Port as I think there is so much to learn. Personally, I though I was reasonably proficient in Arduino programming but found that I have a very (VERY) limited understanding of Arduino when using a C++ 'struct' and how to extract and use the elements of the 'struct'.
Maybe this is a great time to get to grips with this ?.
A said, I thought I was ok with Arduino until I came across the 'struct' as in message port - and also the way to extract and use the elements of such. It leaves me cold really.
Best regards,
Perhaps you guys and @Corjan can help here as It is seemingly not just 'beginners' who are seemingly confused by this. More working examples would be excellent. And a good discussion would be welcome. The thing I personally have difficulty with is the 'struct' and specifically the AM 'struct' and decoding and breaking down the elements. This is more a C++ programmers area of expertise than the cut down version of the 'real' C++ that is used in Arduino (as far as knowledge is concerned - in my case!)
Joe
Joe. CISSP, MSc.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: SI message port: WIKI error?

#12 Post by jph »

Ah, yes - so after a quick test of 'directly' named vs ASSIGNED hardware - 'Directly named' works in AM3.x ! whereas 'assigned' - DOES NOT (at least in my simplistic code !)

the following works in AM 3.X AND AM 4.x !!! - with my simplistic arduino code - USING NAMED AND ASSIGNED HARDWARE -

Code: Select all

-- This function will be called when a message is received from the Arduino.

function new_message(id, payload)
  print("received new message with id " ..id)
end

id = hw_message_port_add("ARDUINO_UNO_A", new_message)
whereas - THIS - ASSIGNED - (assigned in the drop down option boxes )- does not work in AM 3.x .. but DOES in AM 4.x

Code: Select all

-- This function will be called when a message is received from the Arduino.
function new_message(id, payload)
  print("received new message with id " ..id)
end

id = hw_message_port_add("My message port", new_message)
Interesting.
Joe
Joe. CISSP, MSc.

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: SI message port: WIKI error?

#13 Post by Sling »

Have you got a specific application in mind for a full 2 way comms example. For a tutorial example it really ought to not use any special hardware. I think basic hardware that most should have so it’s easy to construct and follow along without anything special.

The example in the tutorial video does show both a tx and rx example, albeit they do not have any relationship if that is what you were looking for.
Last edited by Sling on Sun Oct 03, 2021 9:41 am, edited 1 time in total.

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: SI message port: WIKI error?

#14 Post by jph »

Sling wrote: Mon Apr 19, 2021 10:49 am Have you got a specific application in mind for a full 2 way comms example. For a tutorial example it really ought to not use any special hardware. I think basic hardware that most should have so it’s easy to construct and follow along without anything special.Even better is the

The example in the tutorial video does show both a tx and rx example, albeit they do not have any relationship if that is what you were looking for.
Not specifically Tony,
It is more the use and breakdown of the 'struct' into its components and individual elements at the Arduino end.
Test such as this can be used without specific hardware. It is a matter of understanding the breakdown of the elements of the 'struct' and how to read them
Joe. CISSP, MSc.

JackZ
Posts: 2262
Joined: Mon Feb 22, 2016 1:02 pm

Re: SI message port: WIKI error?

#15 Post by JackZ »

Hi @jph

With my newly found knowledge, let me try and tell you what I gathered about your question:

The payload is a C++ struct of SiMessagePortPayload type, which is defined in the library.
That payload struct is if I understand built as follows and contains 3 elements type, length and data:

Code: Select all

struct SiMessagePortPayload{
enum SiMessagePortDataType; //Data type in the payload. Can be: 
SI_MESSAGE_PORT_DATA_TYPE_BYTE
SI_MESSAGE_PORT_DATA_TYPE_STRING
SI_MESSAGE_PORT_DATA_TYPE_FLOAT
SI_MESSAGE_PORT_DATA_TYPE_INTEGER

uint8_t len;//Number of elements in the payload
//Then the payload data that can be of different types depending on the data type above (only one types allowed)
uint8_t  data_byte ;// The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_BYTE.
char data_string;// The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_STRING.
int32_t   data_int; //The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_INTEGER.
float data_float; //The payload data when the payload has type SI_MESSAGE_PORT_DATA_TYPE_FLOAT 
}
To access the payload elements, one has to use pointers to the struct in your Arduino code:

Say you create a custom payload struct of the SiMessagePortPayload type:

Code: Select all

SiMessagePortPayload* mypayload;
You can then access elements of your payload using ->element or *(my payload).element

Code: Select all


SiMessagePortDataType pType=mypayload-> SiMessagePortDataType; 

SiMessagePortDataType pType=*(mypayload).SiMessagePortDataType;// equivalent

uint8_t  pLength=mypayload-> len;

switch(pType) {
      case SI_MESSAGE_PORT_DATA_TYPE_BYTE:
         uint8_t pData_byte=mypayload-> data_byte;
         break;
      case SI_MESSAGE_PORT_DATA_TYPE_STRING;
         char pData_string=mypayload-> data_string;
         break;
}
And so on...

Corjan and Tony will probably chime in with more precision’s

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
Corjan
Posts: 2936
Joined: Thu Nov 19, 2015 9:04 am

Re: SI message port: WIKI error?

#16 Post by Corjan »

Hi,


The exact C(++) definition is this:

Code: Select all

struct SiMessagePortPayload {
	union {
		uint8_t* data_byte;
		char* data_string;
		int32_t* data_int;
		float* data_float;
	};

	enum SiMessagePortDataType type;
	uint8_t len;
};

What Jacques said is correct, first check the type and the use the corresponding data_* variable.

Corjan

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: SI message port: WIKI error?

#17 Post by jph »

@JackZ and @Corjan and @Sling
Hello guys
Thank you ever so much for your help here. The mist is virtually lifted :D . As usual, a picture is worth a 1000 words. - or in this case, an example is worth a heck of a lot of digging into the 'rabbit hole' depths of C++ :?

I have come across pointers before, but a heck of a long time ago. Also, the -> (and indeed) struct in general is very poorly documented in Arduino. However, there is a very basic reference at Arduino CC to & and * for pointer reference but nothing in relation to -> (arduino) that I can see. But, with the help you have provided I can make progress and have a much clearer understanding. It makes sense.
With this super help, I can now explore more.
I will no doubt be back with some more questions on the finer points if old age / lack of knowledge and brain fade strikes again ;)
The above makes sense to me and was very nicely presented.

I do have a request please ?, when the payload contains multiple data (of the same type as I am aware they must be in one message) in the payload data (as signified, and we can determine by the type and len), how are the individual 'data' elements referenced ?. - for example, say, 3 'byte' values, or 4 'string' values where the type is known and the len (number of actual data elements) is now known from the derivation of the previous data type and len.. how do we reference / store the payload data 1, 2,3 etc ?... say we want the 3rd data element of a string type with a len of 5 elements ? - or the indeed, to reference any of the elements up to the quantity referred to in 'len' of the referenced data type ?
I am sure it will probably be as simple...............
Really appreciate the help.
Joe
Joe. CISSP, MSc.

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: SI message port: WIKI error?

#18 Post by Sling »

jph wrote: Tue Apr 20, 2021 8:22 am I do have a request please ?, when the payload contains multiple data (of the same type as I am aware they must be in one message) in the payload data (as signified, and we can determine by the type and len), how are the individual 'data' elements referenced ?. - for example, say, 3 'byte' values, or 4 'string' values where the type is known and the len (number of actual data elements) is now known from the derivation of the previous data type and len.. how do we reference / store the payload data 1, 2,3 etc ?... say we want the 3rd data element of a string type with a len of 5 elements ? - or the indeed, to reference any of the elements up to the quantity referred to in 'len' of the referenced data type ?
In a similar way to how you would access an array element in Lua.

So from the AM side of the tutorial example its simply payload where i is the data index, not forgetting that the AM read index for arrays(tables) starts at 1.

Code: Select all

function message_callback(id, payload)
    payload_str=""
    i=0
    for _ in pairs(payload) do
        i = i + 1
        if i == 1 then
                 payload_str = tostring(payload[i])
        else 
                payload_str=payload_str..","..tostring(payload[i]) 
        end
    end  
    print("Message received with id:"..id.." and payload of :"..payload_str)
end 
From the Arduino side of the tutorial example its simply data_int[0]; where 0 is the data index, not forgetting that the Arduino index starts at 0.

Code: Select all

if (message_id==7) {
  digitalWrite(led,payload->data_int[0]);
  messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_DEBUG,String(payload->data_int[1]));
} else if (message_id==8) {
    messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_DEBUG,String(payload->data_string));
  }
}	

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: SI message port: WIKI error?

#19 Post by jph »

Hi Tony @Sling
With the very greatest respect, can we please ignore the video tutorial for the led for the moment. ? - I am in no way decrying the example on the vid
Rather than have a specific action in mind - ie - turn on an led , would you be kind enough to simply offer examples of passing data to and from arduino and AM in the context of using the console output for feedback to view the result of changes and experimentation in virtual real time ?

Here, THE biggest issue for those familiar with arduino is not being able to use the serial console in the Arduino IDE for output to aid debugging and learning This is a 'problem' for most Arduino programmers. This is like tying a hand behind your back.

The only way I can see to hone skills is to use the AM / LUA console as the sole feedback.
So, with thins in mind

For example.
in the wiki -
https://siminnovations.com/wiki/index.p ... #Functions

I would be interested in - and I think it would be more informative - to add to the wiki here
Such as

-- This function will be called when a message is received from the Arduino.

Code: Select all

function new_message(id, payload)
  -- Do something with the message from the Arduino
end
Add meaningful code options in place of the -- Do something with the message from the Arduino .. it doesnt have to be hardware related - just produce an output on the console using the payload and options for such - as i differing types and LEN to provide meaningful text output feedback. As in, push the complete reception in text data back to the console as this can be used for compares in virtual real time as the code is changed and experimented with.

Also, in the Arduino -

Code: Select all

static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) {
  // We received a message from Air Manager or Air Player
}
Add meaningful code options in place of the // We received a message from Air Manager or Air Player.. it doesnt have to be hardware related - just produce a meaningful textual output / outputs on the console using the payload and options for such. again, it doesnt have to be hardware related - just produce an output on the console using the payload and options for such to provide meaningful text output feedback. As in, push the complete reception in text data back to the console as this can be used for compares as the code is changed and experimented with. Hopefully - using the options for the varying data types and amounts of data in a text console output where the results of changes can be clearly seen.

Also, in the 'example' i the arduino examples - - displaying the DebugMessage at the AM console end.

Hope that makes sense Tony. I am in no way decrying the example on the vid, I just want to expand on the quoted WIKI where no hardware at all needs to be used or simulated.
By having a simple (or complex) test / console output it is really really good to make changes and see the results immediately.
Joe
Joe. CISSP, MSc.

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: SI message port: WIKI error?

#20 Post by Sling »

The request has changed somewhat. You asked about how to access the payload elements so that’s what I answered. This is now a different request.

On this one I would ask. Have you tried the tutorial example because it may have code for toggling an led but it also has examples of debug messages back from the Arduino and as I previously posted code for reading the different payload elements. I don’t go into great detail in the video about all of this as these were aimed at getting someone started. We can certainly explore some of the detail if you have specific questions about that code.

Post Reply