Csharp/C Sharp by API/System.IO.Ports/SerialPort
Содержание
new SerialPort("COM1", )
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; class Serial {
public static void Main() { byte[] buffer = new byte[256]; using (SerialPort sp = new SerialPort("COM1", 19200)) { sp.Open(); //read directly sp.Read(buffer, 0, (int)buffer.Length); //read using a Stream sp.BaseStream.Read(buffer, 0, (int)buffer.Length); } }
}
</source>
SerialPort.BaseStream
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; class Serial {
public static void Main() { byte[] buffer = new byte[256]; using (SerialPort sp = new SerialPort("COM1", 19200)) { sp.Open(); //read directly sp.Read(buffer, 0, (int)buffer.Length); //read using a Stream sp.BaseStream.Read(buffer, 0, (int)buffer.Length); } }
}
</source>
SerialPort.BaudRate
<source lang="csharp">
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."); } }
}
</source>
SerialPort.Parity
<source lang="csharp">
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."); } }
}
</source>
SerialPort.Read
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.IO.Ports; class Serial {
public static void Main() { byte[] buffer = new byte[256]; using (SerialPort sp = new SerialPort("COM1", 19200)) { sp.Open(); //read directly sp.Read(buffer, 0, (int)buffer.Length); //read using a Stream sp.BaseStream.Read(buffer, 0, (int)buffer.Length); } }
}
</source>
SerialPort.ReadTimeout
<source lang="csharp">
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."); } }
}
</source>
SerialPort.StopBits
<source lang="csharp">
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."); } }
}
</source>
SerialPort.Write
<source lang="csharp">
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."); } }
}
</source>