첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ SerialPort 클래스 : 직렬 통신하기 예제

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region 프로그램 시작하기 - Main()

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        SerialPortManager serialPortManager = new SerialPortManager();

        serialPortManager.Start();
    }

    #endregion
}

 

728x90

 

▶ SerialPort 클래스 : 직렬 통신하기

using System;
using System.IO.Ports;
using System.Threading;

/// <summary>
/// 직렬 포트 관리자
/// </summary>
public class SerialPortManager
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 직렬 포트
    /// </summary>
    private SerialPort serialPort;

    /// <summary>
    /// 읽기 스레드
    /// </summary>
    private Thread readThread;

    /// <summary>
    /// 쓰기 스레드
    /// </summary>
    private Thread writeThread;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - SerialPortManager()

    /// <summary>
    /// 생성자
    /// </summary>
    public SerialPortManager()
    {
        try
        {
            this.serialPort = new SerialPort("COM1", 57600, Parity.None, 8);

            this.serialPort.Open();
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 시작하기 - Start()

    /// <summary>
    /// 시작하기
    /// </summary>
    public void Start()
    {
        this.readThread = new Thread(new ThreadStart(Read));

        this.readThread.IsBackground = true; // Background에서 실행되게 한다.

        this.readThread.Start();

        this.writeThread = new Thread(new ThreadStart(Write));

        this.writeThread.IsBackground = false; // 콘솔 창 출력을 위해 Foreground로 실행되게 한다.

        this.writeThread.Start();
    }

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 쓰기 - Write()

    /// <summary>
    /// 쓰기
    /// </summary>
    private void Write()
    {
        for(;;)
        {
            this.serialPort.WriteLine(Console.ReadLine());

            Thread.Sleep(200); // 200 밀리초 시간마다 다른 쓰레드가 실행될 수 있게 한다.
        }
    }

    #endregion

    #region 읽기 - Read()

    /// <summary>
    /// 읽기
    /// </summary>
    private void Read()
    {
        for(;;)
        {
            Console.Write(this.serialPort.ReadExisting());

            Thread.Sleep(200); // 200 밀리초 시간마다 다른 쓰레드가 실행될 수 있게 한다.
        }
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요