diff --git a/.idea/.idea.PMS5003/.idea/indexLayout.xml b/.idea/.idea.PMS5003/.idea/indexLayout.xml index 473077d..f5a863a 100644 --- a/.idea/.idea.PMS5003/.idea/indexLayout.xml +++ b/.idea/.idea.PMS5003/.idea/indexLayout.xml @@ -1,6 +1,6 @@ - + diff --git a/PMS5003.sln b/PMS5003.sln index a27fbb7..9ff94a9 100644 --- a/PMS5003.sln +++ b/PMS5003.sln @@ -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 diff --git a/PMS5003/Exceptions/BufferUnderflowExceptions.cs b/PMS5003/Exceptions/BufferUnderflowExceptions.cs deleted file mode 100644 index fa94bf2..0000000 --- a/PMS5003/Exceptions/BufferUnderflowExceptions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace PMS5003.Exceptions -{ - /// - /// BufferUnderflowExceptions is thrown when the PMS5003 data buffer is incomplete. - /// - [Serializable] - public class BufferUnderflowException : Exception - { - public BufferUnderflowException() : base("The PMS5003 data buffer is underrun, not enough bytes read!") - { - - } - } -} \ No newline at end of file diff --git a/PMS5003/PMS5003.cs b/PMS5003/PMS5003.cs index d1dc7cd..7cc4e8f 100644 --- a/PMS5003/PMS5003.cs +++ b/PMS5003/PMS5003.cs @@ -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) diff --git a/PMS5003/PMS5003.csproj b/PMS5003/PMS5003.csproj index dcbb449..1317e8e 100644 --- a/PMS5003/PMS5003.csproj +++ b/PMS5003/PMS5003.csproj @@ -1,10 +1,10 @@ - Exe + Library netcoreapp3.1 PMS5003 - 1.0.4 + 1.1.0 Denis Nutiu NucuLabs.dev C# Library for interfacing with the PMS5003 Particulate Matter Sensor. diff --git a/PMS5003/Pms5003Data.cs b/PMS5003/Pms5003Data.cs index 4333967..fcbf082 100644 --- a/PMS5003/Pms5003Data.cs +++ b/PMS5003/Pms5003Data.cs @@ -37,12 +37,7 @@ namespace PMS5003 public static Pms5003Data FromBytes(byte[] buffer) { 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]); diff --git a/readme.md b/readme.md index aaa44e5..e24bcf0 100644 --- a/readme.md +++ b/readme.md @@ -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