Files
tbf/TBF/Rig/RegisterReaders/iPerlASICReader/communication/OptoHeadTest.cs
T

123 lines
3.4 KiB
C#

using System;
using System.IO.Ports;
using Common;
using log4net;
using TBF.Rig.RegisterReaders.iPerlASICReader.communication.Utils;
using TBF.Rig.Uni.SharedDialogs.SmartMetersCommunication.common;
namespace TBF.Rig.RegisterReaders.iPerlASICReader.communication
{
public class OptoHeadTest : IDisposable
{
protected static readonly ILog rfidDataLogger = LogManager.GetLogger("RfidData");
private static SerialDriver serialDriver;
public static SerialDriver BuildConnection(ISmartReader iHead)
{
return new SerialDriverBuilder()
.WithPort($"COM{iHead.RfidComPortNr}")
.WithBaudRate(2400)
.WithDataBits(8)
.WithParity(Parity.None)
.WithStopBits(StopBits.One)
.WithTimeouts(4000, 2000)
.BuildAndConnect();
}
public void CloseConnection()
{
if (serialDriver != null)
serialDriver.CloseConnection();
}
public static string ReadRequest_PCB(ISmartReader iHead)
{
if (iHead.DebugLevel == DebugMode.Simulate)
{
return "-OK Simulated response-";
}
try
{
if (iHead != null)
{
if (serialDriver == null)
serialDriver = BuildConnection(iHead);
RadioService headService = new RadioService(serialDriver);
string serialNo = headService.ReadRequest_PCB(iHead);
return serialNo;
}
}
catch (Exception ex)
{
return (ex.Message.ToString());
}
return "";
}
public static string SetTestMode(ISmartReader iHead)
{
if (iHead.DebugLevel == DebugMode.Simulate)
{
return "-OK Simulated response-";
}
try
{
if (iHead != null)
{
if (serialDriver == null)
serialDriver = BuildConnection(iHead);
RadioService headService = new RadioService(serialDriver);
string answer = headService.SetTestMode(iHead);
return answer;
}
}
catch (Exception ex)
{
return (ex.Message.ToString());
}
return "";
}
public static string SetActiveMode(ISmartReader iHead)
{
if (iHead.DebugLevel == DebugMode.Simulate)
{
return "-OK Simulated response-";
}
try
{
if (iHead != null)
{
if (serialDriver == null)
serialDriver = BuildConnection(iHead);
RadioService headService = new RadioService(serialDriver);
string answer = headService.SetActiveMode(iHead);
return answer;
}
}
catch (Exception ex)
{
return (ex.Message.ToString());
}
return "";
}
public void Dispose()
{
CloseConnection();
}
}
}