日期:2014-05-17  浏览次数:20810 次

C#串口通信
本帖最后由 BenBenBears 于 2013-08-26 08:39:11 编辑
最近在学习C#串口通信,调用C#的SerialPort类开发,编写接收程序时遇到如下的问题:
   
  private void aTimer_Elapsed(object sender, ElapsedEventArgs s)
        {
            if (isOpen)
            {
                try
                {
                    //SetPortProperty();
                    //ScreenBox.Text += sp.ReadExisting();

                    //string overWord="over";
                    //sp.NewLine = overWord;
                    //ScreenBox.Text += sp.ReadLine();
                    ChangeText CText = delegate()//声明和定义一个匿名代理
                    {
                       // ScreenBox.Text += sp.ReadExisting();

                        string dataRev; 
                        byte[] byteRead=new byte[serialPort1.BytesToRead;            
                        sp.Read(byteRead, 0, byteRead.Length);
                        dataRev = Encoding.Default.GetString(byteRead);
                        ScreenBox.Text += dataRev;