Skip to content

Commit

Permalink
Merge pull request #21 from Stira-sa/ErrorHandling
Browse files Browse the repository at this point in the history
Error handling
  • Loading branch information
Touseefelahi authored Oct 16, 2022
2 parents 3f53cdd + d99bf2c commit 1e89038
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions GenICam/Enums/RegisterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public enum RegisterType

/// <summary>Structure register.</summary>
StructReg,
IntConverter,
}
}
33 changes: 10 additions & 23 deletions GenICam/Models/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,8 @@ public double Value
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation..</returns>
public async Task<IReplyPacket> SetValueAsync(long value)
{
return await SetValueAsync();
}

private async Task<IReplyPacket> SetValueAsync()
{
var value = await ExecuteFormulaTo();
return await PValue.SetValueAsync(value);
var toValue = await ExecuteFormulaTo(value);
return await PValue.SetValueAsync(toValue);
}

private async Task<double> ExecuteFormulaFrom()
Expand All @@ -99,9 +94,7 @@ private async Task<double> ExecuteFormulaFrom()
{
if (word.Equals("TO"))
{
long? value = null;

value = await ExecuteFormulaTo();
long? value = await PValue.GetValueAsync();

if (value is null)
{
Expand All @@ -126,6 +119,7 @@ private async Task<double> ExecuteFormulaFrom()
}
}

FormulaFrom = MathParserHelper.FormatExpression(FormulaFrom);
return MathParserHelper.CalculateExpression(FormulaFrom);
}
catch (Exception ex)
Expand All @@ -134,39 +128,32 @@ private async Task<double> ExecuteFormulaFrom()
}
}

private async Task<long> ExecuteFormulaTo()
private async Task<long> ExecuteFormulaTo(long value)
{
try
{
foreach (var word in FormulaTo.Split())
{
if (word.Equals("FROM"))
{
long? value = await PValue.GetValueAsync();

if (value is null)
{
throw new GenICamException("Failed to read formula register value", new NullReferenceException());
}

FormulaTo = FormulaTo.Replace(word, string.Format("0x{0:X8}", value));
}

if (PVariables.ContainsKey(word))
{
long? value = null;
long? variableValue = null;
variableValue = await PVariables[word].GetValueAsync();

value = await PVariables[word].GetValueAsync();

if (value is null)
if (variableValue is null)
{
throw new GenICamException("Failed to read formula register value", new NullReferenceException());
}

FormulaTo = FormulaTo.Replace(word, string.Format("0x{0:X8}", value));
FormulaTo = FormulaTo.Replace(word, string.Format("0x{0:X8}", variableValue));
}
}

FormulaTo = MathParserHelper.FormatExpression(FormulaTo);
return (long)MathParserHelper.CalculateExpression(FormulaTo);
}
catch (Exception ex)
Expand Down
3 changes: 3 additions & 0 deletions GenICam/Services/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ private async Task<IPValue> GetRegister(XmlNode xmlNode)
case nameof(RegisterType.FloatReg):
return new GenIntReg(genRegister.address, genRegister.length, genRegister.accessMode, null, genRegister.pAddress, GenPort);

case nameof(RegisterType.IntConverter):
return await GetConverter(xmlNode);

case nameof(RegisterType.StructReg):
case nameof(RegisterType.MaskedIntReg):
if (xmlNode.Name.Equals(nameof(RegisterType.StructReg)))
Expand Down
4 changes: 4 additions & 0 deletions GigeVision.Core/GigeVision.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<PackageReference Include="System.IO.Pipelines" Version="6.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GenICam\GenICam.csproj" />
</ItemGroup>

<!--<ItemGroup>
<ProjectReference Include="..\GenICam\GenICam.csproj" />
</ItemGroup>-->
Expand Down
4 changes: 2 additions & 2 deletions GigeVision.Core/Services/GenPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<IReplyPacket> WriteAsync(byte[] pBuffer, long? address, long l
{
var addressBytes = GetAddressBytes((long)address, length);
Array.Reverse(addressBytes);
await Gvcp.TakeControl(false);
//await Gvcp.TakeControl(false);
return await Gvcp.WriteRegisterAsync(addressBytes, BitConverter.ToUInt32(pBuffer)).ConfigureAwait(false);
}
catch (Exception ex)
Expand All @@ -64,7 +64,7 @@ public async Task<IReplyPacket> WriteAsync(byte[] pBuffer, long? address, long l
}
finally
{
await Gvcp.LeaveControl();
//await Gvcp.LeaveControl();
}
}

Expand Down
10 changes: 9 additions & 1 deletion GigeVision.Core/Services/Gvcp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,15 @@ public async Task<bool> ReadXmlFileAsync(string ip = null)
XmlDocument xml = new XmlDocument();
xml.Load(await GetXmlFileFromCamera(ip).ConfigureAwait(false));
xmlHelper = new XmlHelper(xml, new GenPort(this));
IsXmlFileLoaded = await xmlHelper.LoadUp();
try
{
await xmlHelper.LoadUp();
IsXmlFileLoaded = true;
}
catch (Exception)
{
IsXmlFileLoaded = false;
}

return IsXmlFileLoaded;
}
Expand Down

0 comments on commit 1e89038

Please sign in to comment.