当前位置: 首页>>代码示例>>C#>>正文


C# SerialPort.Read方法代码示例

本文整理汇总了C#中SerialPort.Read方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Read方法的具体用法?C# SerialPort.Read怎么用?C# SerialPort.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SerialPort的用法示例。


在下文中一共展示了SerialPort.Read方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

 public static void Main()
 {
     Debug.Print("BLE Shield");
     var port = SerialPorts.COM1; // using D0 & D1
     //var port = SerialPorts.COM2; // using D2 & D3
     var bleShield = new SerialPort(port, 19200, Parity.None, 8, StopBits.None);
     bleShield.DataReceived += (sender, args) =>
     {
         var receiveBuffer = new byte[16];
         int bytesReceived = bleShield.Read(receiveBuffer, 0, receiveBuffer.Length);
         if (bytesReceived > 0)
         {
             Debug.Print("Bytes received: " + bytesReceived);
             Debug.Print(new String(Encoding.UTF8.GetChars(receiveBuffer)));
         }
     };
     bleShield.Open();
     while (true)
     {
         var random = new Random();
         var sendBuffer = new byte[4];
         random.NextBytes(sendBuffer);
         bleShield.Write(sendBuffer, 0, sendBuffer.Length);
         Thread.Sleep(1000);
     }
 }
开发者ID:thisischris,项目名称:BLE-Shield,代码行数:26,代码来源:Program.cs

示例2: ExampleThreadFunction

        public static void ExampleThreadFunction()
        {

            // bit rate change acording your GPS
            SerialPort serialPort = new SerialPort("COM3", 4800);
            serialPort.Open();

            //here we create file in SD card main folder
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @"\gps.txt", FileMode.Create);


            while (true)
            {

                int bytesToRead = serialPort.BytesToRead;
                if (bytesToRead > 0)
                {

                    // all struff from GPS streams in to file
                    byte[] buffer = new byte[bytesToRead];
                    serialPort.Read(buffer, 0, buffer.Length);
                    Debug.Print(new String(System.Text.Encoding.UTF8.GetChars(buffer)) + "\n");
                    FileHandle.Write(buffer, 0, buffer.Length);
                    Thread.Sleep(500);
                }
                // Cleaning 
                Debug.GC(true);
                Debug.EnableGCMessages(false);

            }

        }
开发者ID:BdGL3,项目名称:CXPortal,代码行数:33,代码来源:FileIO.cs


注:本文中的SerialPort.Read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。