You are on page 1of 4

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.Ports; //using ZedGraph; using System.IO; using System.Xml; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int so_xe; private string sendMsg; int intlen = 0; // intlen = tong so port com private void trang_thai() { if (so_xe < 99) { Lb_status_bai_xe.Text = "NORMAL"; Lb_status_bai_xe.ForeColor = System.Drawing.Color.Green; } else { if (so_xe >= 99) { Lb_status_bai_xe.Text = "STOPPED"; Lb_status_bai_xe.ForeColor = System.Drawing.Color.Red; Com.WriteLine("cy"); } } } private void timer1_Tick(object sender, EventArgs e) { // code cp nhp cc com kt ni //trang_thai(); string[] ports = SerialPort.GetPortNames(); if (intlen != ports.Length) { intlen = ports.Length; cbb_select_com.Items.Clear(); for (int j = 0; j < intlen; j++) { cbb_select_com.Items.Add(ports[j]); }

} } private void Oncom(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string s; //s = ""; s = Com.ReadByte().ToString(); Display(s); } private delegate void DlDisplay(string s); private void Display(string s) { if (lb_so_xe.InvokeRequired) { DlDisplay sd = new DlDisplay(Display); this.Invoke(sd, new object[] { s }); } else { this.lb_so_xe.Text = s; string temp; temp = s; so_xe = int.Parse(temp); } } private void bt_connect_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Disconnected") { Com.PortName = cbb_select_com.Text; Com.Open(); lb_status_com.Text = "Connected"; bt_connect.Text = "Disconnect"; } else { Com.Close(); lb_status_com.Text = "Disconnected"; lb_status_com.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; bt_connect.Text = "Connect"; } } private void bt_set_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { sendMsg = "b" + txt_so_xe.Text; Com.Write(sendMsg); } } private void bt_open_barrie1_Click(object sender, EventArgs e) {

if (lb_status_com.Text == "Connected") { Com.WriteLine("aq"); } } private void bt_close_barrie_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { Com.WriteLine("aw"); } } private void bt_open_barrie2_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { Com.WriteLine("ae"); } } private void bt_close_barrie2_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { Com.WriteLine("ar"); } } private void bt_start_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { timer2.Enabled = true; Lb_status_bai_xe.Text = "NORMAL"; Lb_status_bai_xe.ForeColor = System.Drawing.Color.Green; Com.WriteLine("ct"); } } private void bt_stop_Click(object sender, EventArgs e) { if (lb_status_com.Text == "Connected") { timer2.Enabled = false; Lb_status_bai_xe.Text = "STOPPED"; Lb_status_bai_xe.ForeColor = System.Drawing.Color.Red; Com.WriteLine("cy"); } } private void bt_exit_Click(object sender, EventArgs e) { Close(); }

private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("Hy Chn Mt Cng Com Kt Ni"); } private void Lb_status_bai_xe_Click(object sender, EventArgs e) { } private void timer2_Tick(object sender, EventArgs e) { trang_thai(); } private void label1_Click(object sender, EventArgs e) { } } }

You might also like