返回> 网站首页 

CP2110芯片HID使用

yoours2017-01-04 18:25:30 阅读 2461

简介一边听听音乐,一边写写文章。

一、CP2110
CP2110为USB接口的hid通讯,免驱动;支持Linux和windows平台

二、linux下编译
下载libusb-1.0.20、slabhiddevice、slabhidtouart

三、使用
BOOL Open(HID_UART_DEVICE* fd)
{
HID_UART_STATUS status = HID_UART_DEVICE_NOT_FOUND;
DWORD numDevices = 0;

if (HidUart_GetNumDevices(&numDevices, VID, PID) == HID_UART_SUCCESS)
{
printf("HidUart_GetNumDevices=%d VID:%04X PID:%04X\n", numDevices, VID, PID);
for (DWORD i = 0; i < numDevices; i++)
{
HID_UART_DEVICE_STR deviceString;

// Search through all HID devices for a matching serial string
if (HidUart_GetString(i, VID, PID, deviceString, HID_UART_GET_SERIAL_STR) == HID_UART_SUCCESS)
{
printf("HidUart_GetString\n");
// Found a matching device
if (serial == deviceString)
{
// Open the device
status = HidUart_Open(fd, i, VID, PID);
printf("Hid_Open=%d\n", status);
break;
}
}
printf("HidUart_GetString=%d\n", status);
}
}

// Found and opened the device
if (status == HID_UART_SUCCESS)
{
// Get part number and version
//status = HidUart_GetPartNumber(fd, &m_partNumber, &m_version);
}
// Got part number
if (status == HID_UART_SUCCESS)
{
status = HidUart_SetUartConfig(*fd, 57600, 3, 0, 0, 0);//很重要,8-5=3、  1停止位=0
printf("Configure the UART status=%02X\n", status);
}

// Configured the UART
if (status == HID_UART_SUCCESS)
{
// Set short read timeouts for periodic read timer
// Set longer write timeouts for user transmits
status = HidUart_SetTimeouts(*fd, READ_TIMEOUT, WRITE_TIMEOUT);
printf("HidUart_SetTimeouts status=%02X Read=%d Write=%d\n", status, READ_TIMEOUT, WRITE_TIMEOUT);
}

// Return TRUE if the device was opened successfully
return (status == HID_UART_SUCCESS);
}

void Close(HID_UART_DEVICE* fd)
{
/* flush output data before close and restore old attribute */
HID_UART_STATUS status = HidUart_Close(*fd);
}

// 发送
if (HidUart_Write(*fd, btData, 1, &length) != HID_UART_SUCCESS)
{
return FALSE;
}

//接收
DWORD length = 0;
HID_UART_STATUS status=HidUart_Read(*fd, btData, 1, &length);
if ((status == HID_UART_SUCCESS || status == HID_UART_READ_TIMED_OUT) && length>0)
{
//接收到数据
}

微信小程序扫码登陆

文章评论

2461人参与,0条评论