C# 串口读取并转换字符串

public string ReadString()
       {
           ASCIIEncoding ascii = new ASCIIEncoding();
           byte[] readBuffer = new byte[serialPort.BytesToRead];
           serialPort.Read(readBuffer, 0, readBuffer.Length);

           if (readBuffer.Length>0)
           {
               string result  = ascii.GetString(readBuffer);
               Console.WriteLine(DateTime.Now);
               Console.WriteLine(result);
               return result;
           }
           return "";
       }

posted on 2021-11-04 15:43  三千世界a  阅读(1036)  评论(0编辑  收藏  举报

导航