日期:2014-05-20 浏览次数:20865 次
private delegate void d_SetText(string str); private void SetText(string str) { this.textBox1.Text += str; } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { int len = serialPort1.BytesToRead; byte[] buf = new byte[len]; serialPort1.Read(buf, 0, buf.Length); this.Invoke(new d_SetText(SetText), new object[] { System.Text.Encoding.Default.GetString(buf) }); } private void button1_Click(object sender, EventArgs e) { serialPort1.Write("这里写串口命令"); //注意有时候需要加Char(13)或者Char(10),具体看通讯协议 }
------解决方案--------------------
请参照一下吧,下载实例看看:http://www.shilidata.com/search.aspx?type=&searchid=11055
关于C#中关于串口的监听
http://www.shilidata.com/showtopic-280.aspx
串口通讯System.IO.Ports.Serialport
http://www.shilidata.com/showtopic-182.aspx