Receiving a message, The msg_arrives event – Echelon Neuron C User Manual

Page 138

Advertising
background image

126

How Devices Communicate Using Application Messages

void msg_cancel(void);

This function cancels the message being built for the msg_out object and frees the
associated buffer, allowing another message to be constructed. It has no

parameters, and has no return value.
If a message is constructed but not sent before the task is exited, the message is
automatically canceled.
The first write operation to the msg_out object triggers automatic, implicit, buffer
allocation. Because all buffers could be busy at that moment, the time taken to

complete the first assignment is non-deterministic. For applications where a

non-deterministic waiting period is not acceptable, the msg_alloc( ) function is
supported and allows for explicit message allocation. The msg_alloc( ) function

does not wait for any pending transaction to complete, but returns immediately

with success or failure. See the

Neuron C Reference Guide

for more information

about this function.

Receiving a Message

You typically receive a message using the msg_arrives predefined event. You can

also use the msg_receive( ) function to receive a message.

The msg_arrives Event

The predefined event for receiving a message is msg_arrives. Its syntax is:

msg_arrives [(

message-code

)]

If a message arrives, this event evaluates to TRUE. You can optionally qualify
the event using a message code. In this case, the event is TRUE only when a

message arrives containing the specified code.
When mixing unqualified msg_arrives events with qualified msg_arrives events,
the #pragma scheduler_reset directive must be specified so that the unqualified

event when clause is processed after all the qualified event when clauses.

It is essential that your program contain a default case as shown in the example

below, to prevent an event queue lockup. This issue is explained in detail in

Importance of a Default When Clause

on page 128.

A sample use of this event is shown in Listing 3 below.
Listing 3. Use of msg_arrives Event

#pragma scheduler_reset

when (msg_arrives(1))
{
io_out(sprinkler,

ON);

}

when (msg_arrives(2))
{
io_out(sprinkler,

OFF);

}

// default case for handling unexpected message codes
when (msg_arrives)

Advertising