namespace My_NEWWW_HID_CS{
public partial class Form1 : Form{
USBHIDDRIVER.USBInterface usb = new USBInterface("vid_1234", "pid_1234");
public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
}
private void button1_Click(object sender, EventArgs e){
try {
usb.Connect();
if (usb.Connect() == true)
MessageBox.Show("Connected !!!");
else
MessageBox.Show("Not Connected !!!");
}catch(Exception er){
MessageBox.Show("BAAA");
}
}
private delegate void setTextDelegate(byte []array);
// set and update a text box using received data from microcontroller
private void setText(byte []array){
if (this.Outbox.InvokeRequired){
this.Outbox.Invoke(new setTextDelegate(this.setText), array);
}else{
this.Outbox.Text = this.Outbox.Text + "\r\n" + array[1].ToString();
this.Outbox.SelectionStart = Outbox.Text.Length;
this.Outbox.ScrollToCaret();
this.Outbox.Refresh();
}
}
//write command to activate receiving data
private void button3_Click(object sender, EventArgs e){
byte[] DataWrite = new byte[64];
if (usb.Connect() == true){
DataWrite[0] = byte.Parse(textBox1.Text);
usb.write(DataWrite);
usb.enableUsbBufferEvent(new System.EventHandler(myEventCatcher));
Thread.Sleep(5);
usb.startRead();
MessageBox.Show("Data Reading!");
//Thread.Sleep(2000);
usb.stopRead();
DataWrite[0] = 20;
usb.write(DataWrite);
}
}
//this is the event catcher !!!
public void myEventCatcher(object sender,System.EventArgs e){
//MessageBox.Show("Event Caught");
if (USBHIDDRIVER.USBInterface.usbBuffer.Count > 0){
byte[] currentRecord = new byte[64];
int counter = 0;
while ((byte[])USBHIDDRIVER.USBInterface.usbBuffer[counter] == null){
lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot){
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}
}
currentRecord = (byte[])USBHIDDRIVER.USBInterface.usbBuffer[0];
lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot){
USBHIDDRIVER.USBInterface.usbBuffer.RemoveAt(0);
}
setText(currentRecord); //update text box with received data
}
}
}//end class
}//end namespace