C# và điều khiễn Uart

cuong_lx

Gà con
tình hình là mình đang làm cái Uart giao tiếp với máy tính. mình viết 1 chương trính C# để truyền nhận Uart. trong C# thì mình làm 3 thanh trackbar để truyền. mình muốn khi kéo trackbar 1 nấc thì máy tính truyền đi 1 chuỗi do mình qui định, ví dụ như mình kéo 1 nấc thì truyền đi số 1, kéo 2 nấc thì truyền đi số 2..... nhưng khi mình kéo thì nó truyền số 1 và số 2 luôn. nay mình post lên nhờ các bạn giúp đỡ. ai biết thì giúp mình với.
đây là project của mình: https://www.mediafire.com/?csgsbhbh4ic92pz

đây là code C#

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 System.IO;
using System.IO.Ports;
using System.Xml;
using System.Runtime.InteropServices;
namespace TEST_RS232
{
public partial class RS232 : Form
{
SerialPort P = new SerialPort();
string InputData = String.Empty;
delegate void SetTextCallback(string text);

public RS232()
{
InitializeComponent();
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
P.ReadTimeout = 1000;
P.DataReceived += new SerialDataReceivedEventHandler(DataReceive);
string[] BaudRate = { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
comboBox2.Items.AddRange(BaudRate);
string[] Databits = { "6", "7", "8" };
comboBox3.Items.AddRange(Databits);

}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (P.IsOpen)
{
P.Close();
}
P.PortName = comboBox1.SelectedItem.ToString();
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (P.IsOpen)
{
P.Close();
}
P.BaudRate = Convert.ToInt32(comboBox2.Text);
}

private void button6_Click(object sender, EventArgs e)
{
if (P.IsOpen)
{
if (textBox2.Text == "") MessageBox.Show("Chưa có dữ liệu!","Thông Báo");
else P.Write(textBox2.Text);
}
else MessageBox.Show("COM chưa mở.", "Thông báo",MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox2.Clear();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
P.Open();
button2.Enabled = true;
button1.Enabled = false;


}
catch (Exception ex)
{
MessageBox.Show("Không kết nối được.","Thử lại",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

private void button2_Click(object sender, EventArgs e)
{
P.Close();
button1.Enabled = true;
button2.Enabled = false;

}

private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
}

private void button4_Click(object sender, EventArgs e)
{
DialogResult kq = MessageBox.Show("Bạn thực sự muốn thoát","", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (kq == DialogResult.Yes)
{
MessageBox.Show("Cảm ơn bạn đã sử dụng chương trình");
this.Close();
}

}

private void RS232_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 3; // 9600
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}
private void DataReceive(object obj, SerialDataReceivedEventArgs e)
{
InputData = P.ReadExisting();
if (InputData != String.Empty)
{

SetText(InputData);
}
}

private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else this.textBox1.Text += text;
}

private void trackBar3_Scroll(object sender, EventArgs e)
{

if (P.IsOpen)

{
P.WriteLine("1");
P.WriteLine("2");
label4.Text = ("CHAY");
}

}

private void trackBar2_Scroll(object sender, EventArgs e)
{
if (P.IsOpen)
{
P.WriteLine("1");
P.WriteLine("2");
label5.Text = ("CHAY");
}

}

private void trackBar1_Scroll(object sender, EventArgs e)
{
if (P.IsOpen)
{
P.WriteLine("1");
P.WriteLine("2");
label6.Text = ("CHAY");
}

}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (P.IsOpen)
{
P.Close();
}
P.DataBits = Convert.ToInt32(comboBox3.Text);
}

}
}
cho mình thank trước!
 

hoangthien94

Ban Chủ Nhiệm
mình chưa hiểu ý định của bạn khi kéo trackbar?
Code:
private void trackBar3_Scroll(object sender, EventArgs e)
{
 
if (P.IsOpen)
 
{
P.WriteLine("1");
P.WriteLine("2");
label4.Text = ("CHAY");
}
 
}
 
Top