Codegiaodien

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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