2009-08-07 7 views
24

C# GUI를 통해 하드웨어에서 데이터를 보내고받는 방법을 배우기 시작했습니다.직렬 포트에서 읽고 쓰는 방법

누구든지 세부 사항을 작성하십시오. 데이터를 직렬 포트에서 읽으 실 수 있습니까?

+0

[C#으로 시리얼 포트 관리]의 중복 가능성 (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 클래스를 사용하는 방법에 대해 설명합니다. 포트 자체의 핀 지정까지도 포함합니다.

예 번호 :

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()); 
    } 
    } 
} 
관련 문제