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