资源简介
调用libusb库, 在VC++上编写的上位机程序, 实现用bulk传输方式, 向USB设备发送和接收数据.
代码片段和文件信息
#include
#include “lusb0_usb.h“
#include “mt_cmd.h“
//#pragma comment(lib“libusb.lib“)
#define WRITE_ERROR -1
#define READ_ERROR -2
#define CLOSE_ERROR -3
int g_MaxPacketSize = 64;
int g_EP_OUT = 0x0001; //端点地址
int g_EP_IN = 0x0081;
unsigned long g_Timeout = 1000;
HANDLE __stdcall mt_open_hid(unsigned long ulVIDunsigned long ulPID)
{
struct usb_device * m_dev = NULL;
struct usb_dev_handle *m_dev_handle;
char str[64];
memset(str0sizeof(str));
usb_init();
usb_find_busses();
usb_find_devices();
struct usb_bus *bus;
struct usb_device *dev;
for(bus = usb_get_busses(); bus; bus = bus->next)
{
for(dev = bus->devices; dev; dev = dev->next)
{
if(dev->descriptor.idVendor == ulVID && dev->descriptor.idProduct == ulPID)
m_dev=dev;
}
}
if(!m_dev)
{
//printf(“m_dev not found\n“);
return NULL;
}
m_dev_handle = usb_open(m_dev);
if(!m_dev_handle)
{
//printf(“Could not open m_dev\n“);
return NULL;
}
if(usb_set_configuration(m_dev_handle 1) < 0)
{
return NULL;
}
if(usb_claim_interface(m_dev_handle 0) < 0) //claim_interface 0 注册与操作系统通信的接口 0
{
return NULL;
}
//获取最大数据包大小
g_MaxPacketSize = m_dev->config->interface->altsetting->endpoint->wMaxPacketSize;
//获取端点地址
int ep = m_dev->config->interface->altsetting->endpoint->bEndpointAddress;
if (ep > 0x0f)
{
g_EP_IN = ep;
g_EP_OUT = ep - 0x80;
}
else
{
g_EP_OUT = ep;
g_EP_IN = ep + 0x80;
}
HANDLE mtdev = NULL;
mtdev = m_dev_handle;
return mtdev;
}
__int16 __stdcall mt_close_hid(HANDLE mtdev)
{
int st = 0;
if ((long)mtdev > 0)
{
usb_release_interface((usb_dev_handle *)mtdev 0); //注销接口,释放资源,和usb_claim_interface搭配使用。
st = usb_close((usb_dev_handle *)mtdev);
if(st = 0)
{
return st;
}
else
{
return st;
}
}
else
{
return CLOSE_ERROR;
}
}
__int16 __stdcall mt_send_cmd(HANDLE mtdev int slen unsigned char* sendcmd int* rlen unsigned char* receivedata int timeout)
{
__int16 st = 0;
if((long)mtdev < 0 || mtdev == NULL)
{
return -1;
}
unsigned char* receive_buffer = (unsigned char*)malloc(4096);
memset(receive_buffer 0 4096);
st = usb_bulk_write((usb_dev_handle *)mtdev g_EP_OUT (char*)sendcmd slen timeout);
if(st != slen)
{
free(receive_buffer);
receive_buffer = NULL;
return WRITE_ERROR;
}
st = usb_bulk_read((usb_dev_handle *)mtdev g_EP_IN (char*)receive_buffer *rlen timeout);
if(st <= 0)
{
free(receive_buffer);
receive_buffer = NULL;
return READ_ERROR;
}
*rlen = st;
memcpy(receivedata receive_buffer *rlen);
free(receive_buffer);
receive_buffer = NULL;
return 0;
}
void __stdcall mt_set_timeouts(unsigned long ulTotalTimeOuts)
{
g_Timeout = ulTotalTimeOuts;
return;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-03-24 23:35 mtusbdll_v1\
目录 0 2017-03-28 22:56 mtusbdll_v1\Bulid\
文件 2097245 2017-03-28 22:56 mtusbdll_v1\Bulid\Test.exe
文件 2484324 2017-03-28 22:56 mtusbdll_v1\Bulid\Test.ilk
目录 0 2017-03-28 22:56 mtusbdll_v1\mt_usblib\
目录 0 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\
文件 15235 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\mtusb.obj
文件 0 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\mtusb.sbr
文件 1934336 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\mt_usblib.bsc
文件 13092 2017-03-18 12:58 mtusbdll_v1\mt_usblib\Debug\mt_usblib.lib
文件 4502192 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\mt_usblib.pch
文件 132096 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\vc60.idb
文件 77824 2017-03-28 22:56 mtusbdll_v1\mt_usblib\Debug\vc60.pdb
文件 11974 2012-01-17 09:39 mtusbdll_v1\mt_usblib\libusb.lib
文件 11879 2017-03-15 13:53 mtusbdll_v1\mt_usblib\lusb0_usb.h
文件 2987 2017-03-28 22:39 mtusbdll_v1\mt_usblib\mtusb.cpp
文件 387 2017-03-28 22:39 mtusbdll_v1\mt_usblib\mt_cmd.h
文件 3110 2017-03-20 19:28 mtusbdll_v1\mt_usblib\mt_usblib.dsp
文件 734 2017-03-20 19:28 mtusbdll_v1\mt_usblib\mt_usblib.dsw
文件 1834 2017-03-20 17:44 mtusbdll_v1\mt_usblib\mt_usblib.lib
文件 214016 2017-03-28 22:56 mtusbdll_v1\mt_usblib\mt_usblib.ncb
文件 58880 2017-03-28 22:56 mtusbdll_v1\mt_usblib\mt_usblib.opt
文件 987 2017-03-28 22:56 mtusbdll_v1\mt_usblib\mt_usblib.plg
文件 415 2017-03-20 19:28 mtusbdll_v1\mt_usblib\mt_usblib.positions
目录 0 2017-03-20 18:38 mtusbdll_v1\mt_usblib\Release\
文件 1812 2017-03-20 18:38 mtusbdll_v1\mt_usblib\Release\mtusb.obj
文件 3596744 2017-03-20 18:38 mtusbdll_v1\mt_usblib\Release\mt_usblib.pch
文件 41984 2017-03-20 18:38 mtusbdll_v1\mt_usblib\Release\vc60.idb
文件 285 2017-03-24 23:50 mtusbdll_v1\ReadMe.txt
目录 0 2017-03-28 22:56 mtusbdll_v1\Test\
目录 0 2017-03-28 22:56 mtusbdll_v1\Test\Debug\
............此处省略44个文件信息
- 上一篇:ATM页面程序MFC对话框
- 下一篇:MFC下父窗口与子窗口的数据发送
评论
共有 条评论