编写在USB接口读写数据的C程序

上传人:宝路 文档编号:18205298 上传时间:2017-11-14 格式:DOC 页数:10 大小:40.49KB
返回 下载 相关 举报
编写在USB接口读写数据的C程序_第1页
第1页 / 共10页
编写在USB接口读写数据的C程序_第2页
第2页 / 共10页
编写在USB接口读写数据的C程序_第3页
第3页 / 共10页
编写在USB接口读写数据的C程序_第4页
第4页 / 共10页
编写在USB接口读写数据的C程序_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《编写在USB接口读写数据的C程序》由会员分享,可在线阅读,更多相关《编写在USB接口读写数据的C程序(10页珍藏版)》请在金锄头文库上搜索。

1、使用一个 GUIDguidHID_1查找并打开一个 USB设备extern C int PASCAL SearchUSBDevice()HANDLE hUsb;int nCount, i, j;/标记同一设备个数HDEVINFO hDevInfoSet;BOOL bResult;PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail =NULL;memset(m_sysversion, 0, 20);GetSysVersion(m_sysversion);/ 检索相关 GUID的 USB设备总设备个数if (!GetUSBList()return 0;/ 取得一个该

2、GUID相关的设备信息集句柄hDevInfoSet = :SetupDiGetClassDevs(LPGUID)&guidHID_1,/GUID_CLASS_USB_DEVICE, / class GUID NULL, / 无关键字 NULL, / 不指定父窗口句柄 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); / 目前存在的设备/ 失败.if (hDevInfoSet = INVALID_HANDLE_VALUE)return NULL;/ 申请设备接口数据空间nCount = 0;bResult = TRUE;for (i=0; icbSize = siz

3、eof(SP_DEVICE_INTERFACE_DETAIL_DATA);bResult = SetupDiGetInterfaceDeviceDetail(hDevInfoSet, / 设备信息集句柄&ifdata, / 设备接口信息pDetail, / 设备接口细节(设备路径)predictedLength, / 输出缓冲区大小&requiredLength, / 不需计算输出缓冲区大小(直接用设定值)NULL); / 不需额外的设备描述if (bResult)/ 复制设备路径到输出缓冲区/:strcpy(pszDevicePathnCount, pDetail-DevicePath);i

4、f (strcmp(m_sysversion, winnt)=0)char ch18;for(i=0;iDevicePath+8+i);ch17=0;if (strcmp(ch,vid_0471&pid_0666)=0)/比较版本号,防止意外出错memset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ; memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ; READ_OS.hEvent = CreateEvent( NULL, / no securityTRUE, / explicit reset reqFALSE, /

5、initial event resetNULL ) ; / no nameif (READ_OS.hEvent = NULL) break;WRITE_OS.hEvent = CreateEvent( NULL, / no securityTRUE, / explicit reset reqFALSE, / initial event resetNULL ) ; / no nameif (NULL = WRITE_OS.hEvent)CloseHandle( READ_OS.hEvent );break;hUsb=CreateFile(pDetail-DevicePath,/&guidHID_

6、1,/GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_OVERLAPPED*/,NULL);if (hUsb != NULL)/ 比较定位找到的 USB在哪个 USB PORT上char id30;memset(id, 0, 30);i=0;doidi=*(pDetail-DevicePath+26+i);i+;while(idi-1!=#);idi-1 = 0;for (j=0; jDevicePath);m_USB

7、PositionMapnCount = j+1;break;CloseHandle(hUsb);nCount+;/ break;/ 比较驱动版本/ 比较操作系统版本elsememset( &READ_OS, 0, sizeof( OVERLAPPED ) ) ; memset( &WRITE_OS, 0, sizeof( OVERLAPPED ) ) ; READ_OS.hEvent = CreateEvent( NULL, / no securityTRUE, / explicit reset reqFALSE, / initial event resetNULL ) ; / no name

8、if (READ_OS.hEvent = NULL) break;WRITE_OS.hEvent = CreateEvent( NULL, / no securityTRUE, / explicit reset reqFALSE, / initial event resetNULL ) ; / no nameif (NULL = WRITE_OS.hEvent)CloseHandle( READ_OS.hEvent );break;hUsb=CreateFile(pDetail-DevicePath,/&guidHID_1,/GENERIC_READ|GENERIC_WRITE,FILE_SH

9、ARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_OVERLAPPED*/,NULL);if (hUsb != NULL)if(strcmp(pDetail-DevicePath, m_USBListj)=0)sprintf(m_DeviceDescj+1, %s, pDetail-DevicePath);m_USBPositionMapnCount = j+1;break;CloseHandle(hUsb);nCount+;/ break;/ 释放设备接口数据空间:GlobalFree

10、(pDetail);/ 关闭设备信息集句柄:SetupDiDestroyDeviceInfoList(hDevInfoSet);iDeviceCount = nCount;return nCount;/ 写BOOL Writestr(char *buf,int buflen, int index)BOOL fWriteStat;DWORD dwErrorFlags;DWORD dwError;COMSTAT ComStat;char szError 10 ;DWORD ret;int len, i, j, packet;div_t div_result;BYTE sendpacket65;BY

11、TE xorcode=0x00;if (m_gphdComindex = NULL) / no usb device(jk100c)return -1;div_result = div(buflen, 58);if (div_result.rem = 0)packet = div_result.quot;elsepacket = div_result.quot+1;for (i=0; i, dwError ) ;OutputDebugString(szError);ClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat ) ;if (dwE

12、rrorFlags 0)wsprintf( szError, nr , dwErrorFlags ) ;OutputDebugString(szError);else / some other error occurredClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat ) ;if (dwErrorFlags 0)wsprintf( szError, nr , dwErrorFlags ) ;OutputDebugString(szError);return FALSE;if (i != packet-1)/ should be re

13、ceive ackif (ReceivePacketAnswer(index) != 0)return FALSE;return TRUE;/ 读int Readstr(char *buf,int nMaxLength, int index)BOOL fReadStat ;COMSTAT ComStat;DWORD dwErrorFlags;DWORD dwLength;DWORD dwError;char szError 10 ;if (fCOMMOpened=0)return FALSE; /串口未打开/ only try to read number of bytes in queue

14、ClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat) ;/dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;dwLength=nMaxLength;if (dwLength 0)if (olap=TRUE) fReadStat = ReadFile(m_gphdComindex,buf, dwLength, &dwLength,&READ_OS) ;if (!fReadStat)if (GetLastError() = ERROR_IO_PENDING)OutputDebugS

15、tring(nrIO Pending);while(!GetOverlappedResult(m_gphdComindex, &READ_OS, &dwLength, TRUE )dwError = GetLastError();if(dwError = ERROR_IO_INCOMPLETE) continue;else/ an error occurred, try to recoverClearCommError(m_gphdComindex,&dwErrorFlags, &ComStat ) ;break;else / end-if (GetLastError() = ERROR_IO_PENDING)/ some other error occurreddwLength = 0 ;ClearCommError(m_gphdComindex, &dwErrorFlags, &ComStat ) ;if (dwErrorFlags 0)wsprintf( szError, nr , dwErrorFlags ) ;OutputDebugString(szError); / end-if (!fReadSta

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号