diff --git a/samples/ZigBeeNet.PlayGround/Properties/PublishProfiles/FolderProfile.pubxml b/samples/ZigBeeNet.PlayGround/Properties/PublishProfiles/FolderProfile.pubxml index 18dab19b..7e495fa0 100644 --- a/samples/ZigBeeNet.PlayGround/Properties/PublishProfiles/FolderProfile.pubxml +++ b/samples/ZigBeeNet.PlayGround/Properties/PublishProfiles/FolderProfile.pubxml @@ -9,7 +9,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Any CPU netcoreapp2.2 bin\Debug\netcoreapp2.2\publish\ - linux-x64 + win-x64 true <_IsPortable>false diff --git a/src/ZigBeeNet.Transport.SerialPort/ZigBeeSerialPort.cs b/src/ZigBeeNet.Transport.SerialPort/ZigBeeSerialPort.cs index f269e714..b7dd2422 100644 --- a/src/ZigBeeNet.Transport.SerialPort/ZigBeeSerialPort.cs +++ b/src/ZigBeeNet.Transport.SerialPort/ZigBeeSerialPort.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using ZigBeeNet.Transport; using Serilog; +using System.Collections.Generic; namespace ZigBeeNet.Tranport.SerialPort { @@ -15,7 +16,7 @@ public class ZigBeeSerialPort : IZigBeePort private CancellationTokenSource _cancellationToken; - // private ManualResetEventSlim _readResetEvent; + // private ManualResetEventSlim _readResetEvent; /// /// The circular fifo queue for receive data @@ -28,18 +29,18 @@ public class ZigBeeSerialPort : IZigBeePort private int _end = 0; /// - /// The receive buffer start pointer (where we take the data to pass to the application) - /// + /// The receive buffer start pointer (where we take the data to pass to the application) + /// private int _start = 0; /// - /// The length of the receive buffer - /// + /// The length of the receive buffer + /// private int _maxLength = 512; /// - /// Synchronisation object for buffer queue manipulation - /// + /// Synchronisation object for buffer queue manipulation + /// private object _bufferSynchronisationObject = new object(); public string PortName { get; set; } @@ -214,43 +215,45 @@ private void ReaderTask() { while (IsOpen && _cancellationToken.IsCancellationRequested == false) { - int length = _serialPort.BytesToRead; - - var message = new byte[length]; - var bytesRead = 0; - var bytesToRead = length; - try { - if (length > 0) + /* This will block until at least one byte is available */ + byte b = Convert.ToByte(_serialPort.ReadByte()); + + /* Read the rest of the data but do not forget the byte above while fill the buffer */ + int length = _serialPort.BytesToRead; + var message = new byte[length + 1]; + message[0] = b; + var bytesRead = 0; + var bytesToRead = length; + + do { + var n = _serialPort.Read(message, bytesRead + 1, length - bytesRead); // read may return anything from 0 - length , 0 = end of stream + if (n == 0) break; + bytesRead += n; + bytesToRead -= n; + } while (bytesToRead > 0); - do - { - var n = _serialPort.Read(message, bytesRead, length - bytesRead); // read may return anything from 0 - length , 0 = end of stream - if (n == 0) break; - bytesRead += n; - bytesToRead -= n; - } while (bytesToRead > 0); - lock (_bufferSynchronisationObject) + lock (_bufferSynchronisationObject) + { + //Array.Copy(message, _buffer, message.Length); + foreach (byte recv in message) { - //Array.Copy(message, _buffer, message.Length); - foreach (byte recv in message) + _buffer[_end++] = recv; + if (_end >= _maxLength) { - _buffer[_end++] = recv; - if (_end >= _maxLength) - { - _end = 0; - } + _end = 0; } } - - //lock (_serialPort) - //{ - // _readResetEvent.Set(); - //} } + + //lock (_serialPort) + //{ + // _readResetEvent.Set(); + //} + //} } catch (Exception e) {