7using System.Collections;
79 string blockName =
"";
114 message =
"Operation succeeded";
118 message =
"Memory block is already open and cannot be opened again";
122 message =
"Memory block is closed and cannot be accessed";
126 message =
"The given name is not a recognized memory block name";
130 message =
"The handle argument does not correspond to a valid open memory block";
134 message =
"The given offset is out of bounds";
138 message =
"The block name pointer or return handle pointer argument is NULL";
142 message =
"Unrecognized error code.";
161 return String.Format(
"{0}: {1}", operation, msg);
176 if (!String.IsNullOrEmpty(blockName))
182 int memorySizeInChunks = 0;
187 "Memory block not opened so cannot retrieve memory block size");
211 public byte[]
Read(
int byteOffset, uint maxBytes)
218 byte[] data =
new byte[maxBytes];
222 int chunkOffset = byteOffset / (
sizeof(UInt32));
224 UInt32 bufferIndex = 0;
229 int byteOffsetInChunk = byteOffset % (
sizeof(UInt32));
230 while (bufferIndex < maxBytes)
232 data[bufferIndex] = (byte) (value & 0xff);
236 if (byteOffsetInChunk ==
sizeof(UInt32))
243 byteOffsetInChunk = 0;
272 public void Write(
int byteOffset,
byte[] data, uint maxBytes)
279 int chunkOffset = byteOffset /
sizeof(UInt32);
281 int byteOffsetInChunk = byteOffset %
sizeof(UInt32);
282 UInt32 bufferIndex = 0;
283 UInt32 byteMask = (UInt32)0xff << (byteOffsetInChunk * 8);
284 if (byteOffsetInChunk != 0)
290 while (bufferIndex < maxBytes)
293 value |= ((UInt32)data[bufferIndex]) << (byteOffsetInChunk * 8);
297 if (byteOffsetInChunk ==
sizeof(UInt32))
303 byteOffsetInChunk = 0;
324 if (byteOffsetInChunk != 0)
354 StringBuilder output =
new StringBuilder();
355 string indentSpaces =
new string(
' ', indent);
357 if (data !=
null && maxBytes != 0)
359 int byteCount = (int)maxBytes;
360 if (byteCount > data.Length)
362 byteCount = data.Length;
364 int bytesPerRow = 32;
365 for (
int row = 0; row < maxBytes; row += bytesPerRow)
367 output.AppendFormat(
"{0}{1:x4} --", indentSpaces, row);
368 for (
int col = 0; col < bytesPerRow && (row + col) < maxBytes; ++col)
374 output.AppendFormat(
"{0:x2}", data[row + col]);
376 output.Append(Environment.NewLine);
379 return output.ToString();
383#region IDisposable Members
const char * BLOCK_NAME_0
Name of the first block.
DDR_ErrorCode DDR_SetDataChunk(int dataHandle, int chunkOffset, uint32_t value)
Writes a single 32-bit value to the given offset in the memory block indicated by the specified handl...
const char * BLOCK_NAME_2
Name of the third block.
DDR_ErrorCode DDR_GetDataChunk(int dataHandle, int chunkOffset, uint32_t *value)
Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
const char * BLOCK_NAME_1
Name of the second block.
DDR_ErrorCode
Represents the possible errors that can be returned from the memory block access functions.
@ DDR_MAX_OFFSET
All offsets must from 0 to 1 less than this value.
Represents some P/Invoke functions for accessing a named blocks of memory to read/write data in the A...
DDR_ErrorCode
Represents the possible errors that can be returned from the memory block access functions....
static DDR_ErrorCode DDR_GetMemorySize(Int32 dataHandle, out Int32 memorySizeInChunks)
P/Invoke wrapper that retrieves the number of chunks in the memory block indicated by the handle to t...
static DDR_ErrorCode DDR_OpenMemoryBlock(string blockName, out Int32 dataHandle)
P/Invoke wrapper that opens access to a memory block for exclusive use, given the name of the memory ...
const int DDR_MAX_OFFSET
All offsets must from 0 to 1 less than this value.
static DDR_ErrorCode DDR_CloseMemoryBlock(Int32 dataHandle)
P/Invoke wrapper that closes access to a previously opened memory block, thus releasing it for others...
Represents an error that occurred when reading or writing data in the Data reader/writer.
DataReaderWriterException(string msg)
Represents a data reader/writer to a caller.
string _ConstructErrorMessage(DDR_ErrorCode errorCode, string operation)
Creates a formatted error message from the given operation, using the error code from the Adapter_Bac...
string BufferToString(byte[] data, uint maxBytes, int indent)
Convert the specified data up to the specified number of bytes into a string by performing a "hex dum...
uint _memoryBlockByteSize
void Dispose()
Shut down the data reader/writer and dispose of resources.
void Write(int byteOffset, byte[] data, uint maxBytes)
Write a specified number of bytes.
string _GetErrorMessage(DDR_ErrorCode errorCode)
Convert the given error code to a string message.
uint MemoryBlockByteSize
Retrieve the size of the memory block in bytes.
byte[] Read(int byteOffset, uint maxBytes)
Read a specified number of bytes.
MemoryBlockNumber
Represents the memory blocks that can be accessed. Hides how memory blocks are actually identified.
@ Memory_Block_0
First block.
@ Memory_Block_2
Third block.
@ Memory_Block_1
Second block.
DataReaderWriter(MemoryBlockNumber blockNumber)
Constructor for a data reader/writer.
string _GetBlockNameForBlockNumber(MemoryBlockNumber blockNumber)
Helper function to convert a value from the MemoryBlockNumber enumeration, which specifies the number...
Represents an error that occurred during initialization or shut down of the Data reader/writer.
DataReaderWriterInitException(string msg)
The namespace containing all Design Pattern Examples implemented in C#.