using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            //serialPort1 = new SerialPort("com1");//实例serialport1
            //serialPort1.BaudRate = 9600;//波特率
            //serialPort1.Parity = Parity.Odd;//无奇偶校验位
            //serialPort1.StopBits = StopBits.One;//两个停止位
            //serialPort1.Handshake = Handshake.RequestToSend;//控制协议
            //serialPort1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
            //serialPort1.ReadTimeout = 5000;//读取超时时间
            //serialPort1.WriteTimeout = 5000;//写入超时时间   
        }
              private void sopen()
        { 
                      //serialport打开端口是否异常
            try
            {
                serialPort1.Open();
            }
            catch { }
            if (serialPort1.IsOpen)
            {
                Console.WriteLine("the port is opened!");
            }
            else
            {
                Console.WriteLine("failure to open the port!");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
           // sopen();
            
            try
            {
                serialPort1.PortName = "COM1";
                serialPort1.BaudRate = 9600;
                serialPort1.WriteTimeout = 5000;//写入超时时间 
                serialPort1.ReadTimeout = 5000;//读取超时时间
                serialPort1.Open();
                byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
                string str = Convert.ToBase64String(data);
                serialPort1.WriteLine(str);
                MessageBox.Show(this, "利用串口成功发送数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
               MessageBox.Show(this, "利用串口发送数据失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
                textBox2.Text = Encoding.Unicode.GetString(data);
                serialPort1.Close();
                MessageBox.Show(this, "利用串口成功接收数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show(this, "利用串口接收数据失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
串口通信

解决方案 »

  1.   

                 
       serialPort1.Parity = Parity.None;//无奇偶校验位
                    serialPort1.StopBits = StopBits.Two;//两个停止位
                    serialPort1.Handshake = Handshake.RequestToSend;//控制协议
                    serialPort1.ReceivedBytesThreshold = 4;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
      加入这段就报错了