永发信息网

C语言如何让调用笔记本的USB接口啊,求实例

答案:4  悬赏:0  手机版
解决时间 2021-01-04 13:47
  • 提问者网友:疯孩纸
  • 2021-01-03 20:38
C语言如何让调用笔记本的USB接口啊,求实例
最佳答案
  • 五星知识达人网友:千夜
  • 2021-01-10 01:55
1.打开usb接口上的设备,或者打开usb控制器,涉及到windows的驱动访问。一般访问设备使用CreateFile打开设备,然后使用ReadFile/WriteFile读写设备。

2.例程:


    handle hFile = CreateFile(..., FILE_FLAG_overlapped, ...); //指定以异步方式打开
    byte bBuffer[100];
    overlapped o = { 0 };
    o.Offset = 345;
    bool bReadDone = ReadFile(hFile, bBuffer, 100, null, &o); // bReadDone 指定I/O请求是不是以同步方式打开
    dword dwError = GetLastError();
    if (!bReadDone && (dwError == ERROR_IO_PENDING)) { //异步方式打开
        // The I/O is being performed asynchronously; wait for it to complete
        WaitForSingleObject(hFile, infinite);
        bReadDone = TRUE;
    }
    if (bReadDone) {
           // o.Internal contains the I/O error
           // o.InternalHigh contains the number of bytes transferred
           // bBuffer contains the read data
    } else {
            // An error occurred; see dwError
    }
全部回答
  • 1楼网友:玩世
  • 2021-01-10 03:21
没有调用USB接口这种说法吧 可以打开USB接口上的设备,或者打开USB控制器,那涉及到windows的驱动访问。 一般访问设备使用CreateFile打开设备,然后使用ReadFile/WriteFile读写设备。 比如: HANDLE hFile = CreateFile(..., FILE_FLAG_OVERLAPPED, ...); //指定以异步方式打开 BYTE bBuffer[100]; OVERLAPPED o = { 0 }; o.Offset = 345; BOOL bReadDone = ReadFile(hFile, bBuffer, 100, NULL, &o); // bReadDone 指定I/O请求是不是以同步方式打开 DWORD dwError = GetLastError(); if (!bReadDone && (dwError == ERROR_IO_PENDING)) { //异步方式打开 // The I/O is being performed asynchronously; wait for it to complete WaitForSingleObject(hFile, INFINITE); bReadDone = TRUE; } if (bReadDone) { // o.Internal contains the I/O error // o.InternalHigh contains the number of bytes transferred // bBuffer contains the read data } else { // An error occurred; see dwError }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