概要
elm327を調べてみた。
c#でreader書いてみた。
写真
サンプルコード
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
aGauge1.Value = 10;
aGauge2.Value = 100;
}
private delegate void addDelegate(Byte[] buff);
private void domsg(Byte[] buff)
{
int i = to_h(buff[6]) * 16 + to_h(buff[7]);
aGauge1.Value = i;
Console.WriteLine(i);
System.Threading.Thread.Sleep(1300);
byte[] dat = {
0x30, 0x31, 0x30, 0x44
};
serialPort1.Write(dat, 0, dat.Length);
Console.WriteLine("send");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int j = serialPort1.BytesToRead;
if (j > 6)
{
Byte[] buff = new Byte[j];
serialPort1.Read(buff, 0, j);
BeginInvoke(new addDelegate(domsg), new object[] {
buff
});
}
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Close();
serialPort1.Open();
byte[] dat = {
0x30, 0x31, 0x30, 0x44
};
serialPort1.Write(dat, 0, dat.Length);
Console.WriteLine("send");
}
public static byte to_h(byte a)
{
if (a >= 0x30 && a <= 0x39)
return (byte)(a - 0x30);
if (a >= 0x41 && a <= 0x46)
return (byte)(a + 10 - 0x41);
if (a >= 0x61 && a <= 0x66)
return (byte)(a + 10 - 0x61);
return 255;
}
}
}
以上。
↧