fixed and optimized port data processing
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
<ProjectGuid>{47A399D3-30C1-4EF4-9E94-F3518919A59C}</ProjectGuid>
|
<ProjectGuid>{47A399D3-30C1-4EF4-9E94-F3518919A59C}</ProjectGuid>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<RootNamespace>Gidrolock_Modbus_Configurator</RootNamespace>
|
<RootNamespace>Gidrolock_Modbus_Scanner</RootNamespace>
|
||||||
<AssemblyName>Gidrolock Modbus Configurator</AssemblyName>
|
<AssemblyName>Gidrolock Modbus Configurator</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
|
|||||||
45
Modbus.cs
45
Modbus.cs
@@ -14,7 +14,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
public static class Modbus
|
public static class Modbus
|
||||||
{
|
{
|
||||||
public static SerialPort port = new SerialPort();
|
public static SerialPort port = new SerialPort();
|
||||||
|
|
||||||
public static event EventHandler<ModbusResponseEventArgs> ResponseReceived = delegate { };
|
public static event EventHandler<ModbusResponseEventArgs> ResponseReceived = delegate { };
|
||||||
|
|
||||||
public static void Init()
|
public static void Init()
|
||||||
@@ -276,38 +276,34 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
static int count = 0;
|
static int count = 0;
|
||||||
static void PortDataReceived(object sender, EventArgs e)
|
static void PortDataReceived(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
//reset values on every event call;
|
//reset values on every event call;
|
||||||
buffer = new byte[255];
|
buffer = new byte[255];
|
||||||
offset = 0;
|
offset = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//reset values on every event call;
|
stopwatch.Restart();
|
||||||
buffer = new byte[255];
|
while (stopwatch.ElapsedMilliseconds < 20)
|
||||||
offset = 0;
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
stopwatch.Restart();
|
if (port.BytesToRead > 0)
|
||||||
while (stopwatch.ElapsedMilliseconds < 20)
|
|
||||||
{
|
{
|
||||||
if (port.BytesToRead > 0)
|
stopwatch.Restart();
|
||||||
{
|
count = port.BytesToRead;
|
||||||
stopwatch.Restart();
|
port.Read(buffer, offset, port.BytesToRead);
|
||||||
count = port.BytesToRead;
|
offset += count;
|
||||||
port.Read(buffer, offset, port.BytesToRead);
|
|
||||||
offset += count;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// assume that the message ended
|
}
|
||||||
|
// assume that the message ended
|
||||||
|
|
||||||
List<byte> message = new List<byte>();
|
List<byte> message = new List<byte>();
|
||||||
for (int i = 0; i < offset; i++)
|
for (int i = 0; i < offset; i++)
|
||||||
{
|
{
|
||||||
message.Add(buffer[i]);
|
message.Add(buffer[i]);
|
||||||
}
|
}
|
||||||
if (message.Count == 0)
|
if (message.Count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Console.WriteLine("Incoming message: " + ByteArrayToString(message.ToArray(), false));
|
Console.WriteLine("Incoming message: " + ByteArrayToString(message.ToArray(), false));
|
||||||
/*
|
/*
|
||||||
if (!CheckResponse(message.ToArray()))
|
if (!CheckResponse(message.ToArray()))
|
||||||
{
|
{
|
||||||
@@ -342,7 +338,6 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ModbusResponseEventArgs : EventArgs
|
public class ModbusResponseEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public byte[] Message { get; set; }
|
public byte[] Message { get; set; }
|
||||||
@@ -362,7 +357,7 @@ namespace Gidrolock_Modbus_Scanner
|
|||||||
}
|
}
|
||||||
//Console.WriteLine("Read data: " + Modbus.ByteArrayToString(Data, false));
|
//Console.WriteLine("Read data: " + Modbus.ByteArrayToString(Data, false));
|
||||||
}
|
}
|
||||||
else Data = new byte[1] {0x0F};
|
else Data = new byte[1] { 0x0F };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
Properties/Resources.Designer.cs
generated
4
Properties/Resources.Designer.cs
generated
@@ -8,7 +8,7 @@
|
|||||||
// </auto-generated>
|
// </auto-generated>
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
namespace Gidrolock_Modbus_Configurator.Properties {
|
namespace Gidrolock_Modbus_Scanner.Properties {
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ namespace Gidrolock_Modbus_Configurator.Properties {
|
|||||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||||
get {
|
get {
|
||||||
if (object.ReferenceEquals(resourceMan, null)) {
|
if (object.ReferenceEquals(resourceMan, null)) {
|
||||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Gidrolock_Modbus_Configurator.Properties.Resources", typeof(Resources).Assembly);
|
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Gidrolock_Modbus_Scanner.Properties.Resources", typeof(Resources).Assembly);
|
||||||
resourceMan = temp;
|
resourceMan = temp;
|
||||||
}
|
}
|
||||||
return resourceMan;
|
return resourceMan;
|
||||||
|
|||||||
2
Properties/Settings.Designer.cs
generated
2
Properties/Settings.Designer.cs
generated
@@ -8,7 +8,7 @@
|
|||||||
// </auto-generated>
|
// </auto-generated>
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
namespace Gidrolock_Modbus_Configurator.Properties {
|
namespace Gidrolock_Modbus_Scanner.Properties {
|
||||||
|
|
||||||
|
|
||||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||||
|
|||||||
Reference in New Issue
Block a user