Request/response examples – Echelon Neuron C User Manual

Page 152

Advertising
background image

140

How Devices Communicate Using Application Messages

To use this field, you must include the <addrdefs.h> and

<msg_addr.h

>

files

.

Request/Response Examples

This example shows sending a request and asynchronously receiving the

responses. The code for receiving this request and responding to it follows in the

next example.

msg_tag tag1;
#define DATA_REQUEST 0

when (io_changes(toggle))
{
msg_out.tag = TAG1;
msg_out.code = DATA_REQUEST;
msg_out.service = REQUEST;
msg_send();
}

when (resp_arrives(TAG1))
{
if (resp_in.code == OK)
process_response(resp_in.data[0]);
}

Here is the code for the responder to this request:

#define DATA_REQUEST 0
#define OK 1

when (msg_arrives(DATA_REQUEST))
{

int x, y;

x = msg_in.data[0];

y = get_response(x);


resp_out.code = OK;

// msg_in no longer available


resp_out.data[0] = y;

resp_send();
}

The following example shows sending a request and receiving the responses

directly:

int x;
msg_tag motor;
#define MOTOR_ON 0
#define DO_MOTOR_ON 3

when (command == DO_MOTOR_ON)
{
// send a request
msg_out.tag = motor; // construct the message
msg_out.code = MOTOR_ON;
msg_out.service = REQUEST;

Advertising