可以開啟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
可以開啟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
}