-
Notifications
You must be signed in to change notification settings - Fork 5
/
XModem.cs
417 lines (333 loc) · 9.48 KB
/
XModem.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
using System.Text;
using System.Threading;
namespace XModem
{
////////////////////////////////////
// Created by Andreas Wögerbauer //
// http://net.trackday.cc //
// //
// Thanks to: //
// Dave Kubasiak //
////////////////////////////////////
//Version 1.3
//This class is tested to be 100% compatibel to Microsoft(r) Hyperterminal 5.1
//XmodemTransmit: supports both CRC and checksum
//XModemReceive: only supports checksum
//Feel free to use/modify/copy/distribute this file
public class XModem
{
#region Events
public event System.EventHandler PacketReceived;
public event System.EventHandler PacketSent;
#endregion
#region Members
System.IO.Ports.SerialPort port = null;
private byte SOH = 0x01; //header for 128byte-pakets
private byte STX = 0x02; //header for 1024byte-pakets
private byte EOT = 0x04; //end of transmission
private byte ACK = 0x06; //acknowledge
private byte NAK = 0x15; //negativ acknowledge
private byte CAN = 0x18; //cancel transfer
private byte CTRLZ = 0x1A; //padding char to fill data blocks < buffer size
private byte C = 0x43; //start of a CRC request
private ushort MAXRETRANS = 25;
//1024 for XModem 1k + 3 head chars + 2 crc + nul
private byte[] xbuff = new byte[1030];
private int bufsz, crc = -1;
private byte packetno = 1;
private int i, c, len = 0;
private int retry;
#endregion
#region Constructor
public XModem(System.IO.Ports.SerialPort port)
{
this.port = port;
}
#endregion
#region Implementation
#region XModemReceive
///////////////////////////
//receive byte Array via XModem using either Checksum or CRC error detection
/// ///////////////////////
public byte[] XModemReceive(bool useCRC)
{
//since we don't know how many bytes we receive it's the
//best solution to use a MemoryStream
System.IO.MemoryStream buf = new System.IO.MemoryStream();
int packetno = 1;
int retry = 0;
int i, c;
double kb = 0;
if (useCRC) // send and use CRC
this.writeByte(C);
else //send and don't use CRC
this.writeByte(NAK);
while (retry < 16)
{
try { c = port.ReadByte(); }
catch { c = 0x00; }
if (c == SOH || c == STX)
{
#region SOH/STX
retry = 0;
bufsz = 128; // default buffer size
if (c == STX)
bufsz = 1024; // buffer size for Xmodem1K
#region fill packet with datastream
xbuff[0] = (byte)c;
for (i = 0; i < (bufsz + (useCRC ? 1 : 0) + 3); i++)
{
xbuff[i + 1] = (byte)this.port.ReadByte();
}
#endregion
if (xbuff[1] == (byte)(~xbuff[2]) && xbuff[1] == packetno && check(useCRC, xbuff, 3, bufsz))
{
//add buffer to memory stream
buf.Write(xbuff, 3, bufsz);
this.writeByte(ACK);
#region fire event & increment packet number
//128 or 1024 Byte per package
double d = bufsz;
d = d / 1024;
kb = (kb + d);
if (this.PacketReceived != null)
PacketReceived(this, null);
packetno++;
if (packetno >= 256)
packetno = 0;
#endregion
}
else
{
this.writeByte(NAK);
}
#endregion
}
else if (c == EOT)
{
#region EOT
port.DiscardInBuffer();
this.writeByte(ACK);
//convert from memory stream to byte[]
return buf.ToArray();
#endregion
}
else if (c == CAN)
{
#region CAN
if (port.ReadByte() == CAN)
{
port.DiscardInBuffer();
this.writeByte(ACK);
return null; //canceled by remote
}
#endregion
}
else
{
retry++;
port.DiscardInBuffer();
this.writeByte(NAK);
}
}//while()
//timeout
this.writeByte(CAN);
this.writeByte(CAN);
this.writeByte(CAN);
port.DiscardInBuffer();
return null;
}
private bool check(bool isCRC, byte[] buf, int index, int sz)
{
if (isCRC) // use CRC checking
{
ushort crc = CRC16.CRC16_ccitt(buf, index, sz);
ushort tcrc = (ushort)((buf[sz + index] << 8) + buf[sz + index + 1]);
if (crc == tcrc)
return true;
}
else { // use Checksum checking
int i;
byte cks = 0;
for (i = 0; i < sz; ++i)
{
cks += buf[i + index];
}
if (cks == buf[sz + index])
return true;
}
return false;
}
#endregion
#region XmodemTransmit
public int XmodemTransmit(byte[] src, int srcsz, bool use1K)
{
for (retry = 0; retry < 16; ++retry)
{
c = port.ReadByte();
if (c >= 0)
{
if (c == 'C')
{
crc = 1;
return this.startTrans(src, srcsz, use1K);
}
else if (c == NAK)
{
crc = 0;
return this.startTrans(src, srcsz, use1K);
}
else if (c == CAN)
{
#region cancled by remote
if ((c = port.ReadByte()) == CAN)
{
this.writeByte(ACK);
port.DiscardInBuffer();
return -1;
}
#endregion
}
}//if
}//for - retry
this.writeByte(CAN);
this.writeByte(CAN);
this.writeByte(CAN);
port.DiscardInBuffer();
return -2; //no sync
}
#endregion
#region startTrans
private int startTrans(byte[] src, int srcsz, bool use1K)
{
while (true)
{
bufsz = use1K ? 1024 : 128;
xbuff[0] = use1K ? STX : SOH;
xbuff[1] = packetno;
xbuff[2] = (byte)(~packetno);
c = srcsz - len; //len = data already sent
if (c > bufsz) c = bufsz;
if (c > 0)
{
//clear buffer
xbuff = memset(xbuff, 3, CTRLZ, bufsz);
#region fill xbuff with data
xbuff = memcpy(xbuff, 3, src, len, c);
#endregion
#region create CRC / Checksum
if (crc == 1)
{
ushort ccrc = CRC16.CRC16_ccitt(xbuff, 3, bufsz);
xbuff[bufsz + 3] = (byte)((ccrc >> 8) & 0xFF);
xbuff[bufsz + 4] = (byte)(ccrc & 0xFF);
}
else
{
byte ccks = 0;
for (i = 3; i < bufsz + 3; ++i)
{
ccks += xbuff[i];
}
xbuff[bufsz + 3] = ccks;
}
#endregion
retry = 0;
bool success = false;
while (retry < MAXRETRANS && !success)
{
retry++;
#region write xbuff out on port
writeBuffer(xbuff, bufsz + 4 + crc);
#endregion
c = port.ReadByte();
if (c >= 0)
{
if (c == ACK)
{
if (this.PacketSent != null)
PacketSent(this, null);
++packetno;
len += bufsz;
success = true; //go ahead in transmition
}
if (c == CAN)
{
if ((c = port.ReadByte()) == CAN)
{
this.writeByte(ACK);
port.DiscardInBuffer();
return -1; //canceled by remote
}
}
if (c == NAK)
{
//do nothing
}
}
}//while
if (!success)
{
#region xmit error
this.writeByte(CAN);
this.writeByte(CAN);
this.writeByte(CAN);
port.DiscardInBuffer();
return -4;
#endregion
}
}
else
{
#region transfer completed
for (retry = 0; retry < 10; ++retry)
{
this.writeByte(EOT);
//avoid problem with to short timeout on port
Thread.Sleep(500);
c = port.ReadByte();
if (c == ACK)
break;
}
//port.DiscardInBuffer();
return (c == ACK) ? len : -5;
#endregion
}
}//while
}
#endregion
#endregion
#region a few really cool C-style functions ;)
private void writeBuffer(byte[] xbuff, int size)
{
this.port.Write(xbuff, 0, size);
}
//writes one single byte
private void writeByte(byte b)
{
byte[] buffer = new byte[1];
buffer[0] = b;
this.port.Write(buffer, 0, 1);
}
//sets the first num bytes pointed by buffer to the value specified by c parameter.
byte[] memset(byte[] xbuff, int index, byte c, int num)
{
for (int i = 0; i < num; i++)
{
xbuff[i + index] = c;
}
return xbuff;
}
//copies num bytes from src buffer to memory location pointed by dest.
byte[] memcpy(byte[] dest, int d_i, byte[] src, int d_s, int num)
{
for (int i = 0; i < num; i++)
{
dest[i + d_i] = src[i + d_s];
}
return dest;
}
#endregion
}
}