本文整理汇总了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);
}
}
示例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);
}
}