본문 바로가기
삽질기초

C# 간단한 시리얼 프로그램.

by @가을바람 2009. 4. 1.





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace rs232
{
    public partial class Form1 : Form
    {

        string InputData = String.Empty;

        // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control:
        delegate void SetTextCallback(string text);

        
        public Form1()
        {
            InitializeComponent();
            /////////////////////combo - com////////////////////////
            string[] ports = SerialPort.GetPortNames();

            // Add all port names to the combo box:
            foreach (string port in ports)
            {
                cmbComSelect.Items.Add(port);
            }
            /////////////////////combo - Baudlate////////////////////////
            System.Int32[] Baudlates = { 9600, 19200, 38400 };

            for (int temp = 0; temp < 3; temp++)
            {
                cmbComBaud.Items.Add(Baudlates[temp]);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void cmbComSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (port.IsOpen) port.Close();
            port.PortName = cmbComSelect.SelectedItem.ToString();
            stsStatus.Text = port.PortName;

            // try to open the selected port:
            try
            {
                port.Open();
            }
            // give a message, if the port is not available:
            catch
            {
                MessageBox.Show("Serial port " + port.PortName + " cannot be opened!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbComSelect.SelectedText = "";
                stsStatus.Text = "Select serial port!";
            }
        }

        private void cmbComBaud_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.String SelectedBaud;
            
            SelectedBaud = cmbComBaud.SelectedItem.ToString() +",8N1";
            BaudText.Text = SelectedBaud; //아래쪽 출력

            port.BaudRate = (Int32)cmbComBaud.SelectedItem; //보레이트 입력

           // txtIn.Text = port.BaudRate.ToString(); 확인작업
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (port.IsOpen) port.WriteLine(txtOut.Text);
            else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //txtOut.Clear();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtIn.Clear();
        }

        private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
        {
            InputData = port.ReadExisting();
            if (InputData != String.Empty)
            {
                //txtIn.Text = InputData;   // because of different threads this does not work properly !!
                SetText(InputData);
            }
        }

        private void SetText(string text)
        {
            if (this.txtIn.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else this.txtIn.Text += text;
        }


    }
}




----

예외처리도 없고~
코드프로젝트에서 참고함.

'삽질기초' 카테고리의 다른 글

Power Systems book  (0) 2009.11.13
MATLAB 원 그리기.  (0) 2009.11.06
Model-Based Design 을 이용한 임베디드 시스템의 개발  (0) 2009.11.03
matlab shortcuts. 단축키.  (0) 2009.11.03
http://freebookspot.in/  (0) 2009.07.10