串口问题
谁给我个接收串口数据的原代码啊
程序代码:
SerialPort spSend; //spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据 SerialPort spReceive; //spReceive接受数据 TextBox txtSend; //发送区 TextBox txtReceive; //接受区 Button btnSend; //数据发送按钮 delegate void HandleInterfaceUpdateDelegate(string text); //委托,此为重点 HandleInterfaceUpdateDelegate interfaceUpdateHandle; public void InitClient() //窗体控件已在初始化 { interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox); //实例化委托对象 spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明 spReceive.DataReceived += Ports.SerialDataReceivedEventHandler(spReceive_DataReceived); spReceive.ReceivedBytesThreshold = 1; spReceive.Open(); } public void btnSend_Click(object sender,EventArgs e) { spSend.WriteLine(txtSend.Text); } public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e) { byte[] readBuffer = new byte[spReceive.ReadBufferSize]; spReceive.Read(readBuffer, 0, readBuffer.Length); this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) }); } private void UpdateTextBox(string text) { txtReceive.Text = text; }