概要
elm327の研究やってみた。
練習問題やってみた。
練習問題
c#で、ELM327のエミュレータを書け。
方針
- OBDscanの方、ボタン1だけでなく、ボタン2も実装。
- ELM327エミュ、書き直し。
写真
サンプルコード
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
namespace OBD
{
public class ELM327 {
public bool IsOpen = true;
public int BytesToRead = 2;
public string res = "";
public void Open() {
}
public void Close() {
}
public string Read() {
if (BytesToRead > 1)
{
BytesToRead = 1;
return res;
}
else
{
BytesToRead = 0;
return ">";
}
}
public void Write(string a) {
//Console.WriteLine(a);
string[] b = a.Split(new string[] {
"\r"
}, StringSplitOptions.None);
res = "";
int len = b[0].Length;
if (len < 2)
{
return;
}
if (b[0].Substring(0, 2) == "AT")
{
switch (b[0])
{
case "ATWS":
case "ATE0":
case "ATL0":
case "ATH1":
case "ATSP0":
break;
case "AT@1":
res += "OBDII to RS232 Interpreter";
break;
case "ATRV":
res += "12.8V";
break;
case "ATZ":
case "ATI":
res += "ELM327 v1.4";
break;
case "ATDP":
res += "AUTO, ISO 9141-2";
break;
default:
res += " NO COMMAND\n";
break;
}
}
else
{
switch (b[0])
{
case "01 00":
res += "41 00 FF FF FC FF";
break;
case "01 01":
res += "41 01 84 07 61 00";
break;
case "01 20":
res += "41 20 FF FF FC FF";
break;
case "01 40":
res += "41 40 FF FF FC FF";
break;
case "01 60":
res += "41 60 FF FF FC FF";
break;
case "01 80":
res += "41 80 FF FF FC FF";
break;
case "01 A0":
res += "41 A0 FF FF FC FF";
break;
case "01 C0":
res += "41 C0 FF FF FC FF";
break;
case "04 ":
res += "44";
break;
default:
res += b[0] + " NO DATA\n";
break;
}
}
BytesToRead = 2;
//Console.WriteLine(res);
return;
}
}
public partial class Form1: Form {
private ELM327 sp;
public TextBox bo1;
public TextBox bo2;
public TextBox bo3;
public TextBox bo4;
private int pc = 0;
public Form1() {
Text = "OBDscan";
ClientSize = new Size(600, 600);
Label label0 = new Label();
label0.Location = new Point(20, 10);
label0.Text = "ATZ";
bo1 = new TextBox();
bo1.Text = "ELM327 v1.4";
bo1.Font = new Font("MS Pゴシック", 12);
bo1.Location = new Point(50, 40);
bo1.Size = new Size(300, 20);
Controls.AddRange(new Control[] {
label0,
bo1
});
Label label1 = new Label();
label1.Location = new Point(20, 70);
label1.Text = "ATRV";
bo2 = new TextBox();
bo2.Text = "12.8V";
bo2.Font = new Font("MS Pゴシック", 12);
bo2.Location = new Point(50, 100);
bo2.Size = new Size(300, 20);
Controls.AddRange(new Control[] {
label1,
bo2
});
Label label2 = new Label();
label2.Location = new Point(20, 140);
label2.Text = "AT@1";
bo3 = new TextBox();
bo3.Text = "OBDII to RS232 Interpreter";
bo3.Font = new Font("MS Pゴシック", 12);
bo3.Location = new Point(50, 170);
bo3.Size = new Size(300, 20);
Controls.AddRange(new Control[] {
label2,
bo3
});
Button btn0 = new Button();
btn0.Location = new Point(150, 220);
btn0.Text = "start";
btn0.Click += btn0_Click;
Controls.AddRange(new Control[] {
btn0
});
Button btn1 = new Button();
btn1.Location = new Point(450, 220);
btn1.Text = "next";
btn1.Click += btn1_Click;
Controls.AddRange(new Control[] {
btn1
});
bo4 = new TextBox();
bo4.Font = new Font("MS Pゴシック", 12);
bo4.Location = new Point(50, 250);
bo4.Size = new Size(400, 300);
bo4.Multiline = true;
Controls.AddRange(new Control[] {
bo4
});
}
void btn0_Click(object sender, System.EventArgs e) {
Log("program start ok\r\n");
Timer timer = new Timer {
Interval = 500
};
timer.Tick += timer_Tick;
timer.Start();
pc = 0;
sp = new ELM327();
sp.Open();
sp.Write("ATWS\r");
Log("ATWS\r");
}
void btn1_Click(object sender, System.EventArgs e) {
Log("next ok\r\n");
pc = 20;
sp.Write("ATWS\r");
Log("ATWS\r");
}
private void timer_Tick(object sender, EventArgs e) {
while (sp.IsOpen && sp.BytesToRead > 0)
{
string inString = sp.Read();
Log(inString);
if (inString == ">")
{
pc++;
switch (pc)
{
case 1:
sp.Write("ATZ\r");
Log("ATZ\r");
break;
case 2:
sp.Write("ATE0\r");
Log("ATE0\r");
break;
case 3:
sp.Write("ATL0\r");
Log("ATL0\r");
break;
case 4:
sp.Write("ATH1\r");
Log("ATH1\r");
break;
case 5:
sp.Write("ATSP0\r");
Log("ATSP0\r");
break;
case 6:
sp.Write("ATI\r");
Log("ATI\r");
break;
case 7:
sp.Write("AT@1\r");
Log("AT@1\r");
break;
case 8:
sp.Write("ATRV\r");
Log("ATRV\r");
break;
case 9:
sp.Write("01 00\r");
Log("01 00\r");
break;
case 10:
Log("ECU Ready for OBD actions");
sp.Write("ATDP\r");
Log("ATDP\r");
break;
case 11:
sp.Write("01 01\r");
Log("01 01\r");
break;
case 13:
Log("Erase acknowledged by ECU");
Log("Restart vehicle to reflect changes");
break;
case 14:
pc--;
break;
case 21:
sp.Write("01 01\r");
Log("01 01\r");
break;
case 22:
pc--;
break;
}
}
}
}
private void Log(string Message) {
bo4.AppendText(Message + Environment.NewLine);
}
[STAThread]
public static void Main() {
Application.Run(new Form1());
}
}
}
以上。