eProsima / Micro-XRCE-DDS

An XRCE DDS implementation. Looking for commercial support? Contact info@eprosima.com
Apache License 2.0
156 stars 17 forks source link

Trying to get serial Ping Example to work #177

Open BoilermakerTobes opened 3 weeks ago

BoilermakerTobes commented 3 weeks ago

I am struggling to get the PingAgent Serial example to work.

I compiled the example using

gcc main.c -lmicrocdr -lmicroxrcedds_client

I then ran it using the following command: ./a.out /dev/ttyUSB0 where I have my serial device ftdi on running at 57600 baud

On my agent side I ran both sudo MicroXRCEAgent serial --dev /dev/ttyUSB1

sudo MicroXRCEAgent serial --dev /dev/ttyUSB1 -b 57600 where I gave a baud rate

However I keep on getting "Sorry, no agent available at device '/dev/ttyUSB0'" I know that I have a connection because I can see lights blinking on the TX side of my client and RX side of my Agent during the ping. Am I doing something wrong with the setup of my client and agent? Is there another example I can look at for serial communication?

BoilermakerTobes commented 3 weeks ago

I also tried to modify the HelloWorldPublisher to serial, but got "Error at create session."

Here is what I was running for my main.c for PublishHwlloWorldSerial:

`// Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima). // // 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.

include "HelloWorld.h"

include <uxr/client/client.h>

include <ucdr/microcdr.h>

include //printf

include //strcmp

include //atoi

include <uxr/client/profile/transport/serial/serial_transport_platform.h>

include // For open()

include // For close>

include // For setting baud rate

define STREAM_HISTORY 8

define BUFFER_SIZE UXR_CONFIG_SERIAL_TRANSPORT_MTU * STREAM_HISTORY

int set_baud_rate(int fd, int baud_rate) { struct termios tty; if (tcgetattr(fd, &tty) != 0) { return -1; }

cfsetospeed(&tty, baud_rate);
cfsetispeed(&tty, baud_rate);

if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    return -1;
}

return 0;

}

int main(int args, char** argv) { // CLI if (args < 2) { printf("usage: program [-h | --help] []\n"); return 0; }

uint32_t max_topics = (args == 2) ? (uint32_t)atoi(argv[1]) : UINT32_MAX;

// Transport
uxrSerialTransport transport;
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC); //Just to be simple I set fixed /dev/ttyUSB0
if (fd < 0) {
    // Handle error
    perror("Error opening serial port");
    return -1;
}

if (set_baud_rate(fd, B57600) != 0) { //just to be simple, I set a fixed baud rate
    // Handle error
    perror("Error setting baud rate");
    close(fd);
    return -1;
}

uint8_t remote_addr = 0x01; // Is 1 just the default? Not very sure about this
if (!uxr_init_serial_transport(&transport, fd, remote_addr, 0)) {
    // Handle error
    perror("Error initializing serial transport");
    close(fd);
    return -1;
}

// Session
uxrSession session;
uxr_init_session(&session, &transport.comm, 0xAAAABBBB); // Not sure about 0xAAAABBBB and what it means for session
if (!uxr_create_session(&session)) {
    printf("Error at create session.\n");
    return 1;
}

// Streams
uint8_t output_reliable_stream_buffer[BUFFER_SIZE];
uxrStreamId reliable_out = uxr_create_output_reliable_stream(&session, output_reliable_stream_buffer, BUFFER_SIZE, STREAM_HISTORY);

uint8_t input_reliable_stream_buffer[BUFFER_SIZE];
uxr_create_input_reliable_stream(&session, input_reliable_stream_buffer, BUFFER_SIZE, STREAM_HISTORY);

// Create entities
uxrObjectId participant_id = uxr_object_id(0x01, UXR_PARTICIPANT_ID);
const char* participant_xml = "<dds>"
                              "<participant>"
                              "<rtps>"
                              "<name>default_xrce_participant</name>"
                              "</rtps>"
                              "</participant>"
                              "</dds>";
uint16_t participant_req = uxr_buffer_create_participant_xml(&session, reliable_out, participant_id, 0, participant_xml, UXR_REPLACE);

uxrObjectId topic_id = uxr_object_id(0x01, UXR_TOPIC_ID);
const char* topic_xml = "<dds>"
                        "<topic>"
                        "<name>HelloWorldTopic</name>"
                        "<dataType>HelloWorld</dataType>"
                        "</topic>"
                        "</dds>";
uint16_t topic_req = uxr_buffer_create_topic_xml(&session, reliable_out, topic_id, participant_id, topic_xml, UXR_REPLACE);

uxrObjectId publisher_id = uxr_object_id(0x01, UXR_PUBLISHER_ID);
const char* publisher_xml = "";
uint16_t publisher_req = uxr_buffer_create_publisher_xml(&session, reliable_out, publisher_id, participant_id, publisher_xml, UXR_REPLACE);

uxrObjectId datawriter_id = uxr_object_id(0x01, UXR_DATAWRITER_ID);
const char* datawriter_xml = "<dds>"
                             "<data_writer>"
                             "<topic>"
                             "<kind>NO_KEY</kind>"
                             "<name>HelloWorldTopic</name>"
                             "<dataType>HelloWorld</dataType>"
                             "</topic>"
                             "</data_writer>"
                             "</dds>";
uint16_t datawriter_req = uxr_buffer_create_datawriter_xml(&session, reliable_out, datawriter_id, publisher_id, datawriter_xml, UXR_REPLACE);

// Send create entities message and wait its status
uint8_t status[4];
uint16_t requests[4] = {participant_req, topic_req, publisher_req, datawriter_req};
if (!uxr_run_session_until_all_status(&session, 1000, requests, status, 4)) {
    printf("Error at create entities: participant: %i topic: %i publisher: %i datawriter: %i\n", status[0], status[1], status[2], status[3]);
    return 1;
}

// Write topics
bool connected = true;
uint32_t count = 0;
while (connected && count < max_topics) {
    HelloWorld topic = {++count, "Hello DDS world!"};

    ucdrBuffer ub;
    uint32_t topic_size = HelloWorld_size_of_topic(&topic, 0);
    uxr_prepare_output_stream(&session, reliable_out, datawriter_id, &ub, topic_size);
    HelloWorld_serialize_topic(&ub, &topic);

    printf("Send topic: %s, id: %i\n", topic.message, topic.index);
    connected = uxr_run_session_time(&session, 1000);
}

// Delete resources
uxr_delete_session(&session);
uxr_close_serial_transport(&transport);
close(fd);
return 0;

} `