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
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;
namespace QuickRoute.GPSDeviceReaders.SerialPortDeviceReader
{
class SerialPortCommandError : Exception
{
public SerialPortCommandError() { }
public SerialPortCommandError(string message) : base(message) { }
public SerialPortCommandError(string message, Exception e) : base(message, e) { }
}
abstract public class SerialPortCommand
{
protected static readonly ASCIIEncoding ASCIIEnc = new ASCIIEncoding();
public static int DefaultMaxBufferSize = 1024 * 1000;
public static int DefaultSleepTime = 300;
private readonly List<byte> _buffer = new List<byte>();
private readonly int _maxBufferSize = DefaultMaxBufferSize;
private readonly int _sleepTime = DefaultSleepTime;
protected SerialPortCommand()
: this(DefaultSleepTime, DefaultMaxBufferSize)
{
}
protected SerialPortCommand(int sleepTime)
: this(sleepTime, DefaultMaxBufferSize)
{
}
protected SerialPortCommand(int sleepTime, int maxBufferSize)
{
_sleepTime = Math.Abs(sleepTime);
_maxBufferSize = maxBufferSize;
}
public void Execute(SerialPort commport)
{
if (!commport.IsOpen)
{
throw new SerialPortCommandError("Port not opened!");
}
Write(commport);
Thread.Sleep(_sleepTime);
Read(commport);
ProcessBuffer(_buffer.ToArray());
_buffer.Clear();
}
private void Write(SerialPort commport)
{
byte[] commandBuffer = GetCommandBuffer();
commport.DiscardInBuffer();
commport.DiscardOutBuffer();
commport.Write(commandBuffer, 0, commandBuffer.Length);
}
abstract protected byte[] GetCommandBuffer();
private void Read(SerialPort commport)
{
while (commport.BytesToRead > 0 && _buffer.Count <= _maxBufferSize) {
_buffer.Add((byte)commport.ReadByte());
}
}
virtual protected void ProcessBuffer(byte[] buffer)
{
// Look in the byte array for useful information
}
public static bool CompareFirstN(byte[] buf1, byte[] buf2, int n)
{
int len = Math.Abs(n);
if (buf1 == null || buf2 == null || buf1.Length < len || buf2.Length < len)
{
return false;
}
for (int i = 0; i < len; i++)
{
if (buf1[i] != buf2[i])
{
return false;
}
}
return true;
}
public static bool CompareLastN(byte[] buf1, byte[] buf2, int n)
{
int len = Math.Abs(n);
if (buf1 == null || buf2 == null || buf1.Length < len || buf2.Length < len)
{
return false;
}
for (int i = 0; i < len; i++)
{
if (buf1[buf1.Length - 1 - i] != buf2[buf2.Length - 1 - i])
{
return false;
}
}
return true;
}
}
}
|