-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceAPI.cs
158 lines (136 loc) · 4.12 KB
/
DeviceAPI.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Software License Agreement
*
* Copyright (C) Cross The Road Electronics. All rights
* reserved.
*
* Cross The Road Electronics (CTRE) licenses to you the right to
* use, publish, and distribute copies of CRF (Cross The Road) firmware files (*.crf) and Software
* API Libraries ONLY when in use with Cross The Road Electronics hardware products.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* CROSS THE ROAD ELECTRONICS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE
*/
using System;
using System.Text;
using System.Collections.ObjectModel;
using Microsoft.NetMicroFramework.Tools.MFDeployTool.Engine;
using System.Threading;
class DeviceAPI
{
private StringBuilder m_sb = new StringBuilder();
private MFDeploy m_deploy = new MFDeploy();
private MFDevice m_device = null;
private int m_devRefCount = 0;
/// <summary>
/// Calling application must Dispose this object before end of app.
/// </summary>
public void Dispose()
{
m_deploy.Dispose();
}
public bool IsConnected()
{
return m_devRefCount > 0;
}
private MFPortDefinition FindHeroPortDef()
{
MFPortDefinition retval = null;
ReadOnlyCollection<MFPortDefinition> list = null;
list = m_deploy.EnumPorts(TransportType.USB);
if (list.Count > 0)
{
retval = list[0];
}
return retval;
}
public int Connect()
{
int retval = 0; /* okay */
if (m_device != null)
{
Interlocked.Increment(ref m_devRefCount);
}
else
{
MFPortDefinition port;
try
{
port = FindHeroPortDef();
}
catch (Exception excep)
{
Console.Out.WriteLine(excep.Message);
return -5; /* exception suggests HERO is connected to VS */
}
if (port == null)
{
retval = -1; /* could not find a device */
}
else
{
try
{
m_device = m_deploy.Connect(port, null);
if (m_device != null)
{
m_device.OnDebugText += new EventHandler<DebugOutputEventArgs>(OnDbgTxt);
Interlocked.Increment(ref m_devRefCount);
}
else
{
retval = -2; /* tried to connect and failed */
}
}
catch (Exception)
{
retval = -3; /* tried to connect and failed */
}
}
}
return retval;
}
public bool Disconnect()
{
bool fDisconnected = (m_device == null);
if (m_device != null)
{
if (Interlocked.Decrement(ref m_devRefCount) <= 0)
{
fDisconnected = true;
m_device.OnDebugText -= new EventHandler<DebugOutputEventArgs>(OnDbgTxt);
m_device.Dispose();
m_device = null;
}
}
else
{
}
return fDisconnected;
}
public void OnDbgTxt(object sender, DebugOutputEventArgs e)
{
lock (m_sb)
{
m_sb.Append(e.Text);
}
}
public String GetCachedOutputBuffer()
{
lock (m_sb)
{
String retval = m_sb.ToString();
m_sb.Clear();
return retval;
}
}
}