H21/3/11 WinUSBを用いた通信

#include<windows.h>
#include<WINUSB.H>
#include<usb100.h>
#include<setupapi.h>
#include<strsafe.h>

struct devInfo{
	HANDLE deviceHandle;
	WINUSB_INTERFACE_HANDLE winUSBHandle;
	unsigned char bulkInPipe;
	unsigned char bulkOutPipe;
	unsigned char interruptPipe;
	int deviceSpeed;   
}devInfo;

struct devInfo deviceInformation;
USB_ENDPOINT_DESCRIPTOR usbEndpointD;

//デバイスパスの取得
BOOL GetDevicePath(LPGUID InterfaceGuid, PCHAR DevicePath, size_t BufLen)
{
  BOOL bResult = FALSE;
  HDEVINFO deviceInfo;
  SP_DEVICE_INTERFACE_DATA interfaceData;
  PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL;
  ULONG length;
  ULONG requiredLength=0;
  HRESULT hr;
  deviceInfo = SetupDiGetClassDevs(InterfaceGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  bResult = SetupDiEnumDeviceInterfaces(deviceInfo, NULL, InterfaceGuid, 0, &interfaceData); 
  SetupDiGetDeviceInterfaceDetail(deviceInfo, &interfaceData, NULL, 0, &requiredLength, NULL);
  detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, requiredLength);
  if(NULL == detailData)
  {
    SetupDiDestroyDeviceInfoList(deviceInfo);
    return FALSE;
  }
  detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  length = requiredLength;
  bResult = SetupDiGetDeviceInterfaceDetail(deviceInfo, &interfaceData, detailData, length, &requiredLength, NULL);
  if(FALSE == bResult)
  {
    LocalFree(detailData);
    return FALSE;
  }
  hr = StringCchCopy(DevicePath, BufLen, detailData->DevicePath);
  if(FAILED(hr))
  {
    SetupDiDestroyDeviceInfoList(deviceInfo);
    LocalFree(detailData);
  }
  LocalFree(detailData);
  return bResult;
}

//デバイスのファイルハンドルの取得
HANDLE OpenDevice()
{
  HANDLE hDev = NULL;
  char path[512];
  BOOL retVal;  

  GUID guid = {
    0x1F1F1F1F,
    0x1F1F,
    0x1F1F,
    { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F}
  };
  retVal = GetDevicePath(&guid, path, 512);
  hDev = CreateFile(path,
                    GENERIC_WRITE | GENERIC_READ,
                    FILE_SHARE_WRITE | FILE_SHARE_READ,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                    NULL);
  return hDev;
}

//WinUSBの初期化
BOOL Initialize_Device()
{
  BOOL bResult;
  WINUSB_INTERFACE_HANDLE usbHandle;
  USB_INTERFACE_DESCRIPTOR ifaceDescriptor;
  WINUSB_PIPE_INFORMATION pipeInfo;
  UCHAR speed;
  ULONG length;
  
//WinUSB初期化
  deviceInformation.winUSBHandle = OpenDevice(TRUE);
  bResult = WinUsb_Initialize(deviceInformation.winUSBHandle, &usbHandle);

  if(bResult)
  {
    deviceInformation.winUSBHandle = usbHandle;
    length = sizeof(UCHAR);
    bResult = WinUsb_QueryDeviceInformation(deviceInformation.winUSBHandle,
                                            DEVICE_SPEED,
                                            &length,
                                            &speed); 
  }

  if(bResult)
  {
    deviceInformation.deviceSpeed = speed;
    bResult = WinUsb_QueryInterfaceSettings(deviceInformation.winUSBHandle,
                                            0,
                                            &ifaceDescriptor);

  }
  if(bResult)
  {
    int i;
    for(i=0;i<ifaceDescriptor.bNumEndpoints;i++)
    {
      bResult = WinUsb_QueryPipe(deviceInformation.winUSBHandle,
                                 0,
                                 (UCHAR) i,
                                 &pipeInfo);

      if(pipeInfo.PipeType == UsbdPipeTypeBulk &&
                  USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId))
      {
        deviceInformation.bulkInPipe = pipeInfo.PipeId;
      }
      else if(pipeInfo.PipeType == UsbdPipeTypeBulk &&
                  USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId))
      {
          
        deviceInformation.bulkOutPipe = pipeInfo.PipeId;
      }
      else if(pipeInfo.PipeType == UsbdPipeTypeInterrupt)
      {
        deviceInformation.interruptPipe = pipeInfo.PipeId;
      }
      else
      {
        bResult = FALSE;
        break;
      }
    }
  }
  return bResult;
}

//エンドポイントの設定
BOOL SetupEndPoint(){

  BOOL bResult;
  ULONG bytesReturned;
  WINUSB_SETUP_PACKET setupPacket;
  UCHAR lightedBars = 0;

  setupPacket.RequestType = 0;
  setupPacket.Request = 0x08;
  setupPacket.Index = 0;
  setupPacket.Length = sizeof(UCHAR);
  setupPacket.Value = 0;
  bResult = WinUsb_ControlTransfer(deviceInformation.winUSBHandle,
                                   setupPacket,
                                   &lightedBars,
                                   sizeof(UCHAR),
                                   &bytesReturned,
                                   NULL);
  return bResult;
}

//USBとの通信(読み込み)
int ReadFromDevice()
{
  BOOL bResult;
  UCHAR lightedBars = 0;
 
  USHORT bufsize = 12;
  UCHAR szBuffer[12];
  ULONG bytesRead;  
  usbEndpointD.bEndpointAddress = 0xf1;
  bResult = WinUsb_ReadPipe(deviceInformation.winUSBHandle,
                            usbEndpointD.bEndpointAddress,
                            szBuffer,
                            1,
                            &bytesRead,
                            NULL);
  if(bResult != 1){
    return -1;
  }
  else{
    return szBuffer[0];
  }
}

int WriteToDevice(unsigned char data)
{
  BOOL bResult;
  USHORT bufSize = 12;
  UCHAR szBuffer[12];
  ULONG bytesWritten;
  
  szBuffer[0] = data; 
  usbEndpointD.bEndpointAddress = 0x01;  
  bResult = WinUsb_WritePipe(deviceInformation.winUSBHandle,
                             usbEndpointD.bEndpointAddress,
                             szBuffer,
                             1,
                             &bytesWritten,
                             NULL);
  
  return bResult;
 
}   

Read関数とWrite関数の違い

簡単に言えば,

usbEndpointD.bEndpointAddressが0xf1か0x01かの違い

だけです.
0x(IN or OUT)(エンドポイント)のようです.

最後に流れを

Initialize_Device();

SetupEndPoint();

Read,Write関数で通信

この3つです.

それから

再試なしで進級できました.
無事高専4年になれます.
これからもよろしくお願いします.