Form1 Bai5KTTN - Cs
Form1 Bai5KTTN - Cs
Form1 Bai5KTTN - Cs
cs 1
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 LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using System.Windows.Forms.DataVisualization.Charting;
namespace HIF__
{
public partial class Form1 : Form
{
//Khai bao cac vung nho de gui du lieu xuong USB
#region variable
// DIN
byte DIValue;
// DOUT
//byte[] DODuty = new byte[8];
byte[] DOValue = new byte[8];
bool[] DOInPWMMode = new bool[8];
// PWM frequency
byte[] FValue = new byte[3];
//AIN gain
double AIGain;
// Sample time
Int16 TsValue;
// AOUT
byte[] AO0ByteValue = new byte[2];
byte[] AO1ByteValue = new byte[2];
// AIN
double[] AIValue = new double[3];
// Counter
Int32 counterValue;
byte resetCounterCmd;
//Variable for Button Q00 Q01
int ctnQ00 = 0;
int ctnQ01 = 0;
int counter;
#endregion variable
public Form1()
{
InitializeComponent();
}
}
}
//Ham ve Chart
private void PlotChart(double dataX, double dataY)
{
chart1.Series[0].Points.AddXY(dataX, dataY);
var chartArea = chart1.ChartAreas[0];
if (dataX > chartArea.AxisX.Maximum)
{
chartArea.AxisX.Maximum = dataX;
if (dataX / chartArea.AxisX.Interval == 10)
chartArea.AxisX.Interval = dataX / 5;
}
if (dataY > chartArea.AxisY.Maximum)
{
chartArea.AxisY.Maximum += 20;
}
}
private void btnconnection_Click(object sender, EventArgs e)
{
// Sinh vien tu them chuong trinh ket noi voi DAQ card
if (btnconnection.Text == "Connect")
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\ThiNghiem\Dethi\Bai5KTTN\C#\Bai5KTTN\HIF++\Form1.cs 3
{
if (myUsbDevice == null)
{
UsbRegDeviceList allDevices = UsbDevice.AllDevices;
foreach (UsbRegistry usbRegistry in allDevices)
{
if (usbRegistry.Open(out myUsbDevice))
{
txtproductname.Text = myUsbDevice.Info.ProductString;
txtvendorid.Text =
myUsbDevice.Info.Descriptor.VendorID.ToString();
txtproductid.Text =
myUsbDevice.Info.Descriptor.ProductID.ToString();
txtmanufacturer.Text =
myUsbDevice.Info.ManufacturerString;
USB_DATA_RECEIVER_INIT();
btnconnection.Text = "Disconnect";
timer1.Enabled = true;
}
}
}
if (myUsbDevice == null)
{
MessageBox.Show("Device Not Found !!!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
Usb_exit();
btnconnection.Text = "Connect";
txtproductname.Text = "";
txtvendorid.Text = "";
txtproductid.Text = "";
txtmanufacturer.Text = "";
txt_AI0.Text = "";
txt_pwm0.Text = "";
txt_pwm1.Text = "";
timer1.Enabled = false;
}
}
void USBWrite(byte[] buffer)
{
try
{
int bytesWritten;
if ((myUsbDevice.IsOpen) && (writer != null))
writer.Write(buffer, 1000, out bytesWritten);
}
catch (Exception err)
{
MessageBox.Show("Can Not Send Data To USB Device\nDetails: " + err,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#region USB_DATA_RECEIVER_INIT
void USB_DATA_RECEIVER_INIT()
{
IUsbDevice wholeUsbDevice = myUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
wholeUsbDevice.SetConfiguration(1);
wholeUsbDevice.ClaimInterface(0);
}
//Open usb end point reader and writer
reader = myUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
D:\STUDY-BKHCM\2022-2023-HK2\DoluongvaDieukhienMT\ThiNghiem\Dethi\Bai5KTTN\C#\Bai5KTTN\HIF++\Form1.cs 4
writer = myUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
}
myUsbDevice = null;
UsbDevice.Exit();
}
}
#endregion USB EXIT
/*int AI1IntValue;
int AI2IntValue;
byte[] AI1ByteValue = new byte[4];
byte[] AI2ByteValue = new byte[4];*/
/*AI1ByteValue[3] = input[9];
AI1ByteValue[2] = input[10];
AI1ByteValue[1] = input[11];
AI1ByteValue[0] = input[12];
AI2ByteValue[3] = input[13];
AI2ByteValue[2] = input[14];
AI2ByteValue[1] = input[15];
AI2ByteValue[0] = input[16];*/
/*AI1IntValue = BitConverter.ToInt32(AI1ByteValue, 0);
AI2IntValue = BitConverter.ToInt32(AI2ByteValue, 0);*/
/* DISPLAY I0.0*/
if (Convert.ToBoolean(DIValue & 0x01))
{
rectangleShape1.BackColor = Color.Red;
}
else
{
rectangleShape1.BackColor = Color.White;
}
/* DISPLAY I0.1*/
if (Convert.ToBoolean((DIValue >> 1) & 0x01))
{
rectangleShape2.BackColor = Color.Red;
}
else
{
rectangleShape2.BackColor = Color.White;
}
/* DISPLAY AI0*/
txt_AI0.Text = AIValue[0].ToString("0.000");
//txtAI1.Text = AIValue[1].ToString("0.000");
//txtAI2.Text = AIValue[2].ToString("0.000");
/* DISPLAY Counter*/
//txtCounter0.Text = counterValue.ToString();
}
#endregion USB DATA RECEIVER INTERRUPT SERVICE ROUNTIE
//Draw chart
PlotChart((double)timer1.Interval / 1000.0 * (double)counter, AIValue[0]);
counter++;
}
if (ctnQ00 == 1)
{
DOValue[0] = 0;
ctnQ00 = 0;
}
}
if (ctnQ01 == 1)
{
DOValue[1] = 0;
ctnQ00 = 0;
}
}