Skip to content
Snippets Groups Projects
port_handler_windows.c 8.68 KiB
Newer Older
leon's avatar
leon committed
/*******************************************************************************
* Copyright (c) 2016, ROBOTIS CO., LTD.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
*   list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
*   this list of conditions and the following disclaimer in the documentation
*   and/or other materials provided with the distribution.
*
* * Neither the name of ROBOTIS nor the names of its
*   contributors may be used to endorse or promote products derived from
*   this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

leon's avatar
leon committed
/* Author: Ryu Woon Jung (Leon) */
leon's avatar
leon committed

#if defined(_WIN32) || defined(_WIN64)
#define WINDLLEXPORT

#include <stdio.h>
#include <string.h>
#include <time.h>
leon's avatar
leon committed
#define LATENCY_TIMER  16 // msec (USB latency timer)

typedef struct
{
leon's avatar
leon committed
  HANDLE  serial_handle;
  LARGE_INTEGER freq, counter;
leon's avatar
leon committed
  int     baudrate;
leon's avatar
leon committed
  double  packet_start_time;
  double  packet_timeout;
  double  tx_time_per_byte;
}PortData;
leon's avatar
leon committed
static PortData *portData;
int portHandlerWindows(const char *port_name)
leon's avatar
leon committed
  int port_num;
  char buffer[15];
leon's avatar
leon committed

  sprintf_s(buffer, sizeof(buffer), "\\\\.\\%s", port_name);
leon's avatar
leon committed

leon's avatar
leon committed
  if (portData == NULL)
leon's avatar
leon committed
  {
leon's avatar
leon committed
    port_num = 0;
    g_used_port_num = 1;
    portData = (PortData*)calloc(1, sizeof(PortData));
    g_is_using = (uint8_t*)calloc(1, sizeof(uint8_t));
leon's avatar
leon committed
  }
  else
  {
leon's avatar
leon committed
    for (port_num = 0; port_num < g_used_port_num; port_num++)
leon's avatar
leon committed
    {
      if (!strcmp(portData[port_num].port_name, buffer))
leon's avatar
leon committed
        break;
    }
leon's avatar
leon committed
    if (port_num == g_used_port_num)
leon's avatar
leon committed
      for (port_num = 0; port_num < g_used_port_num; port_num++)
leon's avatar
leon committed
      {
leon's avatar
leon committed
        if (portData[port_num].serial_handle != INVALID_HANDLE_VALUE)
leon's avatar
leon committed
          break;
      }

leon's avatar
leon committed
      if (port_num == g_used_port_num)
leon's avatar
leon committed
      {
leon's avatar
leon committed
        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));
leon's avatar
leon committed
      }
leon's avatar
leon committed
      printf("[PortHandler setup] The port number %d has same device name... reinitialize port number %d!!\n", port_num, port_num);
leon's avatar
leon committed
  }
leon's avatar
leon committed
  portData[port_num].serial_handle = INVALID_HANDLE_VALUE;
  portData[port_num].baudrate = DEFAULT_BAUDRATE;
  portData[port_num].packet_start_time = 0.0;
  portData[port_num].packet_timeout = 0.0;
  portData[port_num].tx_time_per_byte = 0.0;
leon's avatar
leon committed
  g_is_using[port_num] = False;
  setPortNameWindows(port_num, buffer);
leon's avatar
leon committed
  return port_num;
leon's avatar
leon committed
uint8_t openPortWindows(int port_num)
leon's avatar
leon committed
  return setBaudRateWindows(port_num, portData[port_num].baudrate);
void closePortWindows(int port_num)
leon's avatar
leon committed
  if (portData[port_num].serial_handle != INVALID_HANDLE_VALUE)
leon's avatar
leon committed
  {
leon's avatar
leon committed
    CloseHandle(portData[port_num].serial_handle);
    portData[port_num].serial_handle = INVALID_HANDLE_VALUE;
leon's avatar
leon committed
  }
void clearPortWindows(int port_num)
leon's avatar
leon committed
  PurgeComm(portData[port_num].serial_handle, PURGE_RXABORT | PURGE_RXCLEAR);
void setPortNameWindows(int port_num, const char *port_name)
leon's avatar
leon committed
  strcpy_s(portData[port_num].port_name, sizeof(portData[port_num].port_name), port_name);
char *getPortNameWindows(int port_num)
leon's avatar
leon committed
  return portData[port_num].port_name;
leon's avatar
leon committed
uint8_t setBaudRateWindows(int port_num, const int baudrate)
leon's avatar
leon committed
  closePortWindows(port_num);
leon's avatar
leon committed
  portData[port_num].baudrate = baudrate;
leon's avatar
leon committed
  return setupPortWindows(port_num, baudrate);
int getBaudRateWindows(int port_num)
leon's avatar
leon committed
  return portData[port_num].baudrate;
int readPortWindows(int port_num, uint8_t *packet, int length)
leon's avatar
leon committed
  DWORD dwRead = 0;
leon's avatar
leon committed
  if (ReadFile(portData[port_num].serial_handle, packet, (DWORD)length, &dwRead, NULL) == FALSE)
leon's avatar
leon committed
    return -1;
leon's avatar
leon committed
  return (int)dwRead;
int writePortWindows(int port_num, uint8_t *packet, int length)
leon's avatar
leon committed
  DWORD dwWrite = 0;
leon's avatar
leon committed
  if (WriteFile(portData[port_num].serial_handle, packet, (DWORD)length, &dwWrite, NULL) == FALSE)
