我覺的他寫的很好的 妳可以看看
我用vs2005的串口控件:
寫:
private void button1_Click(object sender, EventArgs e)
{
if (!sp.IsOpen)
{
if (textBox1.Text != "")
{
this.sp.Open();
byte[] buf = Encoding.ASCII.GetBytes(textBox1.Text.ToString());
sp.Write(buf,0,buf.Length);
textBox1.Text = "";
toolStripStatusLabel1.Text = "發送成功!";
sp.Close();
}
}
}
讀:
byte[] by = new byte[1024];
private delegate void SetTexts();
public ClientForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!sp2.IsOpen)
{
sp2.Open();
}
}
private void sp2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
sp2.Read(by, 0, by.Length);
this.Invoke(new SetTexts(SetText));
}
public void SetText()
{
textBox1.Text=null;
textBox1.Text += Encoding.ASCII.GetString(by);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//如果妳還看不懂 ,我可以將MSDN中的串行通信應用程序實例發給妳。vs2005版
//fzy-2004@126.com