Cyclone DDS Getting started

1 篇文章 0 订阅

系列文章目录

ROS getting started https://blog.csdn.net/surfaceyan/article/details/137162604
eProsima Fast DDS getting started https://blog.csdn.net/surfaceyan/article/details/137162608
Fast-DDS & ros2 ros与fastdds通讯 https://blog.csdn.net/surfaceyan/article/details/140621317


ZMQ Getting Started https://blog.csdn.net/surfaceyan/article/details/140072477
zmq socket 和 模式 https://blog.csdn.net/surfaceyan/article/details/140387789



前言


一、

使用Cyclone DDS的关键步骤:
https://cyclonedds.io/docs/cyclonedds/latest/getting_started/helloworld/helloworld_key_steps.html#key-steps

subscriber

  1. 至少创建三个DDS实体
  dds_entity_t participant;
  dds_entity_t topic;
  dds_entity_t reader;
  1. 解决数据,创建声明一些buffer
  HelloWorldData_Msg *msg;  // 用于收发的数据类型
  void *samples[MAX_SAMPLES];  // 必须要有接收数据缓存
  // 用户数据和元数据一起收发
  dds_sample_info_t infos[MAX_SAMPLES];  // 用于解决元数据问题
  1. 创建DDS participant
  participant = dds_create_participant (DDS_DOMAIN_DEFAULT, NULL, NULL);
  if (participant < 0)
    DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant));
  1. 创建Topic
    reader和writer匹配需要满足两个条件1.Topic相同 2.它们的data type 相同
  /* Create a Topic. */
  topic = dds_create_topic (
    participant, &HelloWorldData_Msg_desc, "HelloWorldData_Msg", NULL, NULL);
  if (topic < 0)
    DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-topic));
  1. 创建datareader
  /* Create a reliable Reader. */
  qos = dds_create_qos ();
  dds_qset_reliability (qos, DDS_RELIABILITY_RELIABLE, DDS_SECS (10));
  reader = dds_create_reader (participant, topic, qos, NULL);
  if (reader < 0)
    DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-reader));
  dds_delete_qos(qos);
  1. 为接收缓冲区分配空间
  /* Initialize sample buffer, by pointing the void pointer within
   * the buffer array to a valid sample memory location. */
  samples[0] = HelloWorldData_Msg__alloc ();
  1. 读取数据
  /* Poll until data has been read. */
  while (true)
  {
    /* Do the actual read.
     * The return value contains the number of read samples. 
     * 该调用是非阻塞的
     */
    rc = dds_read (reader, samples, infos, MAX_SAMPLES, MAX_SAMPLES);
    if (rc < 0)
      DDS_FATAL("dds_read: %s\n", dds_strretcode(-rc));

    /* Check if we read some data and it is valid. */
    if ((rc > 0) && (infos[0].valid_data))
    {
      /* Print Message. */
      msg = (HelloWorldData_Msg*) samples[0];
      printf ("=== [Subscriber] Received : ");
      printf ("Message (%"PRId32", %s)\n", msg->userID, msg->message);
      fflush (stdout);
      break;
    }
    else
    {
      /* Polling sleep. */
      dds_sleepfor (DDS_MSECS (20));
    }
  }
  1. 资源释放
  /* Free the data location. */
  HelloWorldData_Msg_free (samples[0], DDS_FREE_ALL);
    /* Deleting the participant will delete all its children recursively as well. */
  rc = dds_delete (participant);
  if (rc != DDS_RETCODE_OK)
    DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc));
    
return EXIT_SUCCESS;

publisher

与subscriber相似
在创建datawriter后:
定期查询status的状态

  rc = dds_set_status_mask(writer, DDS_PUBLICATION_MATCHED_STATUS);
  if (rc != DDS_RETCODE_OK)
    DDS_FATAL("dds_set_status_mask: %s\n", dds_strretcode(-rc));

  while(!(status & DDS_PUBLICATION_MATCHED_STATUS))
  {
    rc = dds_get_status_changes (writer, &status);
    if (rc != DDS_RETCODE_OK)
      DDS_FATAL("dds_get_status_changes: %s\n", dds_strretcode(-rc));

    /* Polling sleep. */
    dds_sleepfor (DDS_MSECS (20));
  }

发送信息赋值并发送

  /* Create a message to write. */
  msg.userID = 1;
  msg.message = "Hello World";

  rc = dds_write (writer, &msg);
  if (rc != DDS_RETCODE_OK)
    DDS_FATAL("dds_write: %s\n", dds_strretcode(-rc));

释放资源

  /* Deleting the participant will delete all its children recursively as well. */
  rc = dds_delete (participant);
  if (rc != DDS_RETCODE_OK)
    DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc));

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值