Hola Sebaglio en esta paguina hay un ejemplo en c++ http://www.muchotrasto.com/PICCalculadoraUSBC++.php
Saludos
Saludos
Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature currently requires accessing the site using the built-in Safari browser.
#include <18F2450.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL2,CPUDIV1, VREGEN,MCLR
#use delay(clock=48000000)
#include "usb_cdc.h"
#include "usb_desc_cdc.h"
#define LED1 (pin_c0)
#define LED_ON output_high
#define LED_OFF output_low
void main (){
delay_ms (500);
usb_cdc_init();
usb_init();
while(true){
LED_ON (LED1);
delay_ms (500);
LED_OFF (LED1);
delay_ms (500);}
}
usb_task();
printf(usb_cdc_putc, "Conexion CDC via USB .\n\r");
if(usb_cdc_getc()=='a')
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace MecaniqueUK
{
class EasyHID
{
// HID specific...
public const UInt32 VENDOR_ID = 6017;
public const UInt32 PRODUCT_ID = 2000;
public const int BUFFER_IN_SIZE = 8;
public const int BUFFER_OUT_SIZE = 8;
// HID events...
private const int WM_APP = 0x8000;
public const int WM_HID_EVENT = WM_APP + 200;
public const int NOTIFY_PLUGGED = 0x0001;
public const int NOTIFY_UNPLUGGED = 0x0002;
public const int NOTIFY_CHANGED = 0x0003;
public const int NOTIFY_READ = 0x0004;
// HID interface...
[DllImport("mcHID.dll")]
public static extern bool Connect(IntPtr pHostWin);
[DllImport("mcHID.dll")]
public static extern bool Disconnect();
[DllImport("mcHID.dll")]
public static extern UInt32 GetItem(UInt32 pIndex);
[DllImport("mcHID.dll")]
public static extern UInt32 GetItemCount();
[DllImport("mcHID.dll")]
public static extern bool Read(UInt32 pHandle, IntPtr pData);
[DllImport("mcHID.dll")]
private static extern bool Write(UInt32 pHandle, IntPtr pData);
[DllImport("mcHID.dll")]
private static extern bool ReadEx(UInt32 pVendorId, UInt32 pProductId, IntPtr pData);
[DllImport("mcHID.dll")]
private static extern bool WriteEx(UInt32 pVendorId, UInt32 pProductId, IntPtr pData);
[DllImport("mcHID.dll")]
public static extern UInt32 GetHandle(UInt32 pVendorID, UInt32 pProductId);
[DllImport("mcHID.dll")]
public static extern UInt32 GetVendorID(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern UInt32 GetProductID(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern UInt32 GetVersionID(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern UInt32 GetInputReportLength(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern UInt32 GetOutputReportLength(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern void SetReadNotify(UInt32 pHandle, bool pValue);
[DllImport("mcHID.dll")]
public static extern bool IsReadNotifyEnabled(UInt32 pHandle);
[DllImport("mcHID.dll")]
public static extern bool IsAvailable(UInt32 pVendorId, UInt32 pProductId);
// Managed version of the read/write functions.
public static bool Read(UInt32 pHandle, out byte[] pData)
{
IntPtr unmanagedBuffer = Marshal.AllocHGlobal(BUFFER_IN_SIZE);
bool result = Read(pHandle, unmanagedBuffer);
try { pData = new byte[BUFFER_IN_SIZE]; Marshal.Copy(unmanagedBuffer, pData, 0, BUFFER_IN_SIZE); }
finally { Marshal.FreeHGlobal(unmanagedBuffer); }
return result;
}
public static bool Write(UInt32 pHandle, byte[] pData)
{
IntPtr unmanagedBuffer = Marshal.AllocHGlobal(BUFFER_OUT_SIZE);
bool result;
try { Marshal.Copy(pData, 0, unmanagedBuffer, BUFFER_OUT_SIZE); result = Write(pHandle, unmanagedBuffer); }
finally { Marshal.FreeHGlobal(unmanagedBuffer); }
return result;
}
public static bool ReadEx(UInt32 pVendorId, UInt32 pProductId, out byte[] pData)
{
IntPtr unmanagedBuffer = Marshal.AllocHGlobal(BUFFER_IN_SIZE);
bool result = ReadEx(pVendorId, pProductId, unmanagedBuffer);
try { pData = new byte[BUFFER_IN_SIZE]; Marshal.Copy(unmanagedBuffer, pData, 0, BUFFER_IN_SIZE); }
finally { Marshal.FreeHGlobal(unmanagedBuffer); }
return result;
}
public static bool WriteEx(UInt32 pVendorId, UInt32 pProductId, byte[] pData)
{
IntPtr unmanagedBuffer = Marshal.AllocHGlobal(BUFFER_OUT_SIZE);
bool result;
try { Marshal.Copy(pData, 0, unmanagedBuffer, BUFFER_OUT_SIZE); result = WriteEx(pVendorId, pProductId, unmanagedBuffer); }
finally { Marshal.FreeHGlobal(unmanagedBuffer); }
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using MecaniqueUK;
namespace WindowsFormsApplication1
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
EasyHID.Connect(Handle);
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
EasyHID.Disconnect();
}
private void OnPlugged(UInt32 handle)
{
if (EasyHID.GetVendorID(handle) == EasyHID.VENDOR_ID && EasyHID.GetProductID(handle) == EasyHID.PRODUCT_ID)
{
Console.WriteLine("Conectado al dispositivo.");
EasyHID.SetReadNotify(handle, true);
// Enviar dato de prueba
byte[] test = new byte[EasyHID.BUFFER_OUT_SIZE];
test[0] = 0; // Report ID
test[1] = 0x11; // Dato aleatorio
if (EasyHID.Write(handle, test) == true)
{
Console.WriteLine("Dato enviado correctamente.");
}
}
}
private void OnUnplugged(UInt32 handle)
{
if (EasyHID.GetVendorID(handle) == EasyHID.VENDOR_ID && EasyHID.GetProductID(handle) == EasyHID.PRODUCT_ID)
{
Console.WriteLine("Desconectado del dispositivo.");
}
}
protected override void WndProc(ref Message message)
{
// Intercept the HID message.
if (message.Msg == EasyHID.WM_HID_EVENT)
{
switch (message.WParam.ToInt32())
{
case EasyHID.NOTIFY_PLUGGED:
OnPlugged((UInt32) message.LParam.ToInt32());
break;
case EasyHID.NOTIFY_UNPLUGGED:
OnUnplugged((UInt32) message.LParam.ToInt32());
break;
case EasyHID.NOTIFY_CHANGED:
break;
case EasyHID.NOTIFY_READ:
break;
}
}
// Run the base procedure.
base.WndProc(ref message);
}
}
}