本文整理匯總了C#中System.IO.Ports.SerialPort.ReadTimeout屬性的典型用法代碼示例。如果您正苦於以下問題:C# SerialPort.ReadTimeout屬性的具體用法?C# SerialPort.ReadTimeout怎麽用?C# SerialPort.ReadTimeout使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.IO.Ports.SerialPort
的用法示例。
在下文中一共展示了SerialPort.ReadTimeout屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = SetPortName(_serialPort.PortName);
_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = SetPortParity(_serialPort.Parity);
_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
Console.Write("Name: ");
name = Console.ReadLine();
Console.WriteLine("Type QUIT to exit");
while (_continue)
{
message = Console.ReadLine();
if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
public static void Read()
{
while (_continue)
{
try
{
string message = _serialPort.ReadLine();
Console.WriteLine(message);
}
catch (TimeoutException) { }
}
}
示例2: Main
//引入命名空間
using System;
using System.IO.Ports;
static class MainClass
{
static void Main(string[] args)
{
using (SerialPort port = new SerialPort("COM1"))
{
// Set the properties.
port.BaudRate = 9600;
port.Parity = Parity.None;
port.ReadTimeout = 10;
port.StopBits = StopBits.One;
// Write a message into the port.
port.Open();
port.Write("Hello world!");
Console.WriteLine("Wrote to the port.");
}
}
}