#include <stdio.h>
#include <conio.h>
#include "dxlib2.h"

void main (void) {
  int f = 1;
  short pos;
  DXDEVICEID  dev;
  TDxErrorCode  err;

  if ((dev = DX_OpenPort ("\\\\.\\COM11", 57143))) {
    printf("Open success\n");

    if (DX_Ping (dev, 1, 1000, &err)) {
      // 現在の位置を読み出し
      if (DX_ReadWordData (dev, 1, 36, (USHORT *)&pos, 100, &err)) {
        printf("Now position = %4d\n",pos);
        while (1) {
          pos += f;
          if (pos > 1023) { pos = 1023; f = -1; }
          else if (pos < 0) { pos = 0; f = 1; }
          if (DX_WriteWordData (dev, 1, 30, pos, 100, &err)) printf("Sent position = %d\r",pos);
          // 5ミリ秒待ち
          Sleep (5);
          // 何かキーを押すとループを抜ける
          if (kbhit ()) break;
        }
        printf("\n");
      }
    }

    DX_ClosePort(dev);

  } else {
    printf("Open error\n");
  }
  printf( "Fin\n");
}
