0% found this document useful (0 votes)
5 views7 pages

thịnh

The document is a C# Windows Forms application that interfaces with a USB HID device to control and monitor LED states. It includes functionality for reading temperature data and updating the UI based on the device's responses. The application uses threading to handle data reception and ensures thread-safe updates to the UI components.
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)
5 views7 pages

thịnh

The document is a C# Windows Forms application that interfaces with a USB HID device to control and monitor LED states. It includes functionality for reading temperature data and updating the UI based on the device's responses. The application uses threading to handle data reception and ensures thread-safe updates to the UI components.
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/ 7

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 HIDLib;
using System.Threading;

namespace usbdevice
{
public partial class Form1 : Form

{
HIDDevice dev = new HIDDevice();
long countChar = 0; // vẽ biểu đồ
string InputDataTime = String.Empty; // Khai báo string buff dùng cho hiển
thị dữ liệu sau này.
public Thread eh; // tạo ra 2 thread tên là eh
float temp, light;
delegate void SetTextCallback(string text); //Khai bao delegate
SetTextCallBack voi tham so string. để khi ông này chạy xông ông kia mới hiển thị
lên
static int i = 0;
Boolean startread = true;

byte[] data = new byte [9];


public Form1()
{
InitializeComponent();
timer1.Start();
try
{
dev.vid = 0x1234;
dev.pid = 0x0001;
dev.in_size = 64;
dev.out_size = 64;
dev.Initialize();
}
catch
{
MessageBox.Show("PID & VID Error");
}
}

private void label3_Click(object sender, EventArgs e)


{

private void getUSB_Click(object sender, EventArgs e)


{
//startread = true;
//Thread.Sleep(10);
//eh = new Thread(new ThreadStart(ReceiveTEMP)); // void tpo thread
chsy hhm mhm d2 11&u tz USB
// eh.Start(); // Chsy thread
}

private void timer1_Tick(object sender, EventArgs e)


{
getDateTime.Text = DateTime.Now.ToString("dd-MM-yyy HH:mm:ss.fff");
}
private void SetText(string text)// hàm hiển thị dữ liệu temp tránh lỗi
cross threading
{

if (this.getData.InvokeRequired)
{

SetTextCallback d = new SetTextCallback(SetText); // khởi tạo 1


delegate mới gọi đến SetText

this.Invoke(d, new object[] { text });

else
{
getData.Text += text;//hien thi list du lieu
//===============tu dong cuon du lieu textbox
getData.SelectionStart = getData.Text.Length;
getData.ScrollToCaret();
}
}
private void ReceiveTEMP()
{
dev.Initialize();
byte[] reData = new byte[64];
//string var1, var2;
reData = dev.Receive();
InputDataTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string stringReData = enc.GetString(reData);
SetText(stringReData);
SetText("\r\n");

string FixReData = stringReData.Substring(0, 9);


//Kiem tra trang thai LED
if (reData[1] == '1') btLED1.ForeColor = Color.Green;
else btLED1.ForeColor = Color.Red;

if (reData[2] == '1') btLED2.ForeColor = Color.Green;


else btLED2.ForeColor = Color.Red;

if (reData[3] == '1') btLED3.ForeColor = Color.Green;


else btLED3.ForeColor = Color.Red;

if (reData[4] == '1') btLED4.ForeColor = Color.Green;


else btLED4.ForeColor = Color.Red;

if (reData[5] == '1') btLED5.ForeColor = Color.Green;


else btLED5.ForeColor = Color.Red;

if (reData[6] == '1') btLED6.ForeColor = Color.Green;


else btLED6.ForeColor = Color.Red;

if (reData[7] == '1') btLED7.ForeColor = Color.Green;


else btLED7.ForeColor = Color.Red;

if (reData[8] == '1') btLED8.ForeColor = Color.Green;


else btLED8.ForeColor = Color.Red;
}
protected override void WndProc(ref Message m)
{
dev.DeviceChange(ref m);
base.WndProc(ref m);
if (!dev.AttachedState)
{
status.Text = "Disconnected!"; status.ForeColor = Color.Red;
}
else
{
status.Text = "Connected!"; status.ForeColor = Color.Green;
}

private void btLED1_Click(object sender, EventArgs e)


{
if (btLED1.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[1] = Convert.ToByte('1');
btLED1.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[1] = Convert.ToByte('0');
btLED1.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED2_Click(object sender, EventArgs e)


{
if (btLED2.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[2] = Convert.ToByte('1');
btLED2.ForeColor = Color.Green;
}
else
{
data[0] = Convert.ToByte('1');
data[2] = Convert.ToByte('0');
btLED2.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED3_Click(object sender, EventArgs e)


{
if (btLED3.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[3] = Convert.ToByte('1');
btLED3.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[3] = Convert.ToByte('0');
btLED3.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED4_Click(object sender, EventArgs e)


{
if (btLED4.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[4] = Convert.ToByte('1');
btLED4.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[4] = Convert.ToByte('0');
btLED4.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED5_Click(object sender, EventArgs e)


{
if (btLED5.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[5] = Convert.ToByte('1');
btLED5.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[5] = Convert.ToByte('0');
btLED5.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED6_Click(object sender, EventArgs e)


{
if (btLED6.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[6] = Convert.ToByte('1');
btLED6.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[6] = Convert.ToByte('0');
btLED6.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED7_Click(object sender, EventArgs e)


{
if (btLED7.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[7] = Convert.ToByte('1');
btLED7.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[7] = Convert.ToByte('0');
btLED7.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void btLED8_Click(object sender, EventArgs e)


{
if (btLED8.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
data[8] = Convert.ToByte('1');
btLED8.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1');
data[8] = Convert.ToByte('0');
btLED8.ForeColor = Color.Red;
}

try
{
if (!dev.Transmit(data))
{
return;
}
}
catch
{ }
ReceiveTEMP();
}

private void BTall_Click(object sender, EventArgs e)


{
if (BTall.ForeColor == Color.Red)
{
data[0] = Convert.ToByte('1');
for (int i = 1; i <= 8; i++)
{
data[i] = Convert.ToByte('1');
}
BTall.Text = "All ON";
BTall.ForeColor = Color.Green;

}
else
{
data[0] = Convert.ToByte('1'); ;
for (int i = 1; i <= 8; i++)
{
data[i] = Convert.ToByte('0'); ;
}
BTall.Text = "All OFF";
BTall.ForeColor = Color.Red;
}

if (!dev.Transmit(data))
{
MessageBox.Show("Thiet bi chua san sang");
return;
}
//kiem tra trang thai Bat/tat LED
ReceiveTEMP();
}

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy