forked from stlk/XBee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiFrame.cs
More file actions
62 lines (52 loc) · 1.67 KB
/
ApiFrame.cs
File metadata and controls
62 lines (52 loc) · 1.67 KB
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
// Copyright (c) 2009 http://grommet.codeplex.com
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using System.IO;
using System;
namespace STLK
{
public class ApiFrame
{
public const byte StartDelimiter = 0x7E;
public byte Retries { get; set; }
public DeliveryStatus DeliveryStatus { get; set; }
public DiscoveryStatus DiscoveryStatus { get; set; }
public XBeeCallback Callback { get; set; }
public ushort Length
{
get { return (ushort)FrameData.Length; }
}
public byte[] FrameData { get; set; }
public byte Checksum
{
get
{
byte checksum = 0xFF;
for (int i = 0; i < FrameData.Length; i++)
{
checksum -= FrameData[i];
}
return checksum;
}
}
public byte[] Serialize()
{
MemoryStream stream = new MemoryStream();
stream.WriteByte(StartDelimiter);
byte[] lengthBuffer = GetBytes(Length);
stream.Write(lengthBuffer, 0, lengthBuffer.Length);
stream.Write(FrameData, 0, Length);
stream.WriteByte(Checksum);
stream.Close();
return stream.ToArray();
}
public static byte[] GetBytes(ushort value)
{
byte[] buffer = new byte[2];
buffer[0] = (byte)(value >> 8);
buffer[1] = (byte)value;
return buffer;
}
}
}