private SerialPort sPort = new SerialPort(); //串行端口资源 /// <summary> /// 函数功能:打开串口/关闭串口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { sPort.PortName = "com3";//串口的portname sPort.BaudRate = 9600;//串口的波特率 sPort.Open(); } private void btnSend_Click(object sender, EventArgs e) { //对于中文的话,要先对其进行编码,将其转换成 _Base64String ,否则你得不到中文字符串的 byte[] data = Encoding.Unicode.GetBytes(textBox2.Text); string str = Convert.ToBase64String(data); sPort.WriteLine(str); MessageBox.Show("数据发送成功", "系统提示"); } private void button1_Click_1(object sender, EventArgs e) { //解码然后将字符串赋值给 文本框的 文本区 byte[] data = Convert.FromBase64String(sPort.ReadLine()); textBox1.Text = Encoding.Unicode.GetString(data); }