c# 串口接收数据问题
我是新手,文本框接收不到数据,下面是代码。调试发现事件都没有触发,请问怎么搞定?程序代码:
namespace 串口 { public partial class Form1 : Form { public string ReceiveString = string.Empty; public string SendString = string.Empty; private SerialPort port = new SerialPort(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { port.PortName = "COM1"; port.BaudRate = 9600; port.Parity = Parity.None; port.StopBits = StopBits.One; port.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); } private void btnSend_Click(object sender, EventArgs e) { try { port.Open(); SendString = txtSend.Text; port.WriteLine(SendString); txtReceive.Text = "数据发送成功!"; } catch (Exception ex) { txtReceive.Text = ex.Message; } finally { port.Close(); } } private void SerialPort_DataReceived(object sender,SerialDataReceivedEventArgs e) { ReceiveString = port.ReadLine(); } private void btnReceive_Click(object sender, EventArgs e) { txtReceive.Text = ReceiveString; } } }