Code:
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
serialPort1.Open();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void ck_Led0_CheckedChanged(object sender, EventArgs e)
{
if (ck_Led0.Checked) serialPort1.Write("a");
else
serialPort1.Write("B");
}
private void ck_led1_CheckedChanged(object sender, EventArgs e)
{
if (ck_led1.Checked) serialPort1.Write("c");
else
serialPort1.Write("d");
}
private void button1_Click(object sender, EventArgs e)
{
byte a = (byte)Convert.ToInt16(txt_a.Text);
byte b = (byte)Convert.ToInt16(txt_b.Text);
serialPort1.Write(new byte[] {0x2b,a,b},0,3);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
lql_kq.Text = serialPort1.ReadByte().ToString(); // em làm theo bài hướng dẫn trên mạng nhưng nó báo sai ở đây
}
}
}
Comment