Fix library: Start search from start frame

This commit is contained in:
Denis-Cosmin Nutiu 2022-12-21 18:19:00 +02:00
parent ae62eff060
commit e46fda594d
7 changed files with 28 additions and 29 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelUserStore">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />

View file

@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMS5003", "PMS5003\PMS5003.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMS5003Tests", "PMS5003Tests\PMS5003Tests.csproj", "{AF7541BD-C0B1-42FE-AEFB-1A1D9CEAC6B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{92DEDAA4-6A42-4EC5-A534-B1985608BB98}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -18,5 +20,9 @@ Global
{AF7541BD-C0B1-42FE-AEFB-1A1D9CEAC6B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF7541BD-C0B1-42FE-AEFB-1A1D9CEAC6B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF7541BD-C0B1-42FE-AEFB-1A1D9CEAC6B9}.Release|Any CPU.Build.0 = Release|Any CPU
{92DEDAA4-6A42-4EC5-A534-B1985608BB98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92DEDAA4-6A42-4EC5-A534-B1985608BB98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DEDAA4-6A42-4EC5-A534-B1985608BB98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DEDAA4-6A42-4EC5-A534-B1985608BB98}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View file

@ -1,16 +0,0 @@
using System;
namespace PMS5003.Exceptions
{
/// <summary>
/// BufferUnderflowExceptions is thrown when the PMS5003 data buffer is incomplete.
/// </summary>
[Serializable]
public class BufferUnderflowException : Exception
{
public BufferUnderflowException() : base("The PMS5003 data buffer is underrun, not enough bytes read!")
{
}
}
}

View file

@ -79,14 +79,28 @@ namespace PMS5003
public Pms5003Data ReadData()
{
var currentTry = 0;
const int maxRetries = 5;
const int maxRetries = 50;
var buffer = new byte[32];
while (currentTry < maxRetries)
{
try
{
var buffer = new byte[32];
_serialPort.Read(buffer, 0, 32);
// Try to find start frame
_serialPort.Read(buffer, 0, 1);
if (buffer[0] != Pms5003Constants.StartByte1)
{
continue;
}
_serialPort.Read(buffer, 1, 1);
if (buffer[1] != Pms5003Constants.StartByte2)
{
continue;
}
// Read rest of data
_serialPort.Read(buffer, 2, 30);
return Pms5003Data.FromBytes(buffer);
}
catch (Exception e)

View file

@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>PMS5003</PackageId>
<Version>1.0.4</Version>
<Version>1.1.0</Version>
<Authors>Denis Nutiu</Authors>
<Company>NucuLabs.dev</Company>
<Description>C# Library for interfacing with the PMS5003 Particulate Matter Sensor.</Description>

View file

@ -38,11 +38,6 @@ namespace PMS5003
{
var pms5003Measurement = new Pms5003Data();
if (buffer.Length < 4)
{
throw new BufferUnderflowException();
}
if (buffer[0] != Pms5003Constants.StartByte1 || buffer[1] != Pms5003Constants.StartByte2)
{
throw new InvalidStartByteException(buffer[0], buffer[1]);

View file

@ -16,7 +16,7 @@ namespace Application
{
static void Main(string[] args)
{
var pms = new Pms5003();
var pms = new Pms5003("COM3", -1, -1);
while (true)
{
try