public SerialTransport(SerialTransportAddress sta, int baudRate) { this.sta = sta; this.port = CreateSerialPort(sta, baudRate); #if MicroFramework SendBuffer = new Dictionary(); #else SendBuffer = new Dictionary <byte, SafeData>(); #endif headerACK = new SafeSerialHeader(); }
public virtual void Send(TransportAddress t, byte[] data) { byte[] tmp; SerialHeader header; if (data.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException("there are too many bytes to transmit"); } // Check if this message is going to use Acknowledgements if (((SerialTransportAddress)t).forceACK) { header = new SafeSerialHeader(); } else { header = new SerialHeader(); } header.length = (ushort)data.Length; if (header is SafeSerialHeader) { ((SafeSerialHeader)header).sequence = GetSequence(); } tmp = new byte[header.size + data.Length]; header.GetBytes().CopyTo(tmp, 0); data.CopyTo(tmp, header.size); header.CRC = SerialCRC.Calculate(tmp, (uint)tmp.Length); header.GetBytes().CopyTo(tmp, 0); if (header is SafeSerialHeader) { lock (SendBuffer) { SendBuffer.Add(sequence, new SafeData(tmp)); } } try { WriteToPort(tmp, 0, tmp.Length, header is SafeSerialHeader); } catch (Exception e) { throw e; } }
public virtual void Receive() { int n; byte[] buffer = new byte[ushort.MaxValue]; // Max UDP Datagram SerialHeader sh; while (!end) { try { // Receive magic byte n = 0; do { n += port.Read(buffer, 0, 1); } while (n < 1); if (buffer[0] == (byte)MagicByte.SAFE) { sh = new SafeSerialHeader(); } else if (buffer[0] == (byte)MagicByte.FAST) { sh = new SerialHeader(); } else { continue; } // Receive the header do { n += port.Read(buffer, n, sh.size - n); } while (n < sh.size); if (!sh.Populate(buffer, 1)) { continue; } // Receive and process the data n = 0; do { n += port.Read(buffer, sh.size + n, sh.length - n); } while (n < sh.length); if (sh.CRC == SerialCRC.Calculate(buffer, (uint)(sh.length + sh.size))) { ProcessPacket(sh, buffer, sh.size); } else { } } catch (ThreadAbortException) { //It's safe to ignore this :) } catch (Exception e) { throw e; } } }
public virtual void Send(TransportAddress t, byte[] data) { byte[] tmp; SerialHeader header; if (data.Length > ushort.MaxValue) throw new ArgumentOutOfRangeException("there are too many bytes to transmit"); // Check if this message is going to use Acknowledgements if (((SerialTransportAddress)t).forceACK) { header = new SafeSerialHeader(); } else { header = new SerialHeader(); } header.length = (ushort)data.Length; if (header is SafeSerialHeader) { ((SafeSerialHeader)header).sequence = GetSequence(); } tmp = new byte[header.size + data.Length]; header.GetBytes().CopyTo(tmp, 0); data.CopyTo(tmp, header.size); header.CRC = SerialCRC.Calculate(tmp, (uint)tmp.Length); header.GetBytes().CopyTo(tmp, 0); if (header is SafeSerialHeader) { lock (SendBuffer) { SendBuffer.Add(sequence, new SafeData(tmp)); } } try { WriteToPort(tmp, 0, tmp.Length, header is SafeSerialHeader); } catch (Exception e) { throw e; } }
public SerialTransport(SerialTransportAddress sta, int baudRate) { this.sta = sta; this.port = CreateSerialPort(sta, baudRate); #if MicroFramework SendBuffer = new Dictionary(); #else SendBuffer = new Dictionary<byte, SafeData>(); #endif headerACK = new SafeSerialHeader(); }