Well this is all started when I wanted to send data from a joystick to my laptop to control a robot. What seemed to be obvious for me is that I have to use one of the gaming library to interface my joystick.
after a lot of researches I found 2 decent library the XNA framework and the SlimDx library.
I didn't know why but I didn't like these frameworks.
So I thought .. the joystick is an HID device hence I can interface it just like any other HID device
I started with searching HID library fot c#. I found libusbdotnet seemed intresting but lack of documentation. then I found the USBHIDLIBRARY and I work on it.
This library is just amazing not so complex, simple documentation and does the job properly.
Here is my C# code for interfacing the joystick:
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 USBHIDDRIVER.USB;
using USBHIDDRIVER.TESTS;
using USBHIDDRIVER.List;
using USBHIDDRIVER;
using System.Threading;
namespace USBWork
{
public partial class Form1 : Form
{
USBHIDDRIVER.USBInterface usb = new USBInterface("vid_0079", "pid_0006");
byte[] currentread=new byte[1024];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
USBHIDDRIVER.USBInterface usb = new USBInterface("vid_0079", "pid_0006");
if (usb.Connect()) lblcnx.BackColor = Color.Green;
}
private void usbEventHandler(object sender, EventArgs args)
{
lblreading.BackColor = Color.Green;
ListWithEvent list = (ListWithEvent)sender;
byte[] byte_array = (byte[])list[list.Count-1];
//txtusb.Text = "start!\n";
for (int i = 0; i < byte_array.Length; i++)
{
currentread[i] = byte_array[i];
}
}
private void btnread_Click(object sender, EventArgs e)
{
usb.enableUsbBufferEvent(new EventHandler(usbEventHandler));
usb.startRead();
timer1.Enabled = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
usb.stopRead();
}
private void timer1_Tick(object sender, EventArgs e)
{
txtusb.Text+= currentread[6].ToString();
if (currentread[6] == 15) chknone.Checked = true; else chknone.Checked = false;
if (currentread[6] == 143) chkcercle.Checked = true; else chkcercle.Checked = false;
if (currentread[6] == 31) chktriangle.Checked = true; else chktriangle.Checked = false;
if (currentread[6] == 47) chkcarreau.Checked = true; else chkcarreau.Checked = false;
if (currentread[6] == 79) chkX.Checked = true; else chkX.Checked = false;
}
}
}
Aucun commentaire:
Enregistrer un commentaire