本文整理汇总了C#中SerialPort.WriteLine方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.WriteLine方法的具体用法?C# SerialPort.WriteLine怎么用?C# SerialPort.WriteLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SerialPort
的用法示例。
在下文中一共展示了SerialPort.WriteLine方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
_serialPort = new SerialPort();
_serialPort.PortName = "COM2";
_serialPort.BaudRate = 9600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.XOnXOff;
// 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(" " + message + " ");
//String.Format("{0}:{1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
_serialPort = null;
}
示例2: MainS
public static void MainS()
{
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();
}
示例3: Main
public static void Main()
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
// Создание нового объекта SerialPort с установками по умолчанию.
_serialPort = new SerialPort();
// Позволяем пользователю установить подходящие свойства.
_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);
// Установка таймаутов чтения/записи (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();
}
示例4: Main
public static void Main()
{
string name;
string message;
var readThread = new Thread(Read);
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
InitializePort(_serialPort);
// 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 (message != null && "quit" == message.ToLowerInvariant())
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}
readThread.Join();
_serialPort.Close();
}
示例5: OpenConnection
public bool OpenConnection()
{
try
{
serialPort = new SerialPort("\\\\.\\" + portName, serialSpeed);
serialPort.Open();
if (serialPort.IsOpen)
{
serialPort.ReadTimeout = 1000 / 120; // this is half a unity frame
serialPort.WriteTimeout = 1000 / 120;
serialPort.WriteLine("arduinity_check"); // send message to arduino
string reply = "";
while (reply == "")
{
reply = serialPort.ReadLine();
if (reply != null && reply != "")
{
if (reply.StartsWith("arduinity_present"))
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): found arduinity on " + portName);
#endif
// parse the name
arduinoName = reply.Substring(reply.LastIndexOf('_') + 1);
return true;
}
else
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): got wrong response on " + portName + " " + reply);
#endif
return false;
}
}
}
}
else
{
#if debugSerial
Debug.Log("ArduinityCommunicator::OpenConnection(): " + portName + " is not open");
#endif
return false;
}
}
#if debugSerial
catch(Exception e)
{
Debug.Log("ArduinityCommunicator::OpenConnection(): exception trying to read from " + portName + " " + e.Message);
return false;
}
#else
catch
{
return false;
}
#endif
return false;
}
示例6: Start
/// <summary>
/// Initialisation of the Arduino IO-Box script.
/// </summary>
///
public void Start()
{
if ( !scriptVehicleData ) Debug.LogError("No vehicle data collector script defined!");
if ( !scriptConfiguration ) Debug.LogError("No simulation configuration script defined!");
vehicleData = null;
speedOfSound = scriptConfiguration.GetSpeedOfSound();
serialPort = new SerialPort(modulePort, moduleSpeed, Parity.None, 8, StopBits.One);
serialPort.Handshake = Handshake.None;
serialPort.RtsEnable = false;
serialPort.DtrEnable = false; // Disable DTR so NOT to reset Arduino board when connecting
serialPort.ReadTimeout = 250; // longer read timeout (module might have had a reset nevertheless)
bool success = false;
try
{
serialPort.Open();
}
catch (IOException)
{
serialPort = null;
}
if ( (serialPort != null) && serialPort.IsOpen )
{
int repeats = 8; // try several times to connect
while ( (repeats-- > 0) && !success )
{
// send the ECHO command
serialPort.WriteLine(CMD_ECHO);
try
{
String serialNo = serialPort.ReadLine();
if ( serialNo.Length > 1 )
{
Debug.Log ("Opened serial port " + modulePort + " to Arduino IO (Version: " + serialNo + ")");
success = true;
serialPort.ReadTimeout = 50; // from now on, shorter response times, please
StartCoroutine(RunDiagnose());
}
}
catch (TimeoutException)
{
// ignore
}
}
}
if ( !success )
{
Debug.LogError("Could not open serial port " + modulePort + " to the Arduino IO module.");
if ( serialPort != null )
{
serialPort.Close();
serialPort = null;
}
}
}
示例7: rcvAndEcho
private bool rcvAndEcho(ref SerialPort mySP)
{
byte rcv;
char tmp;
bool hasRcvd = false;
try {
rcv = (byte)mySP.ReadByte();
if (rcv != 255) {
hasRcvd = true;
tmp = (char)rcv;
if (tmp != 0x0d && tmp != 0x0a) { // not CRLF
accRcvd = accRcvd + tmp.ToString();
}
if (tmp == 0x0d) { // CR
mySP.WriteLine(accRcvd);
rcvdCRLF = true;
}
}
} catch (System.Exception) {
}
return hasRcvd;
}