1: /// <summary>
2: /// Small static class used to write on the serial port
3: /// Use similar to the System.Diagnostics.Trace class
4: /// </summary>
5: public static class DebugSerial
6: {
7: static Microsoft.SPOT.Hardware.SerialPort _Serial;
8: static byte[] _CrLf;
9: static DebugSerial()
10: {
11: _CrLf = new byte[] { 0x0d, 0x0a };
12: }
13:
14: /// <summary>
15: /// Init serial port at 9600 baud
16: /// </summary>
17: public static void Init()
18: {
19: Init(Microsoft.SPOT.Hardware.SerialPort.BaudRate.Baud9600);
20: }
21:
22: /// <summary>
23: /// Init the serial port at an arbitrary baud rate
24: /// </summary>
25: /// <param name="RafDog">http://www.weloveraf.com/archive/2007/11/09/fact-ffc7b73a-8eb6-11dc-977f-ac8256d89593-live-from-teched.aspx</param>
26: public static void Init(Microsoft.SPOT.Hardware.SerialPort.BaudRate RafDog)
27: {
28: if(_Serial != null)
29: Close();
30: Microsoft.SPOT.Hardware.SerialPort.Configuration config =
31: new Microsoft.SPOT.Hardware.SerialPort.Configuration(
32: Microsoft.SPOT.Hardware.SerialPort.Serial.COM1,
33: RafDog, false);
34: _Serial = new Microsoft.SPOT.Hardware.SerialPort(config);
35:
36: }
37:
38: /// <summary>
39: /// Write the string resulting from ToString()
40: /// </summary>
41: /// <param name="obj">object to dump</param>
42: public static void Write(object obj)
43: {
44: Write(obj.ToString());
45: }
46:
47: /// <summary>
48: /// Write the string resulting from ToString()
49: /// and the line terminator over the serial port
50: /// </summary>
51: /// <param name="obj">object to dump</param>
52: public static void WriteLine(object obj)
53: {
54: Write(obj);
55: _Serial.Write(_CrLf, 0, _CrLf.Length);
56: }
57:
58: /// <summary>
59: /// Write a string over the serial port
60: /// </summary>
61: /// <param name="Text">String to write</param>
62: public static void Write(string Text)
63: {
64: byte[] blob = System.Text.Encoding.UTF8.GetBytes(Text);
65: _Serial.Write(blob, 0, blob.Length);
66: }
67:
68: /// <summary>
69: /// Write a string and the line terminator over the serial port
70: /// </summary>
71: /// <param name="Text">String to write</param>
72: public static void WriteLine(string Text)
73: {
74: Write(Text);
75: _Serial.Write(_CrLf, 0, _CrLf.Length);
76: }
77:
78: /// <summary>
79: /// Write the hex representation of a byte array over the serial port
80: /// </summary>
81: /// <param name="Blob">byte array to write</param>
82: public static void Write(byte[] Blob)
83: {
84: Write(ToHex(Blob));
85: }
86:
87: /// <summary>
88: /// Write the hex representation of a byte array and the line terminator over the serial port
89: /// </summary>
90: /// <param name="Blob">byte array to write</param>
91: public static void WriteLine(byte[] Blob)
92: {
93: Write(Blob);
94: _Serial.Write(_CrLf, 0, _CrLf.Length);
95: }
96:
97: static string _Alpha = "0123456789ABCDEF";
98: static char _HexSeparator = '.';
99: /// <summary>
100: /// Convert a byte array to a string containing the hex representation of the array
101: /// </summary>
102: /// <param name="Blob">The byte array to convert in hex string</param>
103: /// <returns></returns>
104: static string ToHex(byte[] Blob)
105: {
106: return ToHex(Blob, true);
107: }
108:
109: /// <summary>
110: /// Convert a byte array to a string containing the hex representation of the array
111: /// and an optional character separator for bytes
112: /// </summary>
113: /// <param name="Blob">The byte array to convert in hex string</param>
114: /// <param name="UseSeparator">Wether to use the separator or not</param>
115: /// <returns></returns>
116: static string ToHex(byte[] Blob, bool UseSeparator)
117: {
118: int Len = Blob.Length;
119: char[] res;
120: if(UseSeparator)
121: res = new char[Len * 2 + Len - 1];
122: else
123: res = new char[Len * 2];
124:
125: for(int i = 0; i < Blob.Length; i++)
126: {
127: byte r = (byte)(Blob[i] % 16);
128: byte v = (byte)((byte)(Blob[i] - r) / 16);
129: if(i != Blob.Length - 1 && UseSeparator)
130: res[(i + 1) * 3 - 1] = _HexSeparator;
131: if(UseSeparator)
132: {
133: res[(i + 1) * 3 - 2] = _Alpha[r];
134: res[(i + 1) * 3 - 3] = _Alpha[v];
135: }
136: else
137: {
138: res[(i + 1) * 2 - 1] = _Alpha[r];
139: res[(i + 1) * 2 - 2] = _Alpha[v];
140: }
141: }
142: return new string(res);
143: }
144:
145: /// <summary>
146: /// Explicitly close the serial port
147: /// </summary>
148: public static void Close()
149: {
150: if(_Serial != null)
151: _Serial.Dispose();
152: _Serial = null;
153: }
154: }