直接使用WriteFile也是返回false,GetLastError函数返回ERROR_INVALID_FUNCTION ,代码及输出如下:
Device Found
type: 1ff7 0001
path: \\?\hid#vid_1ff7&pid_0001&mi_00&col06#7&3683c34&0&0005#{4d1e55b2-f16f-11cf-88cb-001111000030}
serial_number: 00000000001A
Manufacturer: Touch Devic
Product: Touch Device
Manufacturer String: Touch Devic
Product String: Touch Device
Serial Number String: 00000000001A
Write Res = -1,error:函数不正确。
Read Ret = 0
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include "touchdevice.h"
#include
#include "../HidLib/hidapi.h"
#define MAX_STR 255
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
UCHAR commandbuf[64] = {0};
UCHAR rebuf[64] = {0};
int res;
unsigned char buf[64];
wchar_t wstr[MAX_STR];
wchar_t *str_s = NULL;
hid_device *handle;
int i;
// Enumerate and print the HID devices on the system
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(TOUCH_ViD, TOUCH_PiD_1);
cur_dev = devs;
while (cur_dev) {
qDebug("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
qDebug("\n");
qDebug(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
qDebug(" Product: %ls\n", cur_dev->product_string);
qDebug("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
// Open the device using the VID, PID,
// and optionally the Serial number.
handle = hid_open(TOUCH_ViD, TOUCH_PiD_1, NULL);
// Read the Manufacturer String
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
qDebug("Manufacturer String: %ls\n", wstr);
// Read the Product String
res = hid_get_product_string(handle, wstr, MAX_STR);
qDebug("Product String: %ls\n", wstr);
// Read the Serial Number String
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
qDebug("Serial Number String: %ls", wstr);
qDebug("\n");
// Send a Feature Report to the device
buf[0] = (INITIATIVE_REPORT); // First byte is report number
buf[1] = (VERSION);
buf[2] = (VER_BOARD_TYPE_GET);
// Set the hid_read() function to be non-blocking.
hid_set_nonblocking(handle, 1);
res = hid_write(handle, buf, 64);
qDebug("Write Res = %d,error:%ls",res,hid_error(handle));
// Read requested state
res = hid_read_timeout(handle, buf, 64,100);
if (res < 0)
{
qDebug("Unable to read()\n");
}
else
{
qDebug("Read Ret = %d\n",res);
}
// Print out the returned buffer.
for (i = 0; i < res; i++)
qDebug("buf[%d]: %d\n", i, buf[i]);
hid_close(handle);
}
MainWindow::~MainWindow()
{
delete ui;
}