0% found this document useful (0 votes)
53 views

Codegiaodien

This document contains code for a C# program that communicates with a USB device. It defines classes and methods for initializing a USB HID port, sending and receiving data, and handling device connection/disconnection events. When data is received, it will update the color of an LED graphic based on the data value. Buttons allow sending commands to turn the LED on or off.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Codegiaodien

This document contains code for a C# program that communicates with a USB device. It defines classes and methods for initializing a USB HID port, sending and receiving data, and handling device connection/disconnection events. When data is received, it will update the color of an LED graphic based on the data value. Buttons allow sending commands to turn the LED on or off.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UsbLibrary;

namespace USB_PIC
{
public partial class Form1 : Form
{
byte[] readbuff = new byte[2];
byte[] writebuff = new byte[2];
int count = 0;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
this.usbHidPort.VendorId = 0x04D8;
this.usbHidPort.ProductId = 0x0001;
this.usbHidPort.CheckDevicePresent();

if (this.usbHidPort.SpecifiedDevice != null)
{
this.usbHidPort.SpecifiedDevice.SendData(writebuff);
}
textBox_VID.Text = usbHidPort.VendorId.ToString("x4");
textBox_PID.Text = usbHidPort.ProductId.ToString("x4");
}

private void button_Exit_Click(object sender, EventArgs e)


{
DialogResult answer = MessageBox.Show("Do you want to exit the
program?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == DialogResult.Yes)
{
this.Close();
}

private void button2_Click(object sender, EventArgs e)


{
writebuff[1] = (byte) '1';

if (this.usbHidPort.SpecifiedDevice != null)
this.usbHidPort.SpecifiedDevice.SendData(writebuff);
else
{
MessageBox.Show("Device not found . Please reconnect USB Device to
use.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

private void button1_Click(object sender, EventArgs e)


{
writebuff[1] = (byte) '0';
if (this.usbHidPort.SpecifiedDevice != null)
this.usbHidPort.SpecifiedDevice.SendData(writebuff);
else
{
MessageBox.Show(" Device not found . Please reconnect USB Device
USB Device to use.", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);

private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs


e)
{
toolStripStatusLabel_InforDevice.Text = "Device Detected";
textBox_Status.Text = "Connected!";
textBox_Status.BackColor = Color.Lime;
textBox_SW1.Text = "0";
ovalShape_LED.BackColor = Color.DarkGray;

private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e)


{

if (InvokeRequired)
{
Invoke(new EventHandler(usbHidPort1_OnDeviceRemoved), new object[]
{ sender, e });

}
else
{
toolStripStatusLabel_InforDevice.Text = "USB Removed";
}
}

private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e)


{
toolStripStatusLabel_InforDevice.Text = "USB Connected";
}

private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs


e)
{
if (InvokeRequired)
{
Invoke(new EventHandler(usbHidPort1_OnSpecifiedDeviceRemoved), new
object[] { sender, e });

}
else
{
toolStripStatusLabel_InforDevice.Text = "Device Disconnected";
textBox_Status.Text = "Disconnected!";
textBox_Status.BackColor = Color.Red;
}
}

private void usbHidPort1_OnDataRecieved(object sender,


DataRecievedEventArgs args)
{
if (InvokeRequired)
{
try
{
Invoke(new
DataRecievedEventHandler(usbHidPort1_OnDataRecieved), new object[] { sender,
args });

}
catch
{ }

}
else
{
readbuff = args.data;
toolStripStatusLabel_InforDevice.Text = "New Received Data";
if (readbuff[1] == '0')
{
ovalShape_LED.BackColor = Color.Black;
}
else if (readbuff[1] == '1')
{
ovalShape_LED.BackColor = Color.Red;
}
else if (readbuff[1] == 'b')
{
count++;
textBox_SW1.Text = count.ToString();
}
}
}

private void usbHidPort1_OnDataSend(object sender, EventArgs e)


{
toolStripStatusLabel_InforDevice.Text = "Data sent";
}

protected override void OnHandleCreated(EventArgs e)


{
base.OnHandleCreated(e);
usbHidPort.RegisterHandle(Handle);
}
protected override void WndProc(ref Message m)
{
usbHidPort.ParseMessages(ref m);
base.WndProc(ref m);
}

private void toolStripStatusLabel1_Click_1(object sender, EventArgs e)


{

private void label11_Click(object sender, EventArgs e)


{

private void toolStripStatusLabel_InforDevice_Click(object sender,


EventArgs e)
{

private void textBox_VID_TextChanged(object sender, EventArgs e)


{

private void groupBox_ledctrl_Enter(object sender, EventArgs e)


{

private void textBox_SW1_TextChanged(object sender, EventArgs e)


{

private void textBox_VN_TextChanged(object sender, EventArgs e)


{

}
}

You might also like