首页 新闻 会员 周边 捐助

C# SerialPort 串口通讯

0
悬赏园豆:10 [已关闭问题]

我用VS 2005 C#写串口 在打开串口就有问题 帮忙看看啊

  public partial class Form1 : Form
    {
        myCom1 comm = new myCom1();
        parameter p = new parameter();
        public Form1()
        {
            InitializeComponent();

        }

 

 public   class myCom1
    {
      public event SerialDataReceivedEventHandler Received;
      public event SerialErrorReceivedEventHandler Error;

      public bool ReceiveEventFlag = false;  //接收事件是否有效 false表示有效

       
      public SerialPort SpCom1 ;
      public SerialPort SpCom2 ;
        //初始化串口
      public myCom1()
      {
          SpCom1 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
          SpCom1.RtsEnable = true;
          SpCom1.ReadTimeout = 3000;
          SpCom1.DataReceived += new SerialDataReceivedEventHandler(DataReceived1);
          SpCom1.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);

          SpCom2 = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One);
          SpCom2.RtsEnable = true;
          SpCom2.ReadTimeout = 3000;
          SpCom2.DataReceived += new SerialDataReceivedEventHandler(DataReceived2);
          SpCom2.ErrorReceived += new SerialErrorReceivedEventHandler(ErrorEvent);

         
      }
   
       ~myCom1()
        {
            Close1();
            Close2();
        }
       // public SerialPort SpCom1 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
        //public SerialPort SpCom2 = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One);

        //打开串口
        public void Open1()
        {
          
            if (!SpCom1.IsOpen)
            SpCom1.Open();
        }

       public void Open2()
        {
        
            if (!SpCom2.IsOpen)
            SpCom2.Open();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;

           // comm.Open1();
        }
           //打开串口
        private void Bt_Open_Click(object sender, EventArgs e)
        {
            comm.Open1();
           
            comm.Open2();  
        }

 

在监控器中显示发送两遍

Read:

Baud rate set to 19200

StopBits: 1 stop bit, Parity: No parity, WordLength: 8

Baud rate set to 19200

StopBits: 1 stop bit, Parity: No parity, WordLength: 8

Port closed

write:

Port opened

Baud rate set to 19200

StopBits: 1 stop bit, Parity: No parity, WordLength: 8

Baud rate set to 19200

StopBits: 1 stop bit, Parity: No parity, WordLength: 8

Port closed

 

小芊的主页 小芊 | 初学一级 | 园豆:180
提问于:2010-02-21 16:58
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册