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
|
using System;
using System.Collections.Generic;
using System.IO.Ports;
namespace QuickRoute.GPSDeviceReaders.SerialPortDeviceReader
{
public static class SerialPortUtil
{
public static String[] GetLatestPortsList()
{
var portNames = SerialPort.GetPortNames();
Array.Sort(portNames, new AlphanumComparator());
return portNames;
}
}
public class SerialPortSession : IDisposable
{
public SerialPort Port { get; private set; }
public SerialPortSession(SerialPort serialPort)
{
Port = serialPort;
if (!Port.IsOpen)
{
Port.Open();
}
}
public void Dispose()
{
Port.Close();
}
}
public class AlphanumComparator : IComparer<String>
{
public int Compare(String s1, String s2)
{
if (s1 == null)
{
return 0;
}
if (s2 == null)
{
return 0;
}
int len1 = s1.Length;
int len2 = s2.Length;
int marker1 = 0;
int marker2 = 0;
// Walk through two the strings with two markers.
while (marker1 < len1 && marker2 < len2)
{
char ch1 = s1[marker1];
char ch2 = s2[marker2];
// Some buffers we can build up characters in for each chunk.
var space1 = new char[len1];
int loc1 = 0;
var space2 = new char[len2];
int loc2 = 0;
// Walk through all following characters that are digits or
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do
{
space1[loc1++] = ch1;
marker1++;
if (marker1 < len1)
{
ch1 = s1[marker1];
}
else
{
break;
}
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
do
{
space2[loc2++] = ch2;
marker2++;
if (marker2 < len2)
{
ch2 = s2[marker2];
}
else
{
break;
}
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
// If we have collected numbers, compare them numerically.
// Otherwise, if we have strings, compare them alphabetically.
var str1 = new string(space1);
var str2 = new string(space2);
int result;
if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
{
int thisNumericChunk = int.Parse(str1);
int thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
}
if (result != 0)
{
return result;
}
}
return len1 - len2;
}
}
public static class HexUtils
{
public static byte[] GetBytes(Int16 value)
{
var valueBytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(valueBytes);
}
return valueBytes;
}
public static byte[] GetBytes(Int32 value)
{
byte[] valueBytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(valueBytes);
}
return valueBytes;
}
public static Int16 ToInt16(byte[] buffer, int offset)
{
if (BitConverter.IsLittleEndian)
{
var copy = new byte[2];
Array.Copy(buffer, offset, copy, 0, 2);
Array.Reverse(copy);
return BitConverter.ToInt16(copy, 0);
}
return BitConverter.ToInt16(buffer, offset);
}
public static Int32 ToInt32(byte[] buffer, int offset)
{
if (BitConverter.IsLittleEndian)
{
var copy = new byte[4];
Array.Copy(buffer, offset, copy, 0, 4);
Array.Reverse(copy);
return BitConverter.ToInt32(copy, 0);
}
return BitConverter.ToInt32(buffer, offset);
}
public static Int64 ToInt64(byte[] buffer, int offset)
{
if (BitConverter.IsLittleEndian)
{
var copy = new byte[8];
Array.Copy(buffer, offset, copy, 0, 8);
Array.Reverse(copy);
return BitConverter.ToInt64(copy, 0);
}
return BitConverter.ToInt64(buffer, offset);
}
public static byte CheckSum(byte[] bytes, int offset, int length)
{
byte checkSum = 0;
for (var i = offset; i < offset + length; i++)
{
checkSum ^= bytes[i];
}
return checkSum;
}
public static byte[] GetBytesLE(Int16 value)
{
var valueBytes = BitConverter.GetBytes(value);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(valueBytes);
}
return valueBytes;
}
public static byte[] GetBytesLE(Int32 value)
{
byte[] valueBytes = BitConverter.GetBytes(value);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(valueBytes);
}
return valueBytes;
}
public static Int16 ToInt16LE(byte[] buffer, int offset)
{
if (!BitConverter.IsLittleEndian)
{
var copy = new byte[2];
Array.Copy(buffer, offset, copy, 0, 2);
Array.Reverse(copy);
return BitConverter.ToInt16(copy, 0);
}
return BitConverter.ToInt16(buffer, offset);
}
public static Int32 ToInt32LE(byte[] buffer, int offset)
{
if (!BitConverter.IsLittleEndian)
{
var copy = new byte[4];
Array.Copy(buffer, offset, copy, 0, 4);
Array.Reverse(copy);
return BitConverter.ToInt32(copy, 0);
}
return BitConverter.ToInt32(buffer, offset);
}
public static Int64 ToInt64LE(byte[] buffer, int offset)
{
if (!BitConverter.IsLittleEndian)
{
var copy = new byte[8];
Array.Copy(buffer, offset, copy, 0, 8);
Array.Reverse(copy);
return BitConverter.ToInt64(copy, 0);
}
return BitConverter.ToInt64(buffer, offset);
}
}
}
|