LoginSignup
1
1

More than 5 years have passed since last update.

arduinoでターミナル その3

Posted at

概要

arduinoでターミナルやってみる。
20字4行のLCDとps/2キーボードをつないで、シリアル端末を作ってみる。
テストの為、エコーなマスターをC#で書いてみた。

写真

echo.jpg

サンプルコード

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 WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        private static string recBuff = "";
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!serialPort1.IsOpen)
                {
                    serialPort1.Open();
                    richTextBox1.AppendText("start\n");
                }
                else
                {
                    Console.WriteLine("error0");
                    richTextBox1.Text = "Failed to open COM port";
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                recBuff = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(DisplayText));
            }
            catch (System.TimeoutException)
            {
                Console.WriteLine("error1");
            }
        }
        private void DisplayText(object s, EventArgs e)
        {
            int buffLen = recBuff.Length;
            int buffPos = -1;
            int c = ' ';
            if (buffLen > 1)
            {
                if ((buffPos = recBuff.IndexOf("\r")) >= 0)
                {
                    richTextBox1.AppendText(recBuff.Substring(0, buffPos));
                    richTextBox1.AppendText("[CR]");
                    richTextBox1.AppendText(recBuff.Substring(buffPos, (buffLen - buffPos)));
                }
                else
                {
                    richTextBox1.AppendText(recBuff);
                }
            }
            else
            {
                c = recBuff[0];
                if (c == 13)
                {
                    richTextBox1.AppendText("[CR]");
                    richTextBox1.AppendText(recBuff);
                }
                else if (c == 7)
                {
                    richTextBox1.AppendText("[BELL]\n");
                }
            }
            Console.WriteLine(recBuff);
            Byte[] dat = System.Text.Encoding.GetEncoding("SHIFT-JIS").GetBytes(recBuff);
            serialPort1.Write(dat, 0, dat.GetLength(0));
        }
    }
}




以上。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1