111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO.Ports;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace ebbits.Arduino {
|
|
abstract class AbstractArduino {
|
|
protected SerialPort arduino;
|
|
protected LinkedList<ArduinoInput> readStack = new LinkedList<ArduinoInput>();
|
|
protected void init(string port) {
|
|
arduino = new SerialPort(port, 57600);
|
|
arduino.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
|
|
arduino.Open();
|
|
arduino.WriteLine("");
|
|
|
|
}
|
|
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
|
|
SerialPort sp = (SerialPort)sender;
|
|
string s = sp.ReadLine();
|
|
Logger.Serial("in", s);
|
|
if(ArduinoInput.check(s)) {
|
|
readStack.AddLast(new ArduinoInput(s));
|
|
}
|
|
}
|
|
protected void serialSend(char key, byte pos, int val) {
|
|
char c = ArduinoInput.checksum(key + pos.ToString() + "=" + val.ToString());
|
|
string s = "*" + key + pos.ToString() + "=" + val + "$" + c + "#";
|
|
arduino.WriteLine(s);
|
|
Logger.Serial("out", s);
|
|
}
|
|
|
|
protected ArduinoInput getInput(char key, byte id = 255) {
|
|
LinkedList<ArduinoInput>.Enumerator e = readStack.GetEnumerator();
|
|
try {
|
|
while(e.MoveNext()) {
|
|
if((e.Current.key == key && e.Current.id == id) || (id == 255 && e.Current.key == key)) {
|
|
ArduinoInput a = e.Current;
|
|
readStack.Remove(a);
|
|
return a;
|
|
}
|
|
}
|
|
} catch(InvalidOperationException) {
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
protected ArduinoInput getInputWait(char key, byte id = 255) {
|
|
while(true) {
|
|
ArduinoInput a = getInput(key, id);
|
|
if(a != null) {
|
|
return a;
|
|
}
|
|
System.Threading.Thread.Sleep(1);
|
|
}
|
|
}
|
|
|
|
internal void clearStack() {
|
|
readStack.Clear();
|
|
}
|
|
|
|
internal void clearStack(char key) {
|
|
LinkedList<ArduinoInput>.Enumerator e = readStack.GetEnumerator();
|
|
while(e.MoveNext()) {
|
|
if(e.Current.key == key) {
|
|
readStack.Remove(e.Current);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
class ArduinoInput {
|
|
public ArduinoInput(string indata) {
|
|
string reg = "\\*([a-z])(\\d)=([a-fA-F0-9]*)\\$(.)#";
|
|
Match m = Regex.Match(indata, reg);
|
|
this.key = m.Groups[1].ToString().ToCharArray()[0];
|
|
this.id = byte.Parse(m.Groups[2].ToString());
|
|
string val = m.Groups[3].ToString().ToUpper();
|
|
if(val == "") {
|
|
this.value = 0;
|
|
} else if(val.IndexOfAny(new char[] { 'A', 'B', 'C', 'D', 'E', 'F' }) != -1) {
|
|
this.value = Int64.Parse(val, System.Globalization.NumberStyles.HexNumber);
|
|
} else {
|
|
this.value = Int64.Parse(val);
|
|
}
|
|
}
|
|
|
|
public static char checksum(String data) {
|
|
char c = '$';
|
|
foreach(char t in data) {
|
|
c = (char)(c ^ t);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
public static bool check(string indata) {
|
|
string reg = "\\*([a-z]\\d)=([a-fA-F0-9]*)\\$(.)#";
|
|
Match m = Regex.Match(indata, reg);
|
|
if(!m.Success) {
|
|
return false;
|
|
}
|
|
String rec = m.Groups[1].ToString() + "=" + m.Groups[2].ToString();
|
|
return (ArduinoInput.checksum(rec) == m.Groups[3].ToString().ToCharArray()[0]);
|
|
}
|
|
|
|
public char key { get; private set; }
|
|
public byte id { get; private set; }
|
|
public Int64 value { get; private set; }
|
|
}
|
|
}
|