#define LOOPBACK
using System;
using System.IO;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace ar664_example
{
public class ar664_playback
{
private const UInt64 DATARECS_BUFFER_SIZE_MAX = 64 * 1024;
static void Main(string[] args)
{
UInt64 server = 0;
UInt64 txBuffer = 0, rxBuffer = 0;
var module = new UInt64[1];
UInt64 device = 0;
var phyChn = new UInt64[1];
IntPtr rxRec = IntPtr.Zero;
UInt64 allocated;
UInt64 status;
UInt64 usedBytes, freeBytes;
UInt64 msgCount = 0;
UInt64 acqStopTime;
UInt32 rc;
UInt64 count = 0;
UInt64 currentTime = 0;
#if LOCAL
#else
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}\n", rc);
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS && count > 0)
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
#if LOOPBACK
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
try
{
rxRec = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_RUNNING) > 0)
Console.Write("Acquisition Running\n\n");
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
currentTime += 500 * 1000 * 1000;
rc = playback(currentTime, txBuffer);
}
if (rc == MAXT_SUCCESS)
{
rc = readAcquisitionData(rxBuffer, rxRec);
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_STOPPED) > 0)
{
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition stopped at {0}\n", acqStopTime);
}
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rxRec != IntPtr.Zero)
Marshal.FreeHGlobal(rxRec);
Console.Write("\n\nPress a key to terminate rc=0x{0:X8}\n", rc);
Console.ReadKey();
return;
}
private static UInt32 playback(UInt64 startTime, UInt64 txBuffer)
{
IntPtr txRecPtr = IntPtr.Zero;
byte[] byteArray;
BinaryReader fp;
UInt64 baseTimetag = 0;
int datarecSize = 0;
UInt32 iRecord = 0;
int length = 0;
UInt32 rc = MAXT_SUCCESS;
UInt64 writtenCount;
string fileName = "recorder.raw";
try
{
txRecPtr = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
try
{
fp = new BinaryReader(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Read));
}
catch
{
return MAXT_ERROR_FILE_OPEN;
}
Console.Write("Starting Playback\n\n");
do
{
length = fp.Read(byteArray = new byte[Marshal.SizeOf(length)], 0, Marshal.SizeOf(length));
datarecSize = BitConverter.ToInt32(byteArray, 0);
if (length > 0)
{
length = fp.Read(byteArray = new byte[datarecSize], 0, datarecSize);
if (length > 0)
{
txRec = ByteArrayToDatarec664(byteArray);
if (++iRecord == 1)
txRec.
control = MXF_A664_CTRL_TX_RAW_IP_CHECKSUM_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_CRC32_INCLUDED | MXF_A664_CTRL_TX_RAW_MAC_NETID_INCLUDED;
Marshal.StructureToPtr(txRec, txRecPtr, true);
rc =
mxfA664TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, 1, txRecPtr, out writtenCount);
}
}
Console.Write(".");
} while (length > 0 && rc == MAXT_SUCCESS);
fp.Close();
return rc;
}
{
IntPtr ptr = Marshal.AllocHGlobal(Array.Length);
dataRec.
data =
new byte[Array.Length];
Marshal.Copy(Array, 0, ptr, Array.Length);
Marshal.FreeHGlobal(ptr);
return dataRec;
}
private static UInt32 readAcquisitionData(UInt64 rxBuffer, IntPtr rec664)
{
IntPtr recPtr = rec664;
UInt64 recordIdx, dataIdx;
UInt64 status, msgsCount, bytesCount;
UInt32 dataSize;
UInt32 rc = 0;
rc =
mxfA664RxAcqRead(rxBuffer, 0, DATARECS_BUFFER_SIZE_MAX, out status, out msgsCount, out bytesCount, rec664);
if (rc == MAXT_SUCCESS)
Console.Write("\n{0} records received\n\n", msgsCount);
for (recordIdx = 0; recordIdx < msgsCount && rc == MAXT_SUCCESS; recordIdx++)
{
Console.Write(" {0:000}: timeTag={1:D12}, Size={2}", recordIdx, rxRec664.timeTag, rxRec664.dataSize);
Console.Write("\n data=");
dataSize = rxRec664.dataSize;
for (dataIdx = 0; dataIdx < dataSize; dataIdx++)
{
Console.Write("{0:X2} ", rxRec664.data[dataIdx]);
if (((dataIdx + 1) % 8) == 0 && (dataIdx + 1 < rxRec664.dataSize))
Console.Write("\n ");
}
Console.Write("\n\n");
}
return rc;
}
}
}