2009-08-07 8 views
24

ハードウェアからC#GUIを使用してデータを送受信する方法を学び始めます。シリアルポートの読み書き方法

誰でも詳細を書いてください。データはシリアルポートから読み取ることができますか?

+0

可能重複(http://stackoverflow.com/questions/7275084/managing-serial-ports-in-c-sharp) –

+0

それ以外の方法でリンクされた投稿は、この投稿の複製です。正式な複製としてこの質問を使用してください。 – Lundin

答えて

57

SerialPort (RS-232 Serial COM Port) in C# .NET
この記事では、データの読み書き、シリアルポートがマシン上で利用可能であり、どのようにファイルを送信するかを決定するために、.NETでSerialPortクラスを使用する方法について説明します。ポート自体のピン割り当てもカバーします。

例コード:[C#でシリアルポートの管理]の

using System; 
using System.IO.Ports; 
using System.Windows.Forms; 

namespace SerialPortExample 
{ 
    class SerialPortProgram 
    { 
    // Create the serial port with basic settings 
    private SerialPort port = new SerialPort("COM1", 
     9600, Parity.None, 8, StopBits.One); 

    [STAThread] 
    static void Main(string[] args) 
    { 
     // Instatiate this class 
     new SerialPortProgram(); 
    } 

    private SerialPortProgram() 
    { 
     Console.WriteLine("Incoming Data:"); 

     // Attach a method to be called when there 
     // is data waiting in the port's buffer 
     port.DataReceived += new 
     SerialDataReceivedEventHandler(port_DataReceived); 

     // Begin communications 
     port.Open(); 

     // Enter an application loop to keep this thread alive 
     Application.Run(); 
    } 

    private void port_DataReceived(object sender, 
     SerialDataReceivedEventArgs e) 
    { 
     // Show all the incoming data in the port's buffer 
     Console.WriteLine(port.ReadExisting()); 
    } 
    } 
} 
関連する問題