SI Message_Port() need help.

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Post Reply
Message
Author
JackZ
Posts: 1852
Joined: Mon Feb 22, 2016 1:02 pm

SI Message_Port() need help.

#1 Post by JackZ » Sun Apr 18, 2021 7:35 pm

Hi Guys.

I think I need help on this one, as message port is new to me and I'm kinda lost.

I installed the example code from the wiki, downloaded the library and uploaded it to Arduino.

Code: Select all

/*
	Sim Innovations Message Port example:
	
	In this example we can communicate with Air Manager or Air Player over a standard USB cable.
	This is done using the Message Port library.
	
	See the code below on how to implement this library.
	
	More information on how to implement the Air Manager or Air Player side can be found here:
	https://siminnovations.com/wiki/index.php?title=Hw_message_port_add
	
	
	NOTE: 
	The Message Port library communicates with the PC using Serial Port 0 of the Arduino.
	Do not use Serial port 0 yourself!
*/

#include <si_message_port.hpp>

SiMessagePort* messagePort;

static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) {
	// Do something with a message from Air Manager or Air Player

	// The arguments are only valid within this function!
	// Make a clone if you want to store it

	if (payload == NULL) {
		messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received without payload");
	}
	else {
		switch(payload->type) {
			case SI_MESSAGE_PORT_DATA_TYPE_BYTE:
				messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " bytes: " + payload->data_byte[0]);
				break;
			case SI_MESSAGE_PORT_DATA_TYPE_STRING:
				messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received string: " + payload->data_string);
				break;
			case SI_MESSAGE_PORT_DATA_TYPE_INTEGER:
				messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " integers" + payload->data_int[0]);
				break;
			case SI_MESSAGE_PORT_DATA_TYPE_FLOAT:
				messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " floats" + payload->data_float[0]);
				break;
		}
	}
}

void setup() {
	// Init library on channel A and Arduino type MEGA 2560
	messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_MEGA_2560, SI_MESSAGE_PORT_CHANNEL_A, new_message_callback);
}

void loop() {
	// Make sure this function is called regularly
	messagePort->Tick();

	// You can send your own messages to Air Manager or Air Player
	messagePort->SendMessage(777,"RECEIVED");////////////////////////////////////// Message sent to AM
	//messagePort->SendMessage(123, "hello");
	//messagePort->SendMessage(123, (int32_t)1000);
	//messagePort->SendMessage(123, 2.5f);
	//messagePort->SendMessage(123, (uint8_t) 0xAA);
}
I then setup a simple test instrument in AM to test two ways communication.

Code: Select all

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

Arduino_id = hw_message_port_add("ARDUINO_MEGA2560_A", new_message)-- insert here the card and channel you are using.
---------------------------------------------------------------------------------------------------------
-- IMPORTANT NOTE: the Named hardware id doesn't work in AM 3;X, only in v4.0 and up.
-- Use Hardware id (like in the example above: "ARDUINO_MEGA2560_A" instead of a named hardware name.
 --------------------------------------------------------------------------------------------------------
 
-- Send a message to Air Manager with id 101 and an INT as a payload (value) every 500 msec 
timer_start(0,500, function()
i=i+1
hw_message_port_send(Arduino_id, 101, "INT", i)
end)
Nothing happens.
I declared the card in the instrument properties ARDUINO 2560, channel A
As far as I understand, i should have at least the debug messages in the console, correct?

Thanks for your help

Jacques
Last edited by JackZ on Mon Apr 19, 2021 11:58 am, edited 1 time in total.
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: 2371
Joined: Thu Nov 19, 2015 9:04 am

Re: SI Message_Port() need help.

#2 Post by Corjan » Sun Apr 18, 2021 7:42 pm

Hi,

You are overflowing the output buffer with your SendMessage.
Note that the loop function runs many many times a second.

Corjan

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

Re: SI Message_Port() need help.

#3 Post by JackZ » Sun Apr 18, 2021 7:48 pm

Thanks Corjan. I had missed that point. I changed the loop() as follows to send message every 1 sec if i did it corrcetly.
Still no luck.

Code: Select all

unsigned int previousMillis=0;
void loop() {
	// Make sure this function is called regularly
	messagePort->Tick();

	// You can send your own messages to Air Manager or Air Player
if (millis()-previousMillis>1000)	
    {
      previousMillis=millis();
      messagePort->SendMessage(777,"RECEIVED");
    }
	//messagePort->SendMessage(123, "hello");
	//messagePort->SendMessage(123, (int32_t)1000);
	//messagePort->SendMessage(123, 2.5f);
	//messagePort->SendMessage(123, (uint8_t) 0xAA);
}
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

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

Re: SI Message_Port() need help.

#4 Post by JackZ » Sun Apr 18, 2021 9:11 pm

Thanks @CorjanI had missed that point. I changed the loop() as follows to send message every 1 sec if i did it corrcetly.
Still no luck.

Code: Select all

unsigned int previousMillis=0;
void loop() {
	// Make sure this function is called regularly
	messagePort->Tick();

	// You can send your own messages to Air Manager or Air Player
if (millis()-previousMillis>1000)	
    {
      previousMillis=millis();
      messagePort->SendMessage(777,"RECEIVED");
    }
	//messagePort->SendMessage(123, "hello");
	//messagePort->SendMessage(123, (int32_t)1000);
	//messagePort->SendMessage(123, 2.5f);
	//messagePort->SendMessage(123, (uint8_t) 0xAA);
}
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: 2371
Joined: Thu Nov 19, 2015 9:04 am

Re: SI Message_Port() need help.

#5 Post by Corjan » Mon Apr 19, 2021 8:49 am

Hi,

Have you tried without named hardware?
So doing "ARDUINO_MEGA2560_A" or whatever you have.

Corjan

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

Re: SI Message_Port() need help.

#6 Post by JackZ » Mon Apr 19, 2021 9:14 am

@Corjan
It worked!
I changed the hw_message_port_add to use hardware I’d as you suggested, and I finally received the debug messages on the console! Would have never guessed it.
You really saved my day!

I gather that the named hardware os not properly working, but since it is now the recommended method, I went straight to this. Using Named hardware SEEMED to be working: Hardware properties were set as « Arduino mega 2560 » and « channel A », and the Arduino was recognized in the Device tab, so I couldn’t figure that might be the problem.

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

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

Re: SI Message_Port() need help.

#7 Post by Sling » Mon Apr 19, 2021 9:46 am

Ah just seen this. I just posted the same thing in the other thread. Glad you got it working. Now you can finish those projects. :D

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

Re: SI Message_Port() need help.

#8 Post by Corjan » Mon Apr 19, 2021 11:20 am

Hi,


Glad you got is sorted :).

Vaguely remember I fixed this someone at the start of AM 4.0 development.
Like said in the other thread, named message port does work in AM 4.0.


Corjan

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

Re: SI Message_Port() need help.

#9 Post by JackZ » Mon Apr 19, 2021 11:37 am

Thanks to all of you guys for your help.
Was completely stuck with this named hardware thing, and since this was the recommended method in the wiki, no way for me to figure it could be the culprit!
Glad to know that it is sorted out in AM 4.0

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

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests