/*The DLL has four functions:
void USBHIDClassInit (VID, PID, Size) - Sets the Vendor and Product Id used in the class.
Also specifies the Report Size. This must match
the size specified in the devices report descriptor.
bool USBHIDWriteReport (buffer, len) - Sends a buffer to the device. len specifies the
number of valid bytes in the buffer. The data is
padded out to the report size with 0xFF.
bool USBHIDReadReport (buffer) - Reads a packet from the device and returns it the
buffer. buffer[0] contains the first byte from the device.
bool USBHIDIsConnected() - Checks with the OS to see if the device is connected
(VID & PID specified by USBHIDClassInit). Returns
true if the device has is enumerated. This is an OS
inquiry only and does not generate any traffic on the bus.
ReadReport and WriteReport functions use non-blocking IO calls. That is the function
will return after one second even if the call fails. The functions return a boolean indicating
success or failure. This allows the application to gracefully recover when the device is
disconnected rather than hang. */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HIDClass;
namespace USBTest
{
class Program
{
/* Initialize the main program
* Welcome Message and USB Interface
*/
static void Main(string[] args)
{
uint VID = 0x1234; //vid&pid of the USB device
uint PID = 0x0001;
Console.Write("Welcome to USB Terminal! \n\n");
Console.Write("Initializing USB HID Connection....");
//Initialize HID connection!
MCHPHIDClass.USBHIDClassInit(VID, PID, 64);
//Wait for recognizing the USB device
//Function returns 1 when USB device found
while(!MCHPHIDClass.USBHIDIsConnected()) {
}
//Execute the rest of the Program
Console.Write("Connected!\n\n");
while (true)
{
MainMenu();
}
}
/*Main Part of the Program
*User selects a funcion and program jumps to it
*/
static void MainMenu()
{
Console.WriteLine("\n---------------------------------------------------------------------\n");
Console.WriteLine("Enter Option:");
Console.WriteLine("[1] Toggle LATB0");
Console.WriteLine("[2] Get LATB0 Status");
Console.WriteLine("[3] Send Random Data - Receive Data Reversed");
Console.WriteLine("[0] Clear Screen");
Console.Write("Option: ");
int CMD = Convert.ToInt16(Console.ReadLine());
Console.WriteLine();
switch (CMD)
{
case 0: Console.Clear();
break;
case 1: ToggleLATB0();
break;
case 2: GetLATB0Status();
break;
case 3: SendReceiveData();
break;
}
}
/*This functions sends a single byte to the USB
* device: 0x32 . PIC Firmware reads this byte and
* toggles PIN RB0. Although only one byte of data
* is used, all 64 bytes of the buffer have to be sent
*/
unsafe static void ToggleLATB0()
{
//Report size and Buffer size is 64 bytes
//So define a 64 byte wide array
byte* Write_Buf = stackalloc byte[64];
//Set first byte to the command to toggle RB0
Write_Buf[0] = 0x32;
//Send the Buffer to the USB Device
//Function returns 1 if no errors occured
//If error occured jump to CheckError() function
//Please pay attention to setting the correct
//Report size (64 bytes)
if (MCHPHIDClass.USBHIDWriteReport(Write_Buf, 64)) {
Console.Write("Succesfully Toggled LATB0.");
}
else
{ //If not succesfull sent
CheckError();
}
}
/* This function retrieves the status of RB0
* Again, it sends a single-byte command to the
* USB Device, which now will return a single byte
* of data to the Host. Again, the entire 64 byte
* buffer will be sent
*/
unsafe static void GetLATB0Status()
{
//Declare the proper 64 byte read and write buffers
byte* Write_Buf = stackalloc byte[64];
byte* Read_Buf = stackalloc byte[64];
//Set the proper command
Write_Buf[0] = 0x81;
//If the command is succesfully sent to USB Device
//Wait for data from PIC and process it
if (MCHPHIDClass.USBHIDWriteReport(Write_Buf, 64))
{
Console.Write("Sent inquiry, Waiting for Response... ");
//This function reads from USB and copies
//Data into the readbuffer
MCHPHIDClass.USBHIDReadReport(Read_Buf);
//Process Data. PIC Firmware returns data in the
//Second byte
if (Read_Buf[1] == 0x01) {
Console.Write("LATB0 Status: On");
} else if (Read_Buf[1] == 0x00) {
Console.Write("LATB0 Status: Off");
}
}
else
{ //If not succesfull sent
CheckError();
}
}
/* This functions sends 32 bytes of data to the
* USB device. The PIC Firmware will reverse the data
* and send it back to Host.
*/
unsafe static void SendReceiveData()
{
//Once again, create the read and write buffer
byte* Write_Buf = stackalloc byte[64];
byte* Read_Buf = stackalloc byte[64];
//32 byte array for 32 random values
byte[] Random_Buffer = new byte[32];
bool VERIFIED;
//Fill the 32byte array with random values
new Random().NextBytes(Random_Buffer);
//Set the proper Command
Write_Buf[0] = 0x53;
Console.Write("Generating 32-byte Wide Array...\n");
//Copy contents of the 32byte random array
//to the write buffer. Data in writebuffer is
//located at byte 2 to 33
for (int i = 0; i < 32; i++)
{
Write_Buf[i + 1] = Random_Buffer[i];
if (i % 8 == 0) {
Console.Write("\n");
}
Console.Write("{0} ", Random_Buffer[i]);
}
Console.Write("\n\n");
//Send the buffer to USB Device
if (MCHPHIDClass.USBHIDWriteReport(Write_Buf, 64))
{
Console.Write("Sent Random Array, Waiting for Response... ");
//If sending succesful read data from PIC into readbuffer
MCHPHIDClass.USBHIDReadReport(Read_Buf);
Console.Write("Received Data:\n");
//Display received data
for (int i = 0; i < 32; i++)
{
if (i % 8 == 0)
{
Console.Write("\n");
}
Console.Write("{0} ", Read_Buf[i+1]);
}
Console.Write("\n\n");
//Compare generated random data with received data
//Should match exactly (besides that received data
//is reversed ofcourse)
VERIFIED = true;
Console.Write("Verifying Data... ");
for (int i = 0; i < 32; i++) {
if (Read_Buf[32 - i] != Random_Buffer[i]) {
VERIFIED = false;
}
}
if (VERIFIED) {
Console.Write("Data Verified Succesfully!");
} else {
Console.Write("WARNING: Data Verification FAILED!");
}
}
else
{ //Something went wrong
CheckError();
}
}
unsafe static void CheckError()
{
if (!MCHPHIDClass.USBHIDIsConnected())
{
Console.WriteLine("Failed -> Device not Connected!");
}
else if (MCHPHIDClass.USBHIDIsConnected())
{
Console.WriteLine("Failed -> Unknown Error!");
}
}
}
}