leon's avatar
leon committed
    return -1;
leon's avatar
leon committed
  return (int)dwWrite;
void setPacketTimeoutWindows(int port_num, uint16_t packet_length)
leon's avatar
leon committed
  portData[port_num].packet_start_time = getCurrentTimeWindows(port_num);
  portData[port_num].packet_timeout = (portData[port_num].tx_time_per_byte * (double)packet_length) + (LATENCY_TIMER * 2.0) + 2.0;
void setPacketTimeoutMSecWindows(int port_num, double msec)
leon's avatar
leon committed
  portData[port_num].packet_start_time = getCurrentTimeWindows(port_num);
  portData[port_num].packet_timeout = msec;
leon's avatar
leon committed
uint8_t isPacketTimeoutWindows(int port_num)
leon's avatar
leon committed
  if (getTimeSinceStartWindows(port_num) > portData[port_num].packet_timeout)
leon's avatar
leon committed
  {
leon's avatar
leon committed
    portData[port_num].packet_timeout = 0;
    return True;
leon's avatar
leon committed
  }
leon's avatar
leon committed
  return False;

double getCurrentTimeWindows(int port_num)
leon's avatar
leon committed
  QueryPerformanceCounter(&portData[port_num].counter);
  QueryPerformanceFrequency(&portData[port_num].freq);
  return (double)portData[port_num].counter.QuadPart / (double)portData[port_num].freq.QuadPart * 1000.0;
double getTimeSinceStartWindows(int port_num)
leon's avatar
leon committed
  double time_since_start;
leon's avatar
leon committed
  time_since_start = getCurrentTimeWindows(port_num) - portData[port_num].packet_start_time;
  if (time_since_start < 0.0)
    portData[port_num].packet_start_time = getCurrentTimeWindows(port_num);
leon's avatar
leon committed
  return time_since_start;
leon's avatar
leon committed
uint8_t setupPortWindows(int port_num, const int baudrate)
leon's avatar
leon committed
  DCB dcb;
  COMMTIMEOUTS timeouts;
  DWORD dwError;
leon's avatar
leon committed

leon's avatar
leon committed
  closePortWindows(port_num);
leon's avatar
leon committed
  portData[port_num].serial_handle = CreateFileA(portData[port_num].port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (portData[port_num].serial_handle == INVALID_HANDLE_VALUE)
leon's avatar
leon committed
  {
    printf("[PortHandlerWindows::SetupPort] Error opening serial port!\n");
leon's avatar
leon committed
    return False;
leon's avatar
leon committed
  }

  dcb.DCBlength = sizeof(DCB);
leon's avatar
leon committed
  if (GetCommState(portData[port_num].serial_handle, &dcb) == FALSE)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;

  // Set baudrate
  dcb.BaudRate = (DWORD)baudrate;
  dcb.ByteSize = 8;                    // Data bit = 8bit
  dcb.Parity = NOPARITY;             // No parity
  dcb.StopBits = ONESTOPBIT;           // Stop bit = 1
  dcb.fParity = NOPARITY;             // No Parity check
  dcb.fBinary = 1;                    // Binary mode
  dcb.fNull = 0;                    // Get Null byte
  dcb.fAbortOnError = 0;
  dcb.fErrorChar = 0;
  // Not using XOn/XOff
  dcb.fOutX = 0;
  dcb.fInX = 0;
  // Not using H/W flow control
  dcb.fDtrControl = DTR_CONTROL_DISABLE;
  dcb.fRtsControl = RTS_CONTROL_DISABLE;
  dcb.fDsrSensitivity = 0;
  dcb.fOutxDsrFlow = 0;
  dcb.fOutxCtsFlow = 0;

leon's avatar
leon committed
  if (SetCommState(portData[port_num].serial_handle, &dcb) == FALSE)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;

leon's avatar
leon committed
  if (SetCommMask(portData[port_num].serial_handle, 0) == FALSE) // Not using Comm event
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;
leon's avatar
leon committed
  if (SetupComm(portData[port_num].serial_handle, 4096, 4096) == FALSE) // Buffer size (Rx,Tx)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;
leon's avatar
leon committed
  if (PurgeComm(portData[port_num].serial_handle, PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR) == FALSE) // Clear buffer
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;
leon's avatar
leon committed
  if (ClearCommError(portData[port_num].serial_handle, &dwError, NULL) == FALSE)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;

leon's avatar
leon committed
  if (GetCommTimeouts(portData[port_num].serial_handle, &timeouts) == FALSE)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;
  // Timeout (Not using timeout)
  // Immediatly return
  timeouts.ReadIntervalTimeout = 0;
  timeouts.ReadTotalTimeoutMultiplier = 0;
  timeouts.ReadTotalTimeoutConstant = 1; // must not be zero.
  timeouts.WriteTotalTimeoutMultiplier = 0;
  timeouts.WriteTotalTimeoutConstant = 0;
leon's avatar
leon committed
  if (SetCommTimeouts(portData[port_num].serial_handle, &timeouts) == FALSE)
leon's avatar
leon committed
    goto DXL_HAL_OPEN_ERROR;

leon's avatar
leon committed
  portData[port_num].tx_time_per_byte = (1000.0 / (double)portData[port_num].baudrate) * 10.0;
  return True;
leon's avatar
leon committed

  DXL_HAL_OPEN_ERROR:
    closePortWindows(port_num);

leon's avatar
leon committed
  return False;