Hola a todos.....en este momento estoy experimentando un poco con USB en modo HID usando un PIC18F2550 , programando en CCS v4.104 y Visual C#2008 para la PC.
Ahora mi consulta es la siguiente...
yo tengo un programa de control básico usando VB.net y easyHID...
Y la librería de control :
Mi pregunta es como poder migrar este código que está en VB.net a VC#2008
No se mucho de programación de aplicaciones pero me gustaría saber como poder llamar a las funciones de mcHID.dl que es la librería de easyHID en VC# 2008.
Cualquier ayuda es bienvenida
Ahora mi consulta es la siguiente...
yo tengo un programa de control básico usando VB.net y easyHID...
Código:
' Acá está el código fuente del main...
Public Class Form1
Private Const VendorID As Short = 6017 ' Definimos el VendorID.
Private Const ProductID As Short = 2000 ' Definimos el ProductID.
' Declaramos los buffer's de entrada y salida de datos.
Private Const BufferInSize As Short = 8 ' Definimos el tamaño del buffer entrada.
Private Const BufferOutSize As Short = 8 ' Definimos el tamaño del buffer de salida.
Dim BufferIn(BufferInSize) As Byte
Dim BufferOut(BufferOutSize) As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConnectToHID(Me) ' Al ejecutarse el formulario conectamos el dispositivo al controlador.
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
DisconnectFromHID() ' Si cerramos el formulario desconectamos el dispositivo del controlador.
End Sub
' Si se conecta el dispositivo al host...
Public Sub OnPlugged(ByVal pHandle As Integer)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
Me.estado.BackColor = Color.Green
Me.estado_conexion.Text = "CONECTADO"
End If
End Sub
' Si se desconecta el dispositivo del host...
Public Sub OnUnplugged(ByVal pHandle As Integer)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
Me.estado.BackColor = Color.Red
Me.estado_conexion.Text = "DESCONECTADO"
End If
End Sub
Public Sub OnChanged() ' LLama a todas las funciones de mensajes.
Dim pHandle As Integer
pHandle = hidGetHandle(VendorID, ProductID)
hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
End Sub
' Si recibimos un dato...
Public Sub OnRead(ByVal pHandle As Integer)
If hidRead(pHandle, BufferIn(0)) Then
' Aqui se reciben los datos a partir del BufferIn(1) = dato del micro..
End If
End Sub
Public Sub WriteSomeData() ' Si hay un dato listo para enviar...
hidWriteEx(VendorID, ProductID, BufferOut(0))
End Sub
Private Sub boton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boton.Click
BufferOut(1) = 1
Call WriteSomeData()
End Sub
End Class
Código:
Imports System
Imports System.Threading
Imports System.Runtime.InteropServices ' Clase para importar la DLL.
Module HIDDLLInterface
'Declaramos todas las funciones.
Declare Function hidConnect Lib "mcHID.dll" Alias "Connect" (ByVal pHostWin As Integer) As Boolean
Declare Function hidDisconnect Lib "mcHID.dll" Alias "Disconnect" () As Boolean
Declare Function hidGetItem Lib "mcHID.dll" Alias "GetItem" (ByVal pIndex As Integer) As Integer
Declare Function hidGetItemCount Lib "mcHID.dll" Alias "GetItemCount" () As Integer
Declare Function hidRead Lib "mcHID.dll" Alias "Read" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
Declare Function hidWrite Lib "mcHID.dll" Alias "Write" (ByVal pHandle As Integer, ByRef pData As Byte) As Boolean
Declare Function hidReadEx Lib "mcHID.dll" Alias "ReadEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
Declare Function hidWriteEx Lib "mcHID.dll" Alias "WriteEx" (ByVal pVendorID As Integer, ByVal pProductID As Integer, ByRef pData As Byte) As Boolean
Declare Function hidGetHandle Lib "mcHID.dll" Alias "GetHandle" (ByVal pVendoID As Integer, ByVal pProductID As Integer) As Integer
Declare Function hidGetVendorID Lib "mcHID.dll" Alias "GetVendorID" (ByVal pHandle As Integer) As Integer
Declare Function hidGetProductID Lib "mcHID.dll" Alias "GetProductID" (ByVal pHandle As Integer) As Integer
Declare Function hidGetVersion Lib "mcHID.dll" Alias "GetVersion" (ByVal pHandle As Integer) As Integer
Declare Function hidGetVendorName Lib "mcHID.dll" Alias "GetVendorName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetProductName Lib "mcHID.dll" Alias "GetProductName" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetSerialNumber Lib "mcHID.dll" Alias "GetSerialNumber" (ByVal pHandle As Integer, ByVal pText As String, ByVal pLen As Integer) As Integer
Declare Function hidGetInputReportLength Lib "mcHID.dll" Alias "GetInputReportLength" (ByVal pHandle As Integer) As Integer
Declare Function hidGetOutputReportLength Lib "mcHID.dll" Alias "GetOutputReportLength" (ByVal pHandle As Integer) As Integer
Declare Sub hidSetReadNotify Lib "mcHID.dll" Alias "SetReadNotify" (ByVal pHandle As Integer, ByVal pValue As Boolean)
Declare Function hidIsReadNotifyEnabled Lib "mcHID.dll" Alias "IsReadNotifyEnabled" (ByVal pHandle As Integer) As Boolean
Declare Function hidIsAvailable Lib "mcHID.dll" Alias "IsAvailable" (ByVal pVendorID As Integer, ByVal pProductID As Integer) As Boolean
'Funciones para desplegar mensajes.
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Delegate Function SubClassProcDelegate(ByVal hwnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Declare Function DelegateSetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" _
(ByVal hwnd As Integer, ByVal attr As Integer, ByVal lval As SubClassProcDelegate) As Integer
' Constantes de la aplicación.
Public Const WM_APP As Integer = 32768
Public Const GWL_WNDPROC As Short = -4
' Constantes de mensajes de HID.
Private Const WM_HID_EVENT As Decimal = WM_APP + 200
Private Const NOTIFY_PLUGGED As Short = 1
Private Const NOTIFY_UNPLUGGED As Short = 2
Private Const NOTIFY_CHANGED As Short = 3
Private Const NOTIFY_READ As Short = 4
' Variables locales.
Private FPrevWinProc As Integer
Private FWinHandle As Integer
Private Ref_WinProc As New SubClassProcDelegate(AddressOf WinProc)
Private HostForm As Object
'Espera a recibir un mensaje del controlador HOST y luego se conecta a el mediante la librería de
'Funciones.
Public Function ConnectToHID(ByRef targetForm As Form) As Boolean
Dim pHostWin As Integer = targetForm.Handle.ToInt32
FWinHandle = pHostWin
pHostWin = hidConnect(FWinHandle)
FPrevWinProc = DelegateSetWindowLong(FWinHandle, GWL_WNDPROC, Ref_WinProc)
HostForm = targetForm
End Function
' Se desconecta del HOST.
Public Function DisconnectFromHID() As Boolean
DisconnectFromHID = hidDisconnect
SetWindowLong(FWinHandle, GWL_WNDPROC, FPrevWinProc)
End Function
Private Function WinProc(ByVal pHWnd As Integer, ByVal pMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
If pMsg = WM_HID_EVENT Then
Select Case wParam
' Mensaje de que un dispositivo se ha conectado al host.
Case Is = NOTIFY_PLUGGED
HostForm.OnPlugged(lParam)
' Mensaje de que un dispositivo se ha desconectado del host.
Case Is = NOTIFY_UNPLUGGED
HostForm.OnUnplugged(lParam)
' El controlador ha cambiado.
Case Is = NOTIFY_CHANGED
HostForm.OnChanged()
' Se he recibido un dato.
Case Is = NOTIFY_READ
HostForm.OnRead(lParam)
End Select
End If
WinProc = CallWindowProc(FPrevWinProc, pHWnd, pMsg, wParam, lParam)
End Function
End Module
Cualquier ayuda es bienvenida