概要
arduinoでターミナルやってみる。
20字4行のLCDとps/2キーボードをつないで、シリアル端末を作ってみる。
テストの為、エコーなマスターをC#で書いてみた。
写真
サンプルコード
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));
}
}
}
以上。