Skip to content

Commit

Permalink
BugFix: Oprava parsování paketů z příchozích dat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Papouchcom committed Sep 11, 2023
1 parent bdf7429 commit b76f69a
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 68 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
4 changes: 2 additions & 2 deletions Papouch.Comunication/Papouch.Comunication.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Papouch.Comunication</RootNamespace>
<AssemblyName>Papouch.Comunication</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand Down
6 changes: 4 additions & 2 deletions Papouch.Spinel/Papouch.Spinel.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Papouch.Quido</RootNamespace>
<AssemblyName>Papouch.Quido</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand All @@ -27,6 +27,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -35,6 +36,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Compile Include="Spinel.cs" />
Expand Down
39 changes: 7 additions & 32 deletions Papouch.Spinel/Spinel97.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static int GetSpinelPacket(ref byte[] data, int dataLength, out PacketSpi
if ((i + 8) < dataLength)
{
int len = data[i + 2] * 0x100 + data[i + 3] - 5;
if (i + len + 8 <= dataLength)
if ((i + len + 8 <= dataLength) && (data[i + 8 + len] == 0x0D))
{
packet = new PacketSpinel97();
packet.PRE = data[i];
Expand All @@ -35,8 +35,8 @@ public static int GetSpinelPacket(ref byte[] data, int dataLength, out PacketSpi

packet.SUM = data[i + 7 + len];
packet.CR = data[i + 8 + len];
return i + len + 8;

return i + len + 8; ;
}
}
return 0;
Expand All @@ -50,62 +50,37 @@ public static Boolean Receive(ref ICommunicationInterface ci, out PacketSpinel97
int index = 0;
int i;

int bytesToRead = 0;
int bytesToRead;
DateTime dt = DateTime.Now.AddMilliseconds(timeout); // maximální timeout pro příjem zprávy
while (DateTime.Now < dt)
{
System.Threading.Thread.Sleep(1);
bytesToRead = (int)ci.BytesToRead();
if (bytesToRead > 0) Debug.Print(bytesToRead.ToString());
// if (bytesToRead > 0) Debug.Print("Rx: " + bytesToRead.ToString());
if ((bytesToRead > 0) && (index + bytesToRead < 1024))
{
index += ci.Read(ref buffer, index, bytesToRead);
//Debug.Print(Papouch.Utils.PapUtils.ConvertBinToHex(buffer));
//Debug.Print(PapUtils.ConvertBinToHex(buffer));
i = GetSpinelPacket(ref buffer, index, out rxPacket); // testujme zda je již přijat kompletní paket
if ((rxPacket != null) && (i > 0))
{
//Debug.Print("packet");
// Debug.Print("OK: " + PapUtils.ConvertBinToHex(rxPacket.GetBin()));
return true;
}
}
}
//if (bytesToRead!=0)
//Debug.Print("timeout");
Debug.Print("Not parsed data: " + PapUtils.ConvertBinToHex(buffer));
return false;
}

public static Boolean SendAndReceive(ref ICommunicationInterface ci, ref PacketSpinel97 txPacket, out PacketSpinel97 rxPacket, int timeout = 500)
{
rxPacket = null;
byte[] txData = txPacket.GetBin();

ci.Write(txData, 0, txData.Length);
// ci.Write(txData);

return Receive(ref ci, out txPacket, timeout);

/* byte[] buffer = new byte[1024]; // přijímací buffer
int index = 0;
int i;
int bytesToRead = 0;
DateTime dt = DateTime.Now.AddMilliseconds(timeout); // maximální timeout pro příjem zprávy
while (DateTime.Now < dt)
{
System.Threading.Thread.Sleep(1);
bytesToRead = (int)ci.BytesToRead();
if ((bytesToRead > 0) && (index + bytesToRead < 1024))
{
index += ci.Read(ref buffer, index, bytesToRead);
i = GetSpinelPacket(ref buffer, index, out rxPacket); // testujme zda je již přijat kompletní paket
if ((rxPacket != null) && (i > 0))
{
return true;
}
}
}
return false;
*/
}
}
}
4 changes: 2 additions & 2 deletions Papouch.Utils/Papouch.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Papouch.Utils</RootNamespace>
<AssemblyName>Papouch.Utils</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand Down
62 changes: 53 additions & 9 deletions QuidoDemo_CS.NET/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System.IO.Ports;

using System.Threading;

using System.Linq;

namespace QuidoDemo
{
Expand Down Expand Up @@ -350,18 +350,62 @@ private void buttonGetInputs_Click(object sender, EventArgs e)
if (quido != null)
{
LogMsg("*** GetInputs ***");
KvidoTestDelays(null);



//new System.Threading.Timer(ee =>
//{

// bool[] inputs = null;
// if (quido.CmdGetInputs(out inputs))
// {
// string bits = string.Join("", inputs.Select(b => b ? "1" : "0"));
// LogMsg(bits);
// //for (int index = 0; index < inputs.Length; index++)
// //{
// // LogMsg("Input " + index.ToString() + " is " + ((inputs[index]) ? "ON" : "OFF"));
// //}
// System.Diagnostics.Debug.Print("---!!!!---");
// } else
// {
// LogMsg("Failed");
// System.Diagnostics.Debug.Print("------");
// }


//}, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(100));



bool[] inputs = null;
if (quido.CmdGetInputs(out inputs))
{
for (int index = 0; index < inputs.Length; index++)
{
LogMsg("Input " + index.ToString() + " is " + ((inputs[index]) ? "ON" : "OFF"));
}
}
}
}


private void KvidoTestDelays(object state)
{

bool[] inputs = null;
if (quido.CmdGetInputs(out inputs))
{
string bits = string.Join("", inputs.Select(b => b ? "1" : "0"));
// LogMsg(bits);
//for (int index = 0; index < inputs.Length; index++)
//{
// LogMsg("Input " + index.ToString() + " is " + ((inputs[index]) ? "ON" : "OFF"));
//}
// System.Diagnostics.Debug.Print("------");
}
else
{
LogMsg("Failed");
System.Diagnostics.Debug.Print("---Failed---");
}

System.Threading.Timer callbackTimer = new System.Threading.Timer(KvidoTestDelays, null, 100, Timeout.Infinite);

}

#endregion

#region Commands: Counters
Expand Down
26 changes: 13 additions & 13 deletions QuidoDemo_CS.NET/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions QuidoDemo_CS.NET/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions QuidoDemo_CS.NET/QuidoDemo_CS.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -15,7 +15,7 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
2 changes: 1 addition & 1 deletion QuidoDemo_CS.NET/app.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>

0 comments on commit b76f69a

Please sign in to comment.