tbf/TBFTests/Rig/RegisterReaders/iPerlASICReader/communication/C4/wiredProtocol/TouchReadFrameBuilderTest.cs

167 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.hexLogger;
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.protocolCommons;
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.wiredProtocol;
namespace TBFTests.Rig.RegisterReaders.iPerlASICReader.communication.C4.wiredProtocol
{
[TestClass]
[TestSubject(typeof(TouchReadFrameBuilder))]
public class TouchReadFrameBuilderTest
{
[TestMethod]
public void Encode_ViewFactoryId_Command()
{
byte[] frame = new TouchReadFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.ViewFactoryId)
.BuildBytes();
byte[] expected =
{
0x0D, // START
0x04, // LEN
0x08, // CONTROL (RF)
0x01, // COMMAND
0x00, // CHECKSUM HI
0x1A // CHECKSUM LO
};
CollectionAssert.AreEqual(expected, frame);
string log = TouchReadLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: < {0} >", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_ViewProgrammableId_Command()
{
byte[] frame = new TouchReadFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.ViewProgrammableId)
.BuildBytes();
byte[] expected =
{
0x0D, // START
0x04, // LEN
0x08, // CONTROL (RF)
0x03, // COMMAND
0x00, // CHECKSUM HI
0x1C // CHECKSUM LO
};
CollectionAssert.AreEqual(expected, frame);
string log = TouchReadLogger.DescribeTx(frame);
Console.WriteLine(log);
Console.WriteLine(@"Raw: <{0}>", HexFormatter.ToSerialHex(frame));
}
[TestMethod]
public void Encode_SetState_Idle()
{
// Arrange
byte[] frame = new TouchReadFrameBuilder()
.RequestResponse(true)
.AddCommand(ProtocolCommand.SetState)
.AddPayload(new byte[] { 0x01 }) // Idle
.BuildBytes();
byte[] expected =
{
0x0D, // START
0x05, // LEN
0x08, // CONTROL (RF)
0x1A, // COMMAND (Set State)
0x01, // PAYLOAD (Idle)
0x00, // CHECKSUM HI
0x35 // CHECKSUM LO
};
// Assert
CollectionAssert.AreEqual(expected, frame,
$"Encoded frame mismatch.\nExpected: {HexFormatter.ToSerialHex(expected)}\nActual: {HexFormatter.ToSerialHex(frame)}");
}
[TestMethod]
[ExpectedException(typeof(FormatException))]
public void Decode_InvalidStart_Throws()
{
byte[] response =
{
0x00, // invalid START
0x04,
0x00,
0x01,
0x00,
0x12
};
var parser = new TouchReadFrameParser();
parser.Parse(response);
}
[TestMethod]
public void BuildBytes()
{
// Reserve space for checksum at [0] and [1]
var frame = new List<byte>
{
0x00, // checksum high (placeholder)
0x00, // checksum low (placeholder)
0x05, // length
0x01, // command
0x0D // start
};
// Calculate checksum over payload only (skip checksum bytes)
ushort checksum = TouchReadFrameBuilder.CalculateChecksum(
frame.Skip(2)
);
// Write checksum into first two positions
frame[0] = (byte)(checksum >> 8); // high byte
frame[1] = (byte)(checksum & 0xFF); // low byte
Console.WriteLine(HexFormatter.ToHex(frame.ToArray()));
}
[TestMethod]
public void ByteConversion()
{
byte[] frame = { 0x53, 0x57,0x05, 0x01, 0x0D};
Console.WriteLine(@"1: " + HexFormatter.ToHex(frame.ToArray()));
byte[] payload = { 0x31, 0x30, 0x30, 0x31, 0x30, 0x34, 0x30, 0x35, 0x31, 0x00 };
Console.WriteLine(@"Payload: " + HexFormatter.ToSerialHexWithAscii(payload.ToArray()));
//Connect
//2026-02-04 14:06:22.515 TX (8) 53 57 3F 76 65 72 73 0D
//2026-02-04 14:06:22.906 RX (74) 3F 76 65 72 73 3A 20 48 61 72 72 79 20 54 3A 42 38 30 30 2C 20 56 3A 30 36 2E 30 36 2E 30 31 2C 20 46 57 3A 31 39 30 32 31 35 2C 20 37 45 43 45 2C 20 42 31 2E 36 2E 30 31 2C 20 48 57 3A 34 2C 20 53 65 72 69 61 6C 3A 30 0D
string msg = "53 57 3F 76 65 72 73 0D";
byte[] activationMsg = HexFormatter.HexStringToByteArray(msg);
Console.WriteLine(@"ActivationMsg MSG: " + HexFormatter.ToSerialHexWithAscii(activationMsg.ToArray()));
string str =
"3F 76 65 72 73 3A 20 48 61 72 72 79 20 54 3A 42 38 30 30 2C 20 56 3A 30 36 2E 30 36 2E 30 31 2C 20 46 57 3A 31 39 30 32 31 35 2C 20 37 45 43 45 2C 20 42 31 2E 36 2E 30 31 2C 20 48 57 3A 34 2C 20 53 65 72 69 61 6C 3A 30 0D";
byte[] activationMsgRes = HexFormatter.HexStringToByteArray(str);
Console.WriteLine(@"ActivationMsg RES: " + HexFormatter.ToSerialHexWithAscii(activationMsgRes.ToArray()));
}
}
}