當前位置:編程學習大全網 - 編程軟體 - C# 串口讀寫

C# 串口讀寫

/question/70710093.html

我覺的他寫的很好的 妳可以看看

我用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

  • 上一篇:臨沂魯銀新能源有限公司怎麽樣?
  • 下一篇:什麽是Cimatron IT13數控編程
  • copyright 2024編程學習大全網