Skip to content

Cannot open multiple ports at the same time on C version (with possible solution) #670

Description

@strontiumpku

Hi team,

Thank you for the great servos and the well-maintained open source codes. Recently I noticed that I cannot connect and control two or more serial ports simultanously by using C version SDK (and C# P/invoke as well) on Windows.

How to reproduce the issue
In the example C program for multi-port handling, we should create a PortHandler for EACH serial port by the following code:

  int port_num1 = portHandler(DEVICENAME1);
  int port_num2 = portHandler(DEVICENAME2);

However in the real world, port_num2 returns 0 (which is equal to port_num1), thus the access to the first device is lost. I believe port_num2 should be 1 by design.

My work-around
After digging into the source code, I found the following part of portHandlerWindows function in DynamixelSDK/c/src/dynamixel_sdk/port_handler_windows.c might be the issue.

      for (port_num = 0; port_num < g_used_port_num; port_num++)
      {
        if (portData[port_num].serial_handle != INVALID_HANDLE_VALUE)         // ******* Should be == rather than != ?
          break;
      }

      if (port_num == g_used_port_num)
      {
        g_used_port_num++;
        portData = (PortData*)realloc(portData, g_used_port_num * sizeof(PortData));
        g_is_using = (uint8_t*)realloc(g_is_using, g_used_port_num * sizeof(uint8_t));
      }

If the first serial port opens correctly and gets a valid handle, the validation in Line 78 breaks the for-loop, leaving port_num as 0 at the second device, thus mistakenly writes the information for the second port to portData[0]. By changing != to == in Line 78 solves my problem (seemingly).

Please help review this. If this is the case, the Linux/mac version might also need update respondingly.

Best,
Yusi

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions