Szukałem jakiś bibliotek, żeby dostać się do czujki z poziomu .NET, ale jedyne rozwiązanie jakie znalazłem było komercyjne (i drogie). MSDN jest kompletnie do kitu w temacie dostępu do urządzeń HID.
Trafiłem jednak na kod dla VB6 (nawet lepiej IMHO niż .NET) i mam już połączenie z czujką. Oto fragmenty kodu:
Zapis do czujki:
' Send data to the device.
' Pass in a buffer with the data to send, and the number of bytes in the buffer
' Function returns number of characters actually written
' Data array index starts at 1, but element 0 must exist (holds the report ID)
Public Function WriteToHID(ByRef DataToSend() As Byte, Optional Datalength As Long = 64) As Long
Dim NumberOfBytesWritten As Long
Dim ByteValue As String
Dim result As Long
'The first byte is the Report ID
DataToSend(0) = 0
NumberOfBytesWritten = 0
result = WriteFile(HID_Write_Handle, DataToSend(0), Datalength + 1, NumberOfBytesWritten, 0)
WriteToHID = NumberOfBytesWritten - 1
End Function
Odczyt z czujki:
' Read data from the device and puts it into ReceiveBuffer()
' array index starts at 1, but array must include element 0 (holds the report ID)
' Function returns an error code:
' WAIT_OBJECT_0 (0) = read was successful
' WAIT_TIMEOUT (258) = no response within timeout limit
' other code = some other, undefined error
Public Function ReadFromHID(ByRef ReceiveBuffer() As Byte, Optional Datalength As Long = 64, Optional Timeout_in_msec As Integer = 5000) As Long
Dim NumberOfBytesRead As Long
Dim result As Long
result = ReadFile(HID_Read_Handle, ReceiveBuffer(0), Datalength + 1, NumberOfBytesRead, HIDOverlapped)
result = WaitForSingleObject(EventObject, Timeout_in_msec)
Call ResetEvent(EventObject)
If result <> WAIT_OBJECT_0 Then ' if there was a timeout or some other error
result = CancelIo(HID_Read_Handle)
CloseHandle (HID_Write_Handle)
CloseHandle (HID_Read_Handle)
MyDeviceDetected = False
End If
ReadFromHID = result
End Function
Wywołanie zapisu/odczytu:
Private Sub ReadAndWriteToDevice()
Dim i As Long
Dim OutputReportData(65) As Byte
Dim InputReportData(65) As Byte
lstResults.Clear
lstResults.AddItem "***** HID Test Report *****"
OutputReportData(1) = 10 + adder
OutputReportData(2) = 20 + adder
OutputReportData(3) = 30 + adder
OutputReportData(4) = 40 + adder
OutputReportData(5) = 50 + adder
OutputReportData(6) = 60 + adder
OutputReportData(7) = 70 + adder
OutputReportData(8) = 80 + adder
adder = (adder + 1) Mod 10
If MyDeviceDetected = False Then MyDeviceDetected = FindTheHid
If MyDeviceDetected = True Then
Call WriteToHID(OutputReportData)
Call ReadFromHID(InputReportData)
End If
lstResults.AddItem "Data Written:"
For i = 1 To 8
lstResults.AddItem OutputReportData(i)
Next i
lstResults.AddItem ""
lstResults.AddItem "Data Received:"
For i = 1 To 8
lstResults.AddItem InputReportData(i)
Next i
End Sub
Z kodu widzę, że dane są (za pomocą tych funkcji) wysyłane/odbierane w pakietach po 64 bajty, a my używamy 128 (ale to akurat łatwo zmienić). Pytanie teraz, jak powinien wyglądać ciąg danych, aby np. zrobić odczyt z czujki?
A jednak dopadłem się z .NET :)
Czy taka odpowiedź z czujki ma sens?
14 E1 05 00 A7 00 00 00 00 67 00 20 00 47 00 52
00 53 00 61 00 64 00 69 00 6F 00 61 00 63 00 34
03 72 00 61 00 64 00 69 00 6F 00 61 00 63 00 74
00 69 00 76 00 65 00 61 00 74 00 68 00 6F 00 6D
00 65 00 2E 00 6F 00 72 00 67 00 20 00 47 00 52
00 53 00 62 00 64 00 65 00 76 00 2E 00 61 00 74
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00