xrce nano-client via serial - only 4 messages published?

1 post / 0 new
Offline
Last seen: 8 months 3 weeks ago
Joined: 08/03/2023
Posts: 1
xrce nano-client via serial - only 4 messages published?

I'm using the xrce nano client with Arduino via serial (USB serial to be exact) on an Arduino Due. I set LWIP and IPv4 options to disabled because the board does not have wifi. I am trying to use a slightly modified version of the sensor-writer-serial example below. It appears that the writer sends out only 4 messages every time I reset the board. (I only see the TX light flicker 4 times and see 4 messages in RTI Admin console, but the onboard LED that I'm using for a heartbeat keeps on going). Note that this only worked after I switched to a best effort stream_id. I have tried playing with all the delays and MTU, but it always seems to be just 4 messages. Any ideas? Appreciate any help!

/******************************************************************************
 *
 * (c) 2020 Copyright, Real-Time Innovations, Inc. (RTI)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

/******************************************************************************
 Start nanoagentd with the following command (change paths and other arguments
 according to your environment):
 
   nanoagentd -S -Sd /dev/ttyUSB0 -Ss 115200 -St 1000 \
              -a -c nano-client-arduino/extras/sensor_agent.xml
 
 ******************************************************************************/

#include <nano_client_arduino.h>

#define SENSOR_ID           0x00000001
#define CLIENT_KEY          0x01020304
#define WRITER_ID           0x4065
#define READER_ID           0x5BB6
#define TRANSPORT_MTU       128
#define PUBLISH_DELAY       100
#define INIT_DELAY          5000
#define STREAM_ID           0x0A
#define SERIAL_SPEED        115200

XrceData transport_recv_buffer[
            XRCE_TRANSPORT_RECV_BUFFER_SIZE(TRANSPORT_MTU)] = { 0 };

XrceSerialTransport transport(
    transport_recv_buffer, sizeof(transport_recv_buffer));

XrceClient client(transport, CLIENT_KEY);

XrceReliableStream stream(client, STREAM_ID);

XrceDataWriter writer(client, 
    WRITER_ID,
    XRCE_OBJECT_ID_INVALID,
    NULL,
    false,
    STREAM_ID);

XrceDataReader reader(client, READER_ID);


struct SensorData
{
    uint8_t id[4];
    uint32_t value;
};

SensorData data;

/**
 * Listener callback used to notify the application of
 * new XRCE data received by the client.
 */
void on_data(
    void *const listener,
    XrceClient& client,
    const XrceReceivedData& recv_data)
{
    SensorData data;
    const uint8_t *data_ptr = recv_data.data();
    memcpy(data.id, data_ptr, sizeof(data.id));
    data_ptr += sizeof(data.id);
    NANO_u32_deserialize(
        &data.value, data_ptr, recv_data.little_endian());

    /* Do something with deserialized data */
}

void serial_init()
{
  Serial.begin(SERIAL_SPEED);
  while(!Serial && !Serial.available()){
    delay(INIT_DELAY); // Required to wait for USB device to be ready
  }
}

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
    
    serial_init();

    /* Initialize XRCE client and connect to XRCE agent */
    if (!client.initialize())
    {
        // Failed to initialize XRCE client
        while (1) {}
    }
    stream.enable();

    while (!client.connected())
    {
        client.connect(INIT_DELAY);
    }

    /* Initialize sensor data */
    data.value = 0;
    /* Store sensor id as big endian */
    NANO_u32_serialize(SENSOR_ID, data.id, false);
}

void loop()
{
    data.value += 1;
    writer.write_data((uint8_t*)&data, sizeof(data));
    delay(PUBLISH_DELAY);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(PUBLISH_DELAY);
    digitalWrite(LED_BUILTIN, LOW);
}
Organization: