如题 请教大家如何实现?有没有相关资料
解决方案 »
- inet的“仍在执行上一请求”的问题
- Visual Studio下编VB的编译问题
- vb6的list控件能自动根据内容的大小,自动的下拉吗
- 关于数值的累加问题
- sfilename = Appbbb_.Path & "\" & CStr(sfilename) & ".xls"生成exe竟然没报错,只是在运行exe后才说运行时错误424 需要对象
- 在VBA中,如何读出查询中的所有记录
- 大家好,有来求救了。我在VB时,在运行父窗体时,会打开所有的子窗体,有没有一种方法,可以让子窗体不打开呢?
- 求,一个连续打印的VBA
- 帮助高手看过来
- SQL语句有没有包含关系符?
- 列表框的设计问题 急求各位高手支招!!!
- editgrid的问题,希望控件的作者或者会的朋友帮帮忙
说白了要和底层硬件打交道。
给你推荐winio动态链接库,直接读写USB的端口。我试过并口的,USB的没有试过,不知行否。
我调用creatfile
readfile
writefile
等函数可以实现同步通信对设备的访问,但是异步通信就不能接收到下位机传送的数据
有没有人能提供一个关于异步通信的解决方案啊
Function FindTheHid() As Boolean
'Makes a series of API calls to locate the desired HID-class device.
'Returns True if the device is detected, False if not detected.Dim Count As Integer
Dim GUIDString As String
Dim HidGuid As GUID
Dim MemberIndex As LongLastDevice = False
MyDeviceDetected = False
Result = HidD_GetHidGuid(HidGuid)
Call DisplayResultOfAPICall("GetHidGuid")'Display the GUID.
GUIDString = _
Hex$(HidGuid.Data1) & "-" & _
Hex$(HidGuid.Data2) & "-" & _
Hex$(HidGuid.Data3) & "-"For Count = 0 To 7
'Ensure that each of the 8 bytes in the GUID displays two characters.
If HidGuid.Data4(Count) >= &H10 Then
GUIDString = GUIDString & Hex$(HidGuid.Data4(Count)) & " "
Else
GUIDString = GUIDString & "0" & Hex$(HidGuid.Data4(Count)) & " "
End If
Next Count
DeviceInfoSet = SetupDiGetClassDevs _
(HidGuid, _
vbNullString, _
0, _
(DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE))
Call DisplayResultOfAPICall("SetupDiClassDevs")
DataString = GetDataString(DeviceInfoSet, 32)'Begin with 0 and increment until no more devices are detected.
MemberIndex = 0Do
'The cbSize element of the MyDeviceInterfaceData structure must be set to
'the structure's size in bytes. The size is 28 bytes.
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
Result = SetupDiEnumDeviceInterfaces _
(DeviceInfoSet, _
0, _
HidGuid, _
MemberIndex, _
MyDeviceInterfaceData)
Call DisplayResultOfAPICall("SetupDiEnumDeviceInterfaces")
If Result = 0 Then LastDevice = True
'If a device exists, display the information returned.
If Result <> 0 Then
MyDeviceInfoData.cbSize = Len(MyDeviceInfoData)
Result = SetupDiGetDeviceInterfaceDetail _
(DeviceInfoSet, _
MyDeviceInterfaceData, _
0, _
0, _
Needed, _
0)
DetailData = Needed
MyDeviceInterfaceDetailData.cbSize = _
Len(MyDeviceInterfaceDetailData)
ReDim DetailDataBuffer(Needed)
Call RtlMoveMemory _
(DetailDataBuffer(0), _
MyDeviceInterfaceDetailData, _
4)
Result = SetupDiGetDeviceInterfaceDetail _
(DeviceInfoSet, _
MyDeviceInterfaceData, _
VarPtr(DetailDataBuffer(0)), _
DetailData, _
Needed, _
0)
Call DisplayResultOfAPICall(" Result of second call: ") DevicePathName = CStr(DetailDataBuffer())
'Convert to Unicode.
DevicePathName = StrConv(DevicePathName, vbUnicode)
'Strip cbSize (4 bytes) from the beginning.
DevicePathName = Right$(DevicePathName, Len(DevicePathName) - 4)
HidDevice = CreateFile _
(DevicePathName, _
GENERIC_READ Or GENERIC_WRITE, _
(FILE_SHARE_READ Or FILE_SHARE_WRITE), _
0, _
OPEN_EXISTING, _
0, _
0)
If HidDevice = -1 Then '判断CreateFile是否成功,一直检测
GoTo ERR_DEVICE
End If' hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
'
' CloseHandle hThread
Call DisplayResultOfAPICall("CreateFile")
'Set the Size property to the number of bytes in the structure.
DeviceAttributes.Size = LenB(DeviceAttributes)
Result = HidD_GetAttributes _
(HidDevice, _
DeviceAttributes)
Call DisplayResultOfAPICall("HidD_GetAttributes")
If Result <> 0 Then Else MsgBox "USB接口连接错误"
End If
If (DeviceAttributes.VendorID = MyVendorID) And _
(DeviceAttributes.ProductID = MyProductID) Then
MyDeviceDetected = True
Else
MyDeviceDetected = False
'If it's not the one we want, close its handle.
Result = CloseHandle _
(HidDevice)
DisplayResultOfAPICall ("CloseHandle")
End If
End If
'Keep looking until we find the device or there are no more left to examine.
ERR_DEVICE:
MemberIndex = MemberIndex + 1Loop Until (LastDevice = True) Or (MyDeviceDetected = True)If MyDeviceDetected = True Then
FindTheHid = True
Else Exit Function
End If
End FunctionPrivate Sub WriteReport() '发送指令开始下位机采集,可以写入
'Send data to the device.
Dim Count As Integer
Dim NumberOfBytesRead As Long
Dim NumberOfBytesToSend As Long
Dim NumberOfBytesWritten As Long
Dim ReadBuffer() As Byte
Dim SendBuffer() As Byte'The SendBuffer array begins at 0, so subtract 1 from the number of bytes.
ReDim SendBuffer(1)'******************************************************************************
'WriteFile
'Sends a report to the device.
'Returns: success or failure.
'Requires: the handle returned by CreateFile and
'The output report byte length returned by HidP_GetCaps
'******************************************************************************'The first byte is the Report ID
SendBuffer(0) = 1 '报告ID'The next bytes are data SendBuffer(1) = 1
NumberOfBytesWritten = 0Result = WriteFile _
(HidDevice, _
SendBuffer(0), _
CLng(Capabilities.OutputReportByteLength), _
NumberOfBytesWritten, _
0)
End Sub
Private Sub ReadReport() '读取下位机送上来的数据,无法正确读取'Read data from the device.Dim Count
Dim NumberOfBytesRead As Long
'Allocate a buffer for the report.
'Byte 0 is the report ID.
Dim ReadBuffer() As Byte
Dim UBoundReadBuffer As Integer'******************************************************************************
'ReadFile
'Returns: the report in ReadBuffer.
'Requires: a device handle returned by CreateFile,
'the Input report length in bytes returned by HidP_GetCaps.
'******************************************************************************'ReadFile is a blocking call. The application will hang until the device
'sends the requested amount of data. To prevent hanging, be sure that
'the device always has data to send.Dim ByteValue As String
'The ReadBuffer array begins at 0, so subtract 1 from the number of bytes.
ReDim ReadBuffer(Capabilities.InputReportByteLength - 1)
'Pass the address of the first byte of the read buffer.
Result = ReadFile _
(HidDevice, _
ReadBuffer(0), _
CLng(Capabilities.InputReportByteLength), _
NumberOfBytesRead, _
0)
Call DisplayResultOfAPICall("ReadFile")txtBytesReceived.Text = ""
For Count = 1 To UBound(ReadBuffer)
'Add a leading 0 to values 0 - Fh.
If Len(Hex$(ReadBuffer(Count))) < 2 Then
ByteValue = "0" & Hex$(ReadBuffer(Count))
Else
ByteValue = Hex$(ReadBuffer(Count))
End If
Text1.Text = ByteValue
'Display the received bytes in the text box.
Next Count
End Sub
异步通信应该就是串口通信吧?如果我没有理解错误的话,这个也许对你有用:
http://download.csdn.net/source/1339593