0% found this document useful (0 votes)
2 views4 pages

Software

This document is a C# code for a Windows Forms application that interfaces with a USB HID device. It includes functionality for manual and automatic modes, allowing users to send commands to the device based on button clicks and time intervals. The application also updates the UI based on the connection status of the USB device and received data.

Uploaded by

hịhd Hdhc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Software

This document is a C# code for a Windows Forms application that interfaces with a USB HID device. It includes functionality for manual and automatic modes, allowing users to send commands to the device based on button clicks and time intervals. The application also updates the UI based on the connection status of the USB device and received data.

Uploaded by

hịhd Hdhc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Drawing;
using System.Windows.Forms;
using UsbLibrary;

namespace baiUSB
{
public partial class Form1 : Form
{
private bool isManualMode = false;
private bool isMode3Active = false;
private int lastHour = -1;

public Form1()
{
InitializeComponent();

usbHidPort1.VendorId = 0x04D8;
usbHidPort1.ProductId = 0x0001;

usbHidPort1.OnSpecifiedDeviceArrived +=
usbHidPort1_OnSpecifiedDeviceArrived;
usbHidPort1.OnSpecifiedDeviceRemoved +=
usbHidPort1_OnSpecifiedDeviceRemoved;
usbHidPort1.OnDataRecieved += usbHidPort1_OnDataRecieved;

button1.Click += button1_Click;
button2.Click += button2_Click;
button3.Click += button3_Click;

button6.Click += button6_Click;
button7.Click += button7_Click;

timer1.Interval = 1000;
timer1.Tick += timer1_Tick;
timer1.Start();
}

protected override void OnHandleCreated(EventArgs e)


{
base.OnHandleCreated(e);
usbHidPort1.RegisterHandle(Handle);
}

protected override void WndProc(ref Message m)


{
usbHidPort1.ParseMessages(ref m);
base.WndProc(ref m);
}

private void button4_Click(object sender, EventArgs e)


{
usbHidPort1.CheckDevicePresent();
}

private void button5_Click(object sender, EventArgs e)


{
textBox1.Text = "Disconnected";
textBox1.BackColor = Color.Red;
isManualMode = false;
isMode3Active = false;
textBox5.Text = "Chế độ GUI";
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
}

private void button1_Click(object sender, EventArgs e)


{
if (isManualMode) return;
SendToDevice("A");
isMode3Active = false;
}

private void button2_Click(object sender, EventArgs e)


{
if (isManualMode) return;
SendToDevice("B");
isMode3Active = false;
}

private void button3_Click(object sender, EventArgs e)


{
if (isManualMode) return;
isMode3Active = true;
lastHour = -1;
SendMode3CommandByTime();
}

private void button6_Click(object sender, EventArgs e)


{
isManualMode = true;
isMode3Active = false;
textBox5.Text = "Chế độ Thủ công";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
SendToDevice("M");
}

private void button7_Click(object sender, EventArgs e)


{
isManualMode = false;
isMode3Active = false;
textBox5.Text = "Chế độ Giao Diện";
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
SendToDevice("G");
}

private void timer1_Tick(object sender, EventArgs e)


{
textBox2.Text = DateTime.Now.ToString("HH:mm:ss");

if (isMode3Active && !isManualMode)


{
int hour = DateTime.Now.Hour;
if (hour != lastHour)
{
lastHour = hour;
SendMode3CommandByTime();
}
}
}

private void SendMode3CommandByTime()


{
var cmd = (DateTime.Now.Hour >= 5 && DateTime.Now.Hour < 22) ? "C" :
"B";
SendToDevice(cmd);
}

private void SendToDevice(string cmd)


{
if (usbHidPort1.SpecifiedDevice == null)
{
MessageBox.Show("Thiết bị chưa kết nối!", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

byte[] data = new byte[65];


data[0] = 0x00;
data[1] = (byte)cmd[0];
try
{
usbHidPort1.SpecifiedDevice.SendData(data);
}
catch (Exception ex)
{
MessageBox.Show($"Gửi dữ liệu lỗi: {ex.Message}", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void usbHidPort1_OnDataRecieved(object sender,


DataRecievedEventArgs args)
{
BeginInvoke((Action)(() =>
{
char c = (char)args.data[1];
if (c == 'R') pictureBox2.Image = Properties.Resources.dendo;
if (c == 'Y') pictureBox2.Image = Properties.Resources.denvang;
if (c == 'G') pictureBox2.Image = Properties.Resources.denxanh;
}));
}

private void usbHidPort1_OnSpecifiedDeviceArrived(object sender, EventArgs


e)
{
textBox1.Text = "Connected";
textBox1.BackColor = Color.Lime;

if (!isManualMode)
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
}
}

private void usbHidPort1_OnSpecifiedDeviceRemoved(object sender, EventArgs


e)
{
textBox1.Text = "Disconnected";
textBox1.BackColor = Color.Red;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
}

private void usbHidPort1_OnDeviceArrived(object sender, EventArgs e) { }


private void usbHidPort1_OnDeviceRemoved(object sender, EventArgs e) { }
private void usbHidPort1_OnDataSend(object sender, EventArgs e) { }

private void textBox5_TextChanged(object sender, EventArgs e) { }


private void textBox7_TextChanged(object sender, EventArgs e) { }
private void label1_Click(object sender, EventArgs e) { }
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void groupBox3_Enter(object sender, EventArgs e) { }
private void label5_Click(object sender, EventArgs e) { }
private void label6_Click(object sender, EventArgs e) { }
private void label7_Click(object sender, EventArgs e) { }
private void button3_Click_1(object sender, EventArgs e) { }
private void Form1_Load(object sender, EventArgs e) { }
private void label4_Click(object sender, EventArgs e) { }
private void button6_Click_1(object sender, EventArgs e) { }
}
}

You might also like