Csharp/C Sharp by API/System.IO.Ports/SerialPort

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new SerialPort("COM1", )

  
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);
      }
 }
}


SerialPort.BaseStream

  
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);
      }
 }
}


SerialPort.BaudRate

  

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.");
        }
    }
}


SerialPort.Parity

  

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.");
        }
    }
}


SerialPort.Read

  
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);
      }
 }
}


SerialPort.ReadTimeout

  

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.");
        }
    }
}


SerialPort.StopBits

  

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.");
        }
    }
}


SerialPort.Write

  

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.");
        }
    }
}