Add and expand unit tests for iPerl communication, refactor OptoHead methods, update log4net reference, and increment AssemblyVersion.
542 lines
20 KiB
C#
542 lines
20 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Ports;
|
|
using System.Text;
|
|
using log4net;
|
|
using log4net.Appender;
|
|
using log4net.Layout;
|
|
using log4net.Repository.Hierarchy;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.diagnosticLed;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.diagnosticLed.parserer;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.hexLogger;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.IperlHatProtocol;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.protocolCommons;
|
|
|
|
|
|
namespace TBFTests.Rig.RegisterReaders.iPerlASICReader.communication.C4.IperlHatProtocol
|
|
{
|
|
[TestClass]
|
|
public class IperlHatIntegrationTests
|
|
{
|
|
|
|
public static class LogConfig
|
|
{
|
|
public static void Configure()
|
|
{
|
|
var hierarchy = (Hierarchy)LogManager.GetRepository();
|
|
hierarchy.Root.RemoveAllAppenders();
|
|
|
|
var patternLayout = new PatternLayout
|
|
{
|
|
ConversionPattern = "%date{yyyy-MM-dd HH:mm:ss.fff} [%thread] %-5level %logger - %message%newline"
|
|
};
|
|
patternLayout.ActivateOptions();
|
|
|
|
var consoleAppender = new ConsoleAppender
|
|
{
|
|
Layout = patternLayout
|
|
};
|
|
consoleAppender.ActivateOptions();
|
|
|
|
hierarchy.Root.AddAppender(consoleAppender);
|
|
hierarchy.Root.Level = log4net.Core.Level.Debug;
|
|
hierarchy.Configured = true;
|
|
}
|
|
}
|
|
ILog log = LogManager.GetLogger(typeof(IperlHatIntegrationTests));
|
|
|
|
private const string ComPort = "COM3"; // CHANGE THIS
|
|
private const int BaudRate = 2400;
|
|
private const int BaudRateOpto = 38400;
|
|
private const int ReadTimeoutMs = 2000;
|
|
|
|
[TestMethod]
|
|
[TestCategory("Hardware")]
|
|
[TestCategory("Serial")]
|
|
public void Serial_ViewFactoryId_ReadSerialNumber()
|
|
{
|
|
// -------- Arrange --------
|
|
byte[] request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.ViewFactoryId)
|
|
.BuildBytes();
|
|
|
|
var parser = new IperlHatFrameParser();
|
|
|
|
using (var port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One))
|
|
{
|
|
port.Handshake = Handshake.None;
|
|
port.ReadTimeout = 5000;
|
|
port.Open();
|
|
|
|
DateTime end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
byte[] response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
IperlHatResponse iperlHatResponse = parser.Parse(response);
|
|
|
|
Assert.IsTrue(iperlHatResponse.IsOk);
|
|
Assert.IsFalse(iperlHatResponse.Payload.Length < 8);
|
|
|
|
Console.WriteLine("\nDone.");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[TestMethod]
|
|
[TestCategory("Hardware")]
|
|
[TestCategory("Serial")]
|
|
public void Serial_SetStatus_Idle_Active()
|
|
{
|
|
|
|
byte[] requestStatus = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.ViewState)
|
|
.BuildBytes();
|
|
|
|
|
|
|
|
var parser = new IperlHatFrameParser();
|
|
|
|
using (var port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One))
|
|
{
|
|
port.Handshake = Handshake.None;
|
|
port.ReadTimeout = 5000;
|
|
port.Open();
|
|
|
|
|
|
DateTime end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(requestStatus));
|
|
port.Write(requestStatus, 0, requestStatus.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
byte[] resStart = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
resStart = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(resStart));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
IperlHatResponse iperlHatResponseOld = parser.Parse(resStart);
|
|
Assert.IsTrue(iperlHatResponseOld.IsOk, "Idle No set!");
|
|
Console.WriteLine("We start with status: " + HexFormatter.ToHex(iperlHatResponseOld.Payload[0]));
|
|
//----------------------------------------------------------------
|
|
|
|
// -------- Arrange --------
|
|
byte[] request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.SetState)
|
|
.AddSubCommand(ProtocolStatuses.Idle)
|
|
.BuildBytes();
|
|
|
|
// -- set idle
|
|
end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
byte[] response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
IperlHatResponse iperlHatResponse = parser.Parse(response);
|
|
Assert.IsTrue(iperlHatResponse.IsOk, "Idle No set!");
|
|
//----------------------------------------------------------------
|
|
|
|
request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.SetState)
|
|
.AddSubCommand(ProtocolStatuses.Active)
|
|
.BuildBytes();
|
|
|
|
end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
iperlHatResponse = parser.Parse(response);
|
|
Assert.IsTrue(iperlHatResponse.IsOk, "Idle No set!");
|
|
//--------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("\nDone.");
|
|
}
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
[TestCategory("Hardware")]
|
|
public void Serial_RawSniff()
|
|
{
|
|
using (var port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One))
|
|
{
|
|
port.Handshake = Handshake.None;
|
|
port.ReadTimeout = 5000;
|
|
port.Open();
|
|
|
|
DateTime end = DateTime.Now.AddSeconds(10);
|
|
//welcome message
|
|
byte[] frame = HexFormatter.HexStringToByteArray("53 57 3F 76 65 72 73 0D");
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(frame));
|
|
port.Write(frame, 0, frame.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
byte[] response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ",e);
|
|
}
|
|
}
|
|
|
|
|
|
end = DateTime.Now.AddSeconds(10);
|
|
byte[] frameID = HexFormatter.HexStringToByteArray("53 57 05 01 0D");
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(frameID));
|
|
port.Write(frameID, 0, frameID.Length);
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
byte[] response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ",e);
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("\nDone.");
|
|
}
|
|
}
|
|
|
|
public static byte[] ReadResponse(SerialPort port)
|
|
{
|
|
var result = new List<byte>();
|
|
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
int value = port.ReadByte(); // blocks until byte or timeout
|
|
|
|
if (value < 0)
|
|
throw new IOException("Serial port returned end of stream.");
|
|
|
|
byte b = HexFormatter.ToHexByte(value);
|
|
result.Add(b);
|
|
|
|
// stop when CR received
|
|
if (b == 0x0D)
|
|
break;
|
|
}
|
|
|
|
return result.ToArray();
|
|
}
|
|
catch (TimeoutException ex)
|
|
{
|
|
if (result.Count > 0)
|
|
return result.ToArray();
|
|
|
|
throw new TimeoutException("Timeout reading response from serial port.", ex);
|
|
}
|
|
}
|
|
|
|
|
|
[TestMethod]
|
|
[TestCategory("Hardware")]
|
|
public void OptoCommunicationON_ReadOpto_CommunicatonOFF()
|
|
{
|
|
//OPEN COMMUNICATION to Iperl Hat
|
|
|
|
var parser = new IperlHatFrameParser();
|
|
|
|
using (var port = new SerialPort(ComPort, BaudRate, Parity.None, 8, StopBits.One))
|
|
{
|
|
port.Handshake = Handshake.None;
|
|
port.ReadTimeout = 5000;
|
|
port.Open();
|
|
|
|
//----------------------------------------------------------------
|
|
// ------ Set active mode ------
|
|
//----------------------------------------------------------------
|
|
|
|
byte[] requestStatus = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.SetState)
|
|
.AddSubCommand(ProtocolStatuses.Active)
|
|
.BuildBytes();
|
|
|
|
DateTime end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(requestStatus));
|
|
port.Write(requestStatus, 0, requestStatus.Length);
|
|
|
|
Console.WriteLine("Set active mode - Listening for 10 seconds...");
|
|
byte[] resStart = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
resStart = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(resStart));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
IperlHatResponse iperlHatResponseOld = parser.Parse(resStart);
|
|
Assert.IsTrue(iperlHatResponseOld.IsOk, "Active No set!");
|
|
//----------------------------------------------------------------
|
|
// ------ Enable Opto data ------
|
|
//----------------------------------------------------------------
|
|
|
|
// ----------- Arrange -----------
|
|
byte[] request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddDeviceCommand(ProtocolDeviceSubCommand.SetDiagnosticLEDState)
|
|
.AddPayload(DiagnosticLedState.State4)
|
|
.BuildBytes();
|
|
|
|
end = DateTime.Now.AddSeconds(10);
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Enable Opto data - Listening for 10 seconds...");
|
|
byte[] response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
IperlHatResponse iperlHatResponse = parser.Parse(response);
|
|
Assert.IsTrue(iperlHatResponse.IsOk, "Opto data not set!");
|
|
|
|
//----------------------------------------------------------------
|
|
// ------ Test Opto data ------
|
|
//----------------------------------------------------------------
|
|
//Now try test opto data
|
|
Serial_OptoRawSniff();
|
|
|
|
//----------------------------------------------------------------
|
|
// ------ Disable Opto data ------
|
|
//----------------------------------------------------------------
|
|
|
|
// ----------- Arrange -----------
|
|
request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddDeviceCommand(ProtocolDeviceSubCommand.SetDiagnosticLEDState)
|
|
.AddPayload(DiagnosticLedState.StateOFF)
|
|
.BuildBytes();
|
|
|
|
end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Disable Opto data - Listening for 10 seconds...");
|
|
response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
iperlHatResponse = parser.Parse(response);
|
|
Assert.IsTrue(iperlHatResponse.IsOk, "Disabled opto LED not set!");
|
|
|
|
//----------------------------------------------------------------
|
|
// ------ Set Idle mode ------
|
|
//----------------------------------------------------------------
|
|
|
|
// ----------- Arrange -----------
|
|
request = new IperlHatFrameBuilder()
|
|
.RequestResponse(true)
|
|
.AddCommand(ProtocolCommand.SetState)
|
|
.AddSubCommand(ProtocolStatuses.Idle)
|
|
.BuildBytes();
|
|
|
|
end = DateTime.Now.AddSeconds(10);
|
|
|
|
Console.WriteLine("TX → " + HexFormatter.ToSerialHexWithAscii(request));
|
|
port.Write(request, 0, request.Length);
|
|
|
|
Console.WriteLine("Set Idle mode - Listening for 10 seconds...");
|
|
response = null;
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
response = ReadResponse(port);
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHexWithAscii(response));
|
|
break;
|
|
}
|
|
catch (TimeoutException e)
|
|
{
|
|
Assert.Fail("Timeout: ", e);
|
|
}
|
|
}
|
|
|
|
iperlHatResponse = parser.Parse(response);
|
|
Assert.IsTrue(iperlHatResponse.IsOk, "Idle status not set!");
|
|
|
|
//----------------------------------------------------------------
|
|
// ------ Test finished ------
|
|
//----------------------------------------------------------------
|
|
Console.WriteLine("\nDone.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// This test work only if Opto data are active
|
|
/// Use OptoCommunicationON_ReadOpto_CommunicatonOFF() test method
|
|
/// </summary>
|
|
[TestMethod]
|
|
[TestCategory("Hardware")]
|
|
public void Serial_OptoRawSniff_Standalone()
|
|
{
|
|
Serial_OptoRawSniff();
|
|
}
|
|
|
|
//method test connection to optho head
|
|
private void Serial_OptoRawSniff()
|
|
{
|
|
using (var port = new SerialPort("COM15", BaudRateOpto, Parity.None, 8, StopBits.One))
|
|
{
|
|
port.Handshake = Handshake.None;
|
|
port.ReadTimeout = 10000;
|
|
|
|
// 🔑 CRLF handling
|
|
port.NewLine = "\r\n";
|
|
port.Encoding = Encoding.ASCII; // or UTF8 if needed
|
|
|
|
port.Open();
|
|
|
|
Console.WriteLine("Listening for 10 seconds...");
|
|
DateTime end = DateTime.Now.AddSeconds(10);
|
|
|
|
var parser = new DiagnosticLedParser(DiagnosticLedState.State4);
|
|
|
|
while (DateTime.Now < end)
|
|
{
|
|
try
|
|
{
|
|
string line = port.ReadLine(); // string
|
|
byte[] bytes = port.Encoding.GetBytes(line);
|
|
|
|
Console.WriteLine("RX ← " + HexFormatter.ToSerialHex(bytes));
|
|
|
|
try
|
|
{
|
|
var data = (DiagnosticLedState4Data)parser.ParseLine(line,false);
|
|
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] Time: Parsed: {data}");
|
|
log.Info("Parsed: " + data);
|
|
}catch(Exception e)
|
|
{
|
|
Console.WriteLine("Failed to parse: " + e.Message);
|
|
}
|
|
|
|
}
|
|
catch (TimeoutException)
|
|
{
|
|
Assert.Fail("Serial read timeout");
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("\nDone.");
|
|
}
|
|
}
|
|
|
|
}
|
|
} |