138 lines
4.6 KiB
C#
138 lines
4.6 KiB
C#
/*-------------------------------------------------------------------------
|
|
// UNCLASSIFIED
|
|
/*-------------------------------------------------------------------------
|
|
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
|
|
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
|
|
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
|
|
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
|
|
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
|
|
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
|
|
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
|
|
COMPANY.
|
|
|
|
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
|
|
GOVERNMENT.
|
|
|
|
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
|
|
-------------------------------------------------------------------------*/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NLog;
|
|
using Raytheon.Instruments;
|
|
|
|
namespace ProgramLib
|
|
{
|
|
/// <summary>
|
|
/// Perform UART loopback test
|
|
/// </summary>
|
|
internal class UartLoopBackAction : BasicAction
|
|
{
|
|
#region PrivateClassMembers
|
|
private static NLog.ILogger _logger;
|
|
|
|
private ICommDevice _serialDevice;
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public UartLoopBackAction()
|
|
{
|
|
_logger = LogManager.GetCurrentClassLogger();
|
|
|
|
string serialDeviceInstanceName = Program.Instance().ProgramSpecificConfig.ReadValue("UART_INFO", "SERIAL_DEVICE_INSTANCE_NAME");
|
|
_serialDevice = (ICommDevice)Program.Instance().InstrumentManager.GetGenericInstrument(serialDeviceInstanceName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Run the action
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override void Run()
|
|
{
|
|
try
|
|
{
|
|
_logger?.Debug($"Entering {this.GetType().Name}::{System.Reflection.MethodBase.GetCurrentMethod().Name}() method...");
|
|
|
|
SendCommandAndGetResponse();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Send commnd and get response
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public void SendCommandAndGetResponse()
|
|
{
|
|
UartHandshakeMessage msg = new UartHandshakeMessage(0xCCAAFFEEDDAAEEDD, 0xDDEEAADDEEFFAACC);
|
|
List<byte> msgByteList = Util.StructToByteList(msg).ToList();
|
|
// remove checksum byte
|
|
msgByteList.RemoveAt(msgByteList.Count - 1);
|
|
msgByteList.Add(Util.GetTwosComplimentChecksum(msgByteList.ToArray()));
|
|
_serialDevice.Write(msgByteList.ToArray(), (uint)msgByteList.Count);
|
|
_logger.Info($"Raw message sent: {Util.ByteArrayToHexString(msgByteList.ToArray())}");
|
|
|
|
List<byte> receiveByteList = new List<byte>();
|
|
byte[] receiveByteArray = null;
|
|
uint totalBytesRead = 0;
|
|
|
|
_logger.Info($"Waiting for message to be looped back...");
|
|
|
|
uint numBytesRead = _serialDevice.Read(ref receiveByteArray);
|
|
// this is for loopback, we read too fast and we're only getting partial data
|
|
if (numBytesRead < msgByteList.Count)
|
|
{
|
|
_serialDevice.SetReadTimeout(100);
|
|
try
|
|
{
|
|
do
|
|
{
|
|
totalBytesRead += numBytesRead;
|
|
receiveByteList.AddRange(receiveByteArray.Skip(0).Take((int)numBytesRead).ToArray());
|
|
receiveByteArray = null;
|
|
numBytesRead = _serialDevice.Read(ref receiveByteArray);
|
|
}
|
|
while (true);
|
|
}
|
|
catch { }
|
|
// reset to default timeout
|
|
_serialDevice.SetReadTimeout();
|
|
receiveByteArray = receiveByteList.ToArray();
|
|
}
|
|
else
|
|
totalBytesRead = numBytesRead;
|
|
|
|
_logger.Info($"Raw Msg Received: {Util.ByteArrayToHexString(receiveByteArray, (int)totalBytesRead)}");
|
|
|
|
if (totalBytesRead != msgByteList.Count)
|
|
{
|
|
throw new Exception($"Unexpected message size. Expected msg size: {msgByteList.Count}. Actual msg size: {totalBytesRead}");
|
|
}
|
|
|
|
bool loopbackSuccess = true;
|
|
for (int i = 0; i < totalBytesRead; i++)
|
|
{
|
|
if (msgByteList[i] != receiveByteArray[i])
|
|
{
|
|
loopbackSuccess = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!loopbackSuccess)
|
|
{
|
|
throw new Exception($"Messages sent and received don't match");
|
|
}
|
|
|
|
_logger.Info($"UART loopback is successful.");
|
|
}
|
|
}
|
|
}
|