2014年7月22日 星期二

使用C# 接收Uart_Data

主要code來源:
youtobe
功能算是較不齊全,主要只是有功能
介紹如何設定omport連線 & 目前時間取得
https://www.youtube.com/watch?v=NkyLErxr3ZA
介紹設定Comport
http://www.dotblogs.com.tw/peterdotnet/archive/2010/09/13/17697.aspx

小小大學生:功能很齊全 值得學習
http://www.dotblogs.com.tw/peterdotnet/archive/2010/09/13/17697.aspx

修改自動畫面調整: http://msdn.microsoft.com/zh-tw/library/dd492148.aspx

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

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            if (!myserialPort.IsOpen)
            {
                myserialPort.Open();
                tbRX.Text = "port Open :)";
            }
            else
                tbRX.Text = "port busy :( ";
         
     
        }

        private string rxstring;
        private void myserialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            rxstring = myserialPort.ReadExisting();
            this.Invoke(new EventHandler(displayText));
        }
        private void displayText(object o, EventArgs e)
        {
            tbRX.AppendText(rxstring);
        }

        private void bSend_Click(object sender, EventArgs e)
        {
            myserialPort.Write(tbTX.Text);
        }

        private void bClean_Click(object sender, EventArgs e)
        {
            tbTX.Clear();
            tbRX.Clear();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            myserialPort.Close();
        }

        private void tbTX_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (myserialPort.IsOpen && checkBox1.Checked)
            {
                char[] ch = new char[1];
                ch[0] = e.KeyChar;
                myserialPort.Write(ch, 0, 1);
            }
        }



    }
}

沒有留言:

張貼留言