المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : a problem about timer and serialport in VC# [modified]



C# Programming
05-15-2009, 05:55 AM
How can i use timer control and serialport control to dynamic ******* my number of 3 different show control......

i want to get the data form serialport....

namespace ShowNumber
{
public partial class Form1 : Form
{
//public int num = 0;
private bool serialflag = false;
public Form1()
{
InitializeComponent();
//this.label1.Text = num.ToString();
//serialPort1.Open();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (!serialflag)
{
serialflag = true;
serialPort1.ReceivedBytesThreshold = 1;
try
{
this.serialPort1.Open();/// always error here
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}

}



}

private void Form1_Load(object sender, EventArgs e)
{

}


private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte []bt = new byte[1];

serialPort1.Read(bt,0,bt.Length);
string str = bt.ToString();
switch (str)
{
case "0":
{
MessageBox.Show("bt == 0");
label1.Text += str;
}
break;
case "1":
{
MessageBox.Show("bt == 1");
label2.Text += str;
}
break;
case "2":
{
MessageBox.Show("bt == 2");
label3.Text += str;
}
break;
case "3":
{
MessageBox.Show("bt == 3");
label4.Text += str;
}
break;
case "4":
{
MessageBox.Show("bt == 4");
label5.Text += str;
}
break;
default:
break;


}


serialPort1.Close();
serialflag = false;
}

modified on Thursday, May 14, 2009 9:48 PM