private void btnGo_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = "COM1";
serialPort1.Open();
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
serialPort1.Close();
MessageBox.Show("数据发送成功!", "系统提示");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message .ToString (),"提示信息");
}
}
//当有数据到达里触发
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.Open();
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
textBox2.Text = Encoding.Unicode.GetString(data);
serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}