/
ArduinoDriver.cs
72 lines (66 loc) · 2.51 KB
/
ArduinoDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using ArduinoDynamicDriver.SerialProtocol;
using ArduinoDynamicDriver;
namespace ArduinoDynamicDriver
{
public class ArduinoDriver //: IDisposable
{
private const int CurrentProtocolMajorVersion = 1;
private const int CurrentProtocolMinorVersion = 2;
private const int DriverBaudRate = 115200;
private ArduinoDriverSerialPort port;
/// <summary>
/// Creates a new ArduinoDriver instance. The relevant portname will be autodetected if possible.
/// </summary>
/// <param name="arduinoModel"></param>
/// <param name="autoBootstrap"></param>
public ArduinoDriver(ISerialStream serialPortStream)
{
serialPortStream.SetBaudRate(DriverBaudRate);
serialPortStream.Open();
port = new ArduinoDriverSerialPort(serialPortStream);
}
public object Execute(ArduinoFunction function)
{
var result = port.Send(function);
object netObject=null;
switch (function.ReturnType)
{
case SupportedType.BOOLEANT:
netObject = result[0] == 0 ? false : true;
break;
case SupportedType.CHART:
netObject = (sbyte)result[0];
break;
case SupportedType.UCHART:
netObject = result[0];
break;
case SupportedType.BYTET:
netObject = result[0];
break;
case SupportedType.INTT:
netObject = BitConverter.ToInt16(result, 0);
break;
case SupportedType.UINTT:
netObject = BitConverter.ToUInt16(result, 0);
break;
case SupportedType.LONG:
netObject = BitConverter.ToInt32(result, 0);
break;
case SupportedType.ULONG:
netObject = BitConverter.ToUInt32(result, 0);
break;
case SupportedType.SHORT:
netObject = BitConverter.ToInt16(result, 0);
break;
}
return netObject;
}
}
}