This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCanAppli.cs
282 lines (236 loc) · 9.88 KB
/
CCanAppli.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using Microsoft.SPOT;
using System.Collections;
using System.Threading;
using GHI.IO;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Microsoft.SPOT.Hardware;
using System.IO.Ports;
namespace PR
{
enum idErreur : int
{
notDefined, OK = 1, STOPPED = 2, BLOCKED = 3, ACKSTOP = 5, TOCANCEL,
// Code erreur
FatalErreurCAN = -1, ErrorReceived = -2, timoutACK = -3, timoutDONE = -4,
};
enum IDcommande : uint
{
// Id WIDAR
WStop = 0x100, WStopACK, WStopDONE,
WSetColor = 0x103, WSetColorACK, WSetColorDONE,
WSetSpeed = 0x106, WSetSpeedACK, WSetSpeedDONE,
WGoTo = 0x109, WGoToACK, WGoToDONE,
WGoTo2 = 0x10C, WGoTo2ACK, WGoTo2DONE,
WRotate = 0x10F, WRotateACK, WRotateDONE,
WGetPosition = 0x112, WGetPositionACK, WGetPositionDONE,
WGetCodeur = 0x115, WGetCodeurACK, WGetCodeurDONE,
WBeBlocked = 280
};
class CCanAppli
{
//private GTM.GHIElectronics.CANDW m_busCan;
private ControllerAreaNetwork m_busCan;
private int returnWaitAny;
static private object mutex;
uint m_id;
Bitmap m_LCD;
Font m_font;
// Déclaration du tableau de data reçu.
ControllerAreaNetwork.Message[] m_msgRecu;
// CAN.Message[] m_msgRecu = new CAN.Message[1];
// Déclaration des Evenements
static WaitHandle[] m_tabWaitDONE;
static WaitHandle[] m_waitACK;
idErreur m_erreur;
public CCanAppli(Font font) //J'ai modifié le code en supprimant le Bitmap LCD du paramètre
{
//m_busCan=new GTM.GHIElectronics.CANDW(6);
m_busCan = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, ControllerAreaNetwork.Speed.Kbps250);
m_font = font;
// m_LCD = LCD;
//
m_tabWaitDONE = new WaitHandle[]
{
new AutoResetEvent(false), //DONE
new AutoResetEvent(false), //STOPPED
new AutoResetEvent(false) //BLOCKED
};
m_waitACK = new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false),
};
m_msgRecu = new ControllerAreaNetwork.Message[10];
for (int i = 0; i < 10; i++)
m_msgRecu[i] = new ControllerAreaNetwork.Message();
// m_busCan.Initialize(GHI.IO.ControllerAreaNetwork.Speed.Kbps125, GHI.IO.ControllerAreaNetwork.Channel.One);
//m_busCan.ErrorReceived += m_busCan_ErrorReceived;
// m_busCan.MessagesReceived += m_busCan_MessagesReceived;
m_busCan.ErrorReceived += m_busCan_ErrorReceived;
m_busCan.MessageAvailable += m_busCan_MessageAvailable;
m_busCan.Enabled = true;
// mutex = new Object();
}
void m_busCan_MessageAvailable(ControllerAreaNetwork sender, ControllerAreaNetwork.MessageAvailableEventArgs e)
{
m_msgRecu[0] = sender.ReadMessage();
Debug.Print("CanId reçu" + m_msgRecu[0].ArbitrationId.ToString());
AutoResetEvent evtDONE = (AutoResetEvent)m_tabWaitDONE[0];
AutoResetEvent evtSTOP = (AutoResetEvent)m_tabWaitDONE[1];
AutoResetEvent evtBLOCKED = (AutoResetEvent)m_tabWaitDONE[2];
AutoResetEvent evtACK = (AutoResetEvent)m_waitACK[0];
AutoResetEvent evtACKSTOP = (AutoResetEvent)m_waitACK[1];
// Etude de la commande reçue.
if ((m_msgRecu[0].ArbitrationId != (uint)IDcommande.WStopACK) && (m_msgRecu[0].ArbitrationId != (uint)IDcommande.WStopDONE))
if (m_msgRecu[0].ArbitrationId == m_id + 1)
evtACK.Set();
else if (m_msgRecu[0].ArbitrationId == m_id + 2)
evtDONE.Set();
switch (m_msgRecu[0].ArbitrationId)
{
case (uint)IDcommande.WStopACK:
evtACKSTOP.Set();
break;
case (uint)IDcommande.WStopDONE:
evtSTOP.Set();
break;
case (uint)IDcommande.WBeBlocked:
evtBLOCKED.Set();
break;
}
}
void m_busCan_ErrorReceived(ControllerAreaNetwork sender, ControllerAreaNetwork.ErrorReceivedEventArgs e)
{
throw new NotImplementedException();
}
public idErreur envoyer(uint id, byte[] data = null, int nbData = 0)
{
idErreur retour = idErreur.FatalErreurCAN;
m_id = id;
// Tableau d'envoie de commande.
ControllerAreaNetwork.Message[] commande;
commande = new ControllerAreaNetwork.Message[1];
commande[0] = new ControllerAreaNetwork.Message();
//construction de la trame
commande[0].ArbitrationId = id;
commande[0].Length = nbData;
commande[0].IsExtendedId = false;
commande[0].IsRemoteTransmissionRequest = false;
if (nbData != 0)
for (int i = 0; i < nbData; i++)
commande[0].Data[i] = data[i];
// --- lock de la commande à envoyer
//System.Threading.Monitor.Enter(mutex);
m_busCan.SendMessages(commande);
Debug.Print("CanId Envoye" + commande[0].ArbitrationId.ToString());
// ### Commande envoyé ###
returnWaitAny = WaitHandle.WaitAny(m_waitACK, 500, false);
switch (returnWaitAny)
{
case 0:
retour = idErreur.OK;
break;
case 1:
retour = idErreur.ACKSTOP;
break;
default:
retour = idErreur.timoutACK;
break;
}
// --- unlock de la commande
//System.Threading.Monitor.Exit(mutex);
return retour;
}
void m_busCan_ErrorReceived(CANDW sender, CANDW.ErrorReceivedEventArgs args)
{
//throw new NotImplementedException();
Debug.Print(">>> can_ErrorReceivedEvent <<<");
switch (args.Error)
{
/* case CAN.Error.Overrun:
Debug.Print("Overrun error. Message lost");
break;*/
case ControllerAreaNetwork.Error.RXOver:
Debug.Print("RXOver error. Internal buffer is full. Message lost");
break;
case ControllerAreaNetwork.Error.BusOff:
Debug.Print("BusOff error. Reset CAN controller.");
sender.Reset();
break;
case ControllerAreaNetwork.Error.ErrorPassive:
Debug.Print("Error Passive.");
break;
}
}
/* void m_busCan_MessagesReceived(CANDW sender, CANDW.MessagesReceivedEventArgs e) // bus Can va receptionner le message
{
int count = e.MessageCount;
for(int i=0;i<count;i++)
m_msgRecu[i] = e.Messages[i];
Debug.Print("CanId reçu" + m_msgRecu[0].ArbitrationId.ToString());
AutoResetEvent evtDONE = (AutoResetEvent)m_tabWaitDONE[0];
AutoResetEvent evtSTOP = (AutoResetEvent)m_tabWaitDONE[1];
AutoResetEvent evtBLOCKED = (AutoResetEvent)m_tabWaitDONE[2];
AutoResetEvent evtACK = (AutoResetEvent)m_waitACK[0];
AutoResetEvent evtACKSTOP = (AutoResetEvent)m_waitACK[1];
// Etude de la commande reçus.
for (int i = 0; i < count; i++)
{
if ((m_msgRecu[i].ArbitrationId != (uint)IDcommande.WStopACK) && (m_msgRecu[i].ArbitrationId != (uint)IDcommande.WStopDONE))
if (m_msgRecu[i].ArbitrationId == m_id + 1)
evtACK.Set();
else if (m_msgRecu[i].ArbitrationId == m_id + 2)
evtDONE.Set();
switch (m_msgRecu[i].ArbitrationId)
{
case (uint)IDcommande.WStopACK:
evtACKSTOP.Set();
break;
case (uint)IDcommande.WStopDONE:
evtSTOP.Set();
break;
case (uint)IDcommande.WBeBlocked:
evtBLOCKED.Set();
break;
}
}
}*/
public idErreur isDone(int timeOutDONE, byte[] data = null, int nbData = 0)
{
idErreur retour = idErreur.OK;
// Attente DONE - DONE = 0; Stop = 1; Timer
returnWaitAny = WaitHandle.WaitAny(m_tabWaitDONE, timeOutDONE, false);
switch (returnWaitAny)
{
case 0:
retour = idErreur.OK;
if (m_msgRecu[0].Length != 0)
{
for (int i = 0; i < m_msgRecu[0].Length; i++)
data[i] = m_msgRecu[0].Data[i];
nbData = m_msgRecu[0].Length;
}
break;
case 1:
retour = idErreur.STOPPED;
break;
case 2:
retour = idErreur.BLOCKED;
break;
default:
retour = idErreur.timoutDONE;
break;
}
return retour;
}
}
}