Skip to content

Commit

Permalink
Refactor SerialPort.Read() #49
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaiw committed May 20, 2019
1 parent bb94a2b commit f653048
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.2</TargetFramework>
<PublishDir>bin\Debug\netcoreapp2.2\publish\</PublishDir>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<_IsPortable>false</_IsPortable>
</PropertyGroup>
Expand Down
71 changes: 37 additions & 34 deletions src/ZigBeeNet.Transport.SerialPort/ZigBeeSerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using ZigBeeNet.Transport;
using Serilog;
using System.Collections.Generic;

namespace ZigBeeNet.Tranport.SerialPort
{
Expand All @@ -15,7 +16,7 @@ public class ZigBeeSerialPort : IZigBeePort

private CancellationTokenSource _cancellationToken;

// private ManualResetEventSlim _readResetEvent;
// private ManualResetEventSlim _readResetEvent;

/// <summary>
/// The circular fifo queue for receive data
Expand All @@ -28,18 +29,18 @@ public class ZigBeeSerialPort : IZigBeePort
private int _end = 0;

/// <summary>
/// The receive buffer start pointer (where we take the data to pass to the application)
/// </summary>
/// The receive buffer start pointer (where we take the data to pass to the application)
/// </summary>
private int _start = 0;

/// <summary>
/// The length of the receive buffer
/// </summary>
/// The length of the receive buffer
/// </summary>
private int _maxLength = 512;

/// <summary>
/// Synchronisation object for buffer queue manipulation
/// </summary>
/// Synchronisation object for buffer queue manipulation
/// </summary>
private object _bufferSynchronisationObject = new object();

public string PortName { get; set; }
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit f653048

Please sign in to comment.