Related
I learned a bit of the basics of C# language a couple years ago and now I'm slowly trying to increase knowledge of it through the creation of a trainer for a CEMU game i really enjoyed (hope i dont get criticized for my honesty).
Since i'm a noob at this i learned much from looking at another trainer that a programer released on gitHub, mostly copying classes and then trying to understand the how and why of things... Surely not the best way to learn but one has to start somewhre. But now i'm way out of my league.
I've managed to get offsets (2byte Big Endian) of many things (HP,Stamina,Speed, Stamina) and am able to chage values. Now i wanted to get player position and though i have right offsets i have no idea how work with Float Big Endian.
My repositary (not all files yet) is here:
https://github.com/Snowfall007/ZELDA-BOTW-Trainer
The class - ProcessWrapper is probably the key, but dont know how to work it.
Any good samaritan with patience and skill to help this kid at heart?
internal class ProcessWrapper
{
private readonly string _processName;
private string _processLocation;
private Kernel32.SafeHPROCESS _process;
private long _baseAddress = 0x0;
public ProcessWrapper(string processName)
{
_processName = processName;
}
public bool OpenProcess()
{
Process[] processes = Process.GetProcessesByName(_processName);
if (processes.Length != 1)
{
return false;
}
// Required rights : 0x0010 | 0x0400 | 0x0020 | 0x0008
_process = Kernel32.OpenProcess(ACCESS_MASK.GENERIC_ALL, true, (uint)processes[0].Id);
if (processes[0].MainModule == null) throw new Exception("Process main module is null.");
_processLocation = processes[0].MainModule.FileName;
return true;
}
public void SetBaseAddress(long baseAddress)
{
_baseAddress = baseAddress;
}
public bool OverwriteMemoryProtection(long address, int size)
{
return Kernel32.VirtualProtectEx(_process, (IntPtr)(_baseAddress + address), size, Kernel32.MEM_PROTECTION.PAGE_READWRITE, out Kernel32.MEM_PROTECTION oldProtection);
}
public byte[] ReadMemory(long address, int size, MemoryType memoryType)
{
IntPtr memoryReadPtr = Marshal.AllocHGlobal(size);
Kernel32.ReadProcessMemory(_process, (IntPtr)(_baseAddress + address), memoryReadPtr, size, out SizeT readSize);
byte[] memoryBuffer = new byte[readSize];
Marshal.Copy(memoryReadPtr, memoryBuffer, 0, readSize);
Marshal.FreeHGlobal(memoryReadPtr);
return ProcessMemoryType(memoryBuffer, memoryType);
}
public byte[] ReadMemory(Offset offset)
{
return ReadMemory(offset.Address, offset.Size, offset.MemoryType);
}
public bool WriteMemory(long address, byte[] data, MemoryType memoryType)
{
data = ProcessMemoryType(data, memoryType);
IntPtr dataPtr = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, dataPtr, data.Length);
bool writeResult = Kernel32.WriteProcessMemory(_process, (IntPtr)(_baseAddress + address), dataPtr, data.Length, out SizeT wroteCount);
Marshal.FreeHGlobal(dataPtr);
return writeResult;
}
public bool WriteMemory(Offset offset, byte[] data)
{
return WriteMemory(offset.Address, data, offset.MemoryType);
}
public string GetProcessLocation()
{
return _processLocation;
}
private static byte[] ProcessMemoryType(byte[] data, MemoryType memoryType)
{
if (memoryType == MemoryType.BigEndian && data.Length > 1)
{
data = data.Reverse().ToArray();
}
return data;
}
}
//In Form1 class i've have also these instructions:
//Coordinates
//Position
public int GetXPosition()
{
return ReadOffset(GameOffsets.Xposition);
}
public bool UpdateXPosition(int value)
{
return WriteOffset(GameOffsets.Xposition, value);
}
public int GetYPosition()
{
return ReadOffset(GameOffsets.Yposition);
}
public bool UpdateYPosition(int value)
{
return WriteOffset(GameOffsets.Yposition, value);
}
public int GetZPosition()
{
return ReadOffset(GameOffsets.Zposition);
}
public bool UpdateZPosition(int value)
{
return WriteOffset(GameOffsets.Zposition, value);
}
//AND ALSO THIS:
private int ReadOffset(Offset offset)
{
byte[] data = _processWrapper.ReadMemory(offset);
if (data.Length != offset.Size) return -1;
else if (data.Length == 1) return data[0];
else if (data.Length == 2) return BitConverter.ToInt16(data, 0);
throw new Exception("32 Bits and 64 Bits types are currently not handled");
}
How can i make changes to work specifically with Float Big Endian?
I am Trying to make a Game Engine in C# and OpenGL using OpenGL.NET. I have abstracted the Basic OpenGL Objects like VAO, VBO, etc. I Have a BufferLayout Class which specifies the Data present in the Vertex Buffer. The method in the VertexArray class that adds a buffer is this:
public void AddBuffer(uint index, ref VertexBuffer buffer)
{
Bind();
buffer.Bind();
uint i = index;
foreach (BufferElement element in buffer.Layout.Elements)
{
Gl.VertexAttribPointer(
i++,
element.GetComponentCount(),
Globals.ShaderDataTypeToOpenGLBaseType(element.Type),
element.Normalized,
buffer.Layout.Stride,
element.Offset
);
}
m_Buffers.Add(buffer);
}
This method has 2 Overloads: One which takes an IntPtr as the Offset and another which takes in an object.
This is the overload which takes in an object. But for some reason, the element.Offset is not working as expected. As I am only using a positions attribute for this test, it works when I replace element.Offset with null.
Here is BufferLayout.cs
public enum ShaderDataType
{
None,
Float, Float2, Float3, Float4,
Mat3, Mat4,
Int, Int2, Int3, Int4,
}
public class BufferElement
{
internal string Name;
internal ShaderDataType Type;
internal int Size;
internal int Offset;
internal bool Normalized;
public BufferElement(ShaderDataType type, string name, bool normalized = false)
{
Name = name;
Type = type;
Size = Globals.ShaderDataTypeSize(type);
Offset = 0;
Normalized = normalized;
}
internal void SetOffset(int offset) => Offset = offset;
public int GetComponentCount()
{
switch (Type)
{
case ShaderDataType.Float: return 1;
case ShaderDataType.Float2: return 2;
case ShaderDataType.Float3: return 3;
case ShaderDataType.Float4: return 4;
case ShaderDataType.Mat3: return 3 * 3;
case ShaderDataType.Mat4: return 4 * 4;
case ShaderDataType.Int: return 1;
case ShaderDataType.Int2: return 2;
case ShaderDataType.Int3: return 3;
case ShaderDataType.Int4: return 4;
}
return 0;
}
}
internal sealed class BufferLayout
{
public List<BufferElement> Elements { get; private set; }
public int Stride { get; private set; }
public BufferLayout(BufferElement[] e)
{
Elements = new List<BufferElement>();
Elements.AddRange(e);
CalculateOffsetsAndStride();
}
private void CalculateOffsetsAndStride()
{
int offset = 0;
Stride = 0;
for (int i = 0; i < Elements.Count; i++)
{
Elements[0].SetOffset(offset);
offset += Elements[0].Size;
Stride += Elements[0].Size;
}
}
}
The overload which takes an object is meant for compatibility profile OpenGL contex and the object has to be an object containing the vertex array data.
If you want to set an offset, then you have to create an IntPtr. e.g:
var offset = new IntPtr(element.Offset);
Gl.VertexAttribPointer(
i++,
element.GetComponentCount(),
Globals.ShaderDataTypeToOpenGLBaseType(element.Type),
element.Normalized,
buffer.Layout.Stride,
offset
);
Note, the offset and the stride parameter have to be specified in bytes.
I'm looking for an implementation of a three dimensional circular buffer in C# .NET that allows me to extract slices of the buffer.
I tried the multidimensional array but it doesn't allow me to get a specific slice of the buffer.
I also tried to implement a version using jagged array, but i have find some problems in understanding how to initialzie the matrix, and also in slice extraction.
Thanks for the help.
[EDIT 1]
The buffer will be used to store data from reltime sensors, it will be fixed in width height and the depth (time which be the circular part) will be defined by the user.
The slices i need will be a fixed size matrix (width and heigh with the data of a given time).
[EDIT 2]
The only implementation that i could implement that works. With the jagged array i'm still stuck at the declaration (i find it chaotic)
public class CircularBufferMatrix<T>
{
private readonly T[,,] _buffer;
public CircularBufferMatrix(int width, int height, int depth)
{
Width = width;
Height = height;
Depth = depth;
_buffer = new T[width, height, depth];
}
public T this[int x, int y, int z]
{
set { _buffer[x, y, z%Depth] = value; }
get { return _buffer[x, y, z%Depth]; }
}
#region Getters
public int Width { get; }
public int Height { get; }
public int Depth { get; }
#endregion
}
I would split these in two classes. You want something like a CircularBuffer class, which handles the reading and writing. The other could be an implementation of the 2D array you want to store. The reason of splitting these, is because you want to Read/write frames separately.
For example:
Circular buffer implementation:
public class CircularBuffer<T>
{
private T[] _buffer;
private int IncRollover(int value)
{
value++;
if (value >= _buffer.Length)
value = 0;
return value;
}
public CircularBuffer(int count)
{
_buffer = new T[count];
}
public bool Write(T element)
{
// if the writeindex (after increasing) equals the readindex, the buffer is full
var newWriteIndex = IncRollover(WriteIndex);
if (newWriteIndex == ReadIndex)
return false;
_buffer[WriteIndex] = element;
WriteIndex = newWriteIndex;
return true;
}
public bool TryRead(out T element)
{
if (ReadIndex == WriteIndex)
{
element = default(T);
return false;
}
element = _buffer[ReadIndex];
ReadIndex = IncRollover(ReadIndex);
return true;
}
public IEnumerable<T> ReadAll()
{
T element;
while (TryRead(out element))
yield return element;
}
public int ReadIndex { get; private set; }
public int WriteIndex { get; private set; }
}
This will take care of reading and writing induvidual 'frames'/slices. You can expand this class if you like to read on Index.
note: The write will return false if the buffer is full
This could be an implementation of the 2d buffers stored within the Circular buffer:
public class MyWhateverBuffer<T>
{
private CircularBuffer<T[,]> _buffer;
public int Width { get; private set; }
public int Height { get; private set; }
public int Depth { get; private set; }
public MyWhateverBuffer(int width, int height, int depth)
{
Width = width;
Height = height;
Depth = depth;
_buffer = new CircularBuffer<T[,]>(depth);
}
public T[,] New()
{
return new T[Width, Height];
}
public bool Add(T[,] buffer)
{
return _buffer.Write(buffer);
}
public bool TryRead(out T[,] buffer)
{
return _buffer.TryRead(out buffer);
}
public IEnumerable<T[,]> ReadAll()
{
return _buffer.ReadAll();
}
}
This class can be used like:
MyWhateverBuffer<double> myBuffer = new MyWhateverBuffer<double>(100, 100, 100);
var oneSlice = myBuffer.New();
oneSlice[10, 10] = 3.5;
oneSlice[50, 10] = 23.5;
oneSlice[10, 20] = 43.5;
myBuffer.Add(oneSlice);
var anotherSlice = myBuffer.New();
anotherSlice[10, 10] = 13.5;
anotherSlice[50, 10] = 23.5;
anotherSlice[10, 20] = 43.5;
var result = myBuffer.Add(anotherSlice);
if(!result)
{
// the buffer was full..
}
// Read the results from the buffer.
foreach(var slice in myBuffer.ReadAll())
{
Trace.WriteLine(slice[10, 10]);
}
You should always check if the buffer could be added. You don't want to lose info.
Side note:
The most profit gains with a Circular buffer is declaring the elements ones. Like big arrays will be reused every time.
If I understand your requirements correctly, you want a circular buffer consisting of items that are two dimensional arrays of data and the data from one item is never repeated in other items. C# doesn't have a cicular buffer type but you can always create one yourself:-
public class CircularBuffer <T> // make it generic
{
public class Iterator
{
public void MoveForward ();
public void MoveBackward ();
public T Item { get { } };
CicrcularBuffer <T> m_container;
LinkedListNode <T> m_current_item;
}
public void Add (T item);
public Iterator GetIterator ();
LinkedList<T> m_data;
}
Then create a class to contain your data:-
public class SensorData
{
// contains the two dimension data
}
And use it like:-
var buffer = new CircularBuffer <SensorData> ();
var iterator = buffer.GetIterator ();
var item = iterator.Item;
iterator.MoveForward ();
Obviously, there's a lot to be filled in but it should get you started.
I'm looking for a way to use the RFID "RC522" on a Raspberry Pi 2.0 on Windows IOT.
It is of course not offical compatible...
The offical one (OM5577 demo board) is way to expensive in France (i haven't found any reseller who sold it without a lot of shipping cost (total cost is around 80$)).
The RC522 is cheap (<10$). It works great on Arduino and on Raspberry Pi 2.0 on linux. But unfortunatly not yet on Windows IOT.
I'm actually using an arduino as a bridge... It isn't an optimal solution; but work well and cost always half the price than the OM5577.
I've found this project and try to convert them into a VS (Visual C++) project with the Windows IOT SIP and IO... I miserably fail...
In my dream I would be able to use this device in C# with the standard windows IOT "ProximityDevice" class.
Have you any idea for me?
Thanks in advance.
I finally found a solution.
I haven't success in the arudino portability; so i've used this project as start point.
The project is writen in C#. I've just adapt the code for Windows IOT GPIO and SPI. An it's working !
Main
var mfrc = new Mfrc522();
await mfrc.InitIO();
while (true)
{
if (mfrc.IsTagPresent())
{
var uid = mfrc.ReadUid();
mfrc.HaltTag();
}
}
Library Mfrc522Lib.cs (all in one)
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.Gpio;
using Windows.Devices.Spi;
namespace Mfrc522Lib
{
public static class Registers
{
private const byte bitFraming = 0x0D;
private const byte comIrq = 0x04;
private const byte comIrqEnable = 0x02;
private const byte command = 0x01;
private const byte control = 0x0C;
private const byte error = 0x06;
private const byte fifoData = 0x09;
private const byte fifoLevel = 0x0A;
private const byte mode = 0x11;
private const byte rxMode = 0x13;
private const byte timerMode = 0x2A;
private const byte timerPrescaler = 0x2B;
private const byte timerReloadHigh = 0x2C;
private const byte timerReloadLow = 0x2D;
private const byte txAsk = 0x15;
private const byte txControl = 0x14;
private const byte txMode = 0x12;
private const byte version = 0x37;
public static byte BitFraming
{
get
{
return bitFraming;
}
}
public static byte ComIrq
{
get
{
return comIrq;
}
}
public static byte ComIrqEnable
{
get
{
return comIrqEnable;
}
}
public static byte Command
{
get
{
return command;
}
}
public static byte Control
{
get
{
return control;
}
}
public static byte Error
{
get
{
return error;
}
}
public static byte FifoData
{
get
{
return fifoData;
}
}
public static byte FifoLevel
{
get
{
return fifoLevel;
}
}
public static byte Mode
{
get
{
return mode;
}
}
public static byte RxMode
{
get
{
return rxMode;
}
}
public static byte TimerMode
{
get
{
return timerMode;
}
}
public static byte TimerPrescaler
{
get
{
return timerPrescaler;
}
}
public static byte TimerReloadHigh
{
get
{
return timerReloadHigh;
}
}
public static byte TimerReloadLow
{
get
{
return timerReloadLow;
}
}
public static byte TxAsk
{
get
{
return txAsk;
}
}
public static byte TxControl
{
get
{
return txControl;
}
}
public static byte TxMode
{
get
{
return txMode;
}
}
public static byte Version
{
get
{
return version;
}
}
}
public static class PiccResponses
{
private const ushort answerToRequest = 0x0004;
private const byte selectAcknowledge = 0x08;
private const byte acknowledge = 0x0A;
public static byte Acknowledge
{
get
{
return acknowledge;
}
}
public static byte SelectAcknowledge
{
get
{
return selectAcknowledge;
}
}
public static ushort AnswerToRequest
{
get
{
return answerToRequest;
}
}
}
public static class PiccCommands
{
private const byte anticollision_1 = 0x93;
private const byte anticollision_2 = 0x20;
private const byte authenticateKeyA = 0x60;
private const byte authenticateKeyB = 0x61;
private const byte halt_1 = 0x50;
private const byte halt_2 = 0x00;
private const byte read = 0x30;
private const byte request = 0x26;
private const byte select_1 = 0x93;
private const byte select_2 = 0x70;
private const byte write = 0xA0;
public static byte AuthenticateKeyA
{
get
{
return authenticateKeyA;
}
}
public static byte AuthenticateKeyB
{
get
{
return authenticateKeyB;
}
}
public static byte Halt_1
{
get
{
return halt_1;
}
}
public static byte Halt_2
{
get
{
return halt_2;
}
}
public static byte Read
{
get
{
return read;
}
}
public static byte Request
{
get
{
return request;
}
}
public static byte Select_1
{
get
{
return select_1;
}
}
public static byte Select_2
{
get
{
return select_2;
}
}
public static byte Write
{
get
{
return write;
}
}
public static byte Anticollision_1
{
get
{
return anticollision_1;
}
}
public static byte Anticollision_2
{
get
{
return anticollision_2;
}
}
}
public static class PcdCommands
{
private const byte idle = 0x00;
private const byte mifareAuthenticate = 0x0E;
private const byte transceive = 0x0C;
public static byte Idle
{
get
{
return idle;
}
}
public static byte MifareAuthenticate
{
get
{
return mifareAuthenticate;
}
}
public static byte Transceive
{
get
{
return transceive;
}
}
}
public class Uid
{
public byte Bcc { get; private set; }
public byte[] Bytes { get; private set; }
public byte[] FullUid { get; private set; }
public bool IsValid { get; private set; }
internal Uid(byte[] uid)
{
FullUid = uid;
Bcc = uid[4];
Bytes = new byte[4];
System.Array.Copy(FullUid, 0, Bytes, 0, 4);
foreach (var b in Bytes)
{
if (b != 0x00)
IsValid = true;
}
}
public sealed override bool Equals(object obj)
{
if (!(obj is Uid))
return false;
var uidWrapper = (Uid)obj;
for (int i = 0; i < 5; i++)
{
if (FullUid[i] != uidWrapper.FullUid[i])
return false;
}
return true;
}
public sealed override int GetHashCode()
{
int uid = 0;
for (int i = 0; i < 4; i++)
uid |= Bytes[i] << (i * 8);
return uid;
}
public sealed override string ToString()
{
var formatString = "x" + (Bytes.Length * 2);
return GetHashCode().ToString(formatString);
}
}
public sealed class Mfrc522
{
public SpiDevice _spi { get; private set; }
public GpioController IoController { get; private set; }
public GpioPin _resetPowerDown { get; private set; }
/* Uncomment for Raspberry Pi 2 */
private const string SPI_CONTROLLER_NAME = "SPI0";
private const Int32 SPI_CHIP_SELECT_LINE = 0;
private const Int32 RESET_PIN = 25;
internal async Task InitIO()
{
try
{
IoController = GpioController.GetDefault();
_resetPowerDown = IoController.OpenPin(RESET_PIN);
_resetPowerDown.Write(GpioPinValue.High);
_resetPowerDown.SetDriveMode(GpioPinDriveMode.Output);
}
/* If initialization fails, throw an exception */
catch (Exception ex)
{
throw new Exception("GPIO initialization failed", ex);
}
try
{
var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
settings.ClockFrequency = 1000000;
settings.Mode = SpiMode.Mode0;
String spiDeviceSelector = SpiDevice.GetDeviceSelector();
IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);
_spi = await SpiDevice.FromIdAsync(devices[0].Id, settings);
}
/* If initialization fails, display the exception and stop running */
catch (Exception ex)
{
throw new Exception("SPI Initialization Failed", ex);
}
Reset();
}
public void Reset()
{
_resetPowerDown.Write(GpioPinValue.Low);
System.Threading.Tasks.Task.Delay(50).Wait();
_resetPowerDown.Write(GpioPinValue.High);
System.Threading.Tasks.Task.Delay(50).Wait();
// Force 100% ASK modulation
WriteRegister(Registers.TxAsk, 0x40);
// Set CRC to 0x6363
WriteRegister(Registers.Mode, 0x3D);
// Enable antenna
SetRegisterBits(Registers.TxControl, 0x03);
}
public bool IsTagPresent()
{
// Enable short frames
WriteRegister(Registers.BitFraming, 0x07);
// Transceive the Request command to the tag
Transceive(false, PiccCommands.Request);
// Disable short frames
WriteRegister(Registers.BitFraming, 0x00);
// Check if we found a card
return GetFifoLevel() == 2 && ReadFromFifoShort() == PiccResponses.AnswerToRequest;
}
public Uid ReadUid()
{
// Run the anti-collision loop on the card
Transceive(false, PiccCommands.Anticollision_1, PiccCommands.Anticollision_2);
// Return tag UID from FIFO
return new Uid(ReadFromFifo(5));
}
public void HaltTag()
{
// Transceive the Halt command to the tag
Transceive(false, PiccCommands.Halt_1, PiccCommands.Halt_2);
}
public bool SelectTag(Uid uid)
{
// Send Select command to tag
var data = new byte[7];
data[0] = PiccCommands.Select_1;
data[1] = PiccCommands.Select_2;
uid.FullUid.CopyTo(data, 2);
Transceive(true, data);
return GetFifoLevel() == 1 && ReadFromFifo() == PiccResponses.SelectAcknowledge;
}
internal byte[] ReadBlock(byte blockNumber, Uid uid, byte[] keyA = null, byte[] keyB = null)
{
if (keyA != null)
MifareAuthenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA);
else if (keyB != null)
MifareAuthenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB);
else
return null;
// Read block
Transceive(true, PiccCommands.Read, blockNumber);
return ReadFromFifo(16);
}
internal bool WriteBlock(byte blockNumber, Uid uid, byte[] data, byte[] keyA = null, byte[] keyB = null)
{
if (keyA != null)
MifareAuthenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA);
else if (keyB != null)
MifareAuthenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB);
else
return false;
// Write block
Transceive(true, PiccCommands.Write, blockNumber);
if (ReadFromFifo() != PiccResponses.Acknowledge)
return false;
// Make sure we write only 16 bytes
var buffer = new byte[16];
data.CopyTo(buffer, 0);
Transceive(true, buffer);
return ReadFromFifo() == PiccResponses.Acknowledge;
}
protected void MifareAuthenticate(byte command, byte blockNumber, Uid uid, byte[] key)
{
// Put reader in Idle mode
WriteRegister(Registers.Command, PcdCommands.Idle);
// Clear the FIFO
SetRegisterBits(Registers.FifoLevel, 0x80);
// Create Authentication packet
var data = new byte[12];
data[0] = command;
data[1] = (byte)(blockNumber & 0xFF);
key.CopyTo(data, 2);
uid.Bytes.CopyTo(data, 8);
WriteToFifo(data);
// Put reader in MfAuthent mode
WriteRegister(Registers.Command, PcdCommands.MifareAuthenticate);
// Wait for (a generous) 25 ms
System.Threading.Tasks.Task.Delay(25).Wait();
}
protected void Transceive(bool enableCrc, params byte[] data)
{
if (enableCrc)
{
// Enable CRC
SetRegisterBits(Registers.TxMode, 0x80);
SetRegisterBits(Registers.RxMode, 0x80);
}
// Put reader in Idle mode
WriteRegister(Registers.Command, PcdCommands.Idle);
// Clear the FIFO
SetRegisterBits(Registers.FifoLevel, 0x80);
// Write the data to the FIFO
WriteToFifo(data);
// Put reader in Transceive mode and start sending
WriteRegister(Registers.Command, PcdCommands.Transceive);
SetRegisterBits(Registers.BitFraming, 0x80);
// Wait for (a generous) 25 ms
System.Threading.Tasks.Task.Delay(25).Wait();
// Stop sending
ClearRegisterBits(Registers.BitFraming, 0x80);
if (enableCrc)
{
// Disable CRC
ClearRegisterBits(Registers.TxMode, 0x80);
ClearRegisterBits(Registers.RxMode, 0x80);
}
}
protected byte[] ReadFromFifo(int length)
{
var buffer = new byte[length];
for (int i = 0; i < length; i++)
buffer[i] = ReadRegister(Registers.FifoData);
return buffer;
}
protected byte ReadFromFifo()
{
return ReadFromFifo(1)[0];
}
protected void WriteToFifo(params byte[] values)
{
foreach (var b in values)
WriteRegister(Registers.FifoData, b);
}
protected int GetFifoLevel()
{
return ReadRegister(Registers.FifoLevel);
}
protected byte ReadRegister(byte register)
{
register <<= 1;
register |= 0x80;
var writeBuffer = new byte[] { register, 0x00 };
return TransferSpi(writeBuffer)[1];
}
protected ushort ReadFromFifoShort()
{
var low = ReadRegister(Registers.FifoData);
var high = (ushort)(ReadRegister(Registers.FifoData) << 8);
return (ushort)(high | low);
}
protected void WriteRegister(byte register, byte value)
{
register <<= 1;
var writeBuffer = new byte[] { register, value };
TransferSpi(writeBuffer);
}
protected void SetRegisterBits(byte register, byte bits)
{
var currentValue = ReadRegister(register);
WriteRegister(register, (byte)(currentValue | bits));
}
protected void ClearRegisterBits(byte register, byte bits)
{
var currentValue = ReadRegister(register);
WriteRegister(register, (byte)(currentValue & ~bits));
}
private byte[] TransferSpi(byte[] writeBuffer)
{
var readBuffer = new byte[writeBuffer.Length];
_spi.TransferFullDuplex(writeBuffer, readBuffer);
return readBuffer;
}
}
}
Schema
Since it uses SPI, there shouldn't be a hardware compatibility issue. If you don't want to translate existing Arduino code, Microsoft does have some tech that allows you to use existing Arduino sketches and libraries. You can read more about it here: http://ms-iot.github.io/content/en-US/win10/ArduinoWiringProjectGuide.htm
Addition: If you want to see the UID as a string representing the hex chars, you can use the following inside TagIsPresent part:
var uid = mfrc.ReadUid();
string txt_Result = "";
foreach (byte byt in uid.FullUid)
{
txt_Result = txt_Result + byt.ToString("x2");
}
mfrc.HaltTag();
I'm using a library that requires I provide an object that implements this interface:
public interface IConsole {
TextWriter StandardInput { get; }
TextReader StandardOutput { get; }
TextReader StandardError { get; }
}
The object's readers then get used by the library with:
IConsole console = new MyConsole();
int readBytes = console.StandardOutput.Read(buffer, 0, buffer.Length);
Normally the class implementing IConsole has the StandardOutput stream as coming from an external process. In that case the console.StandardOutput.Read calls work by blocking until there is some data written to the StandardOutput stream.
What I'm trying to do is create a test IConsole implementation that uses MemoryStreams and echo's whatever appears on the StandardInput back onto the StandardInput. I tried:
MemoryStream echoOutStream = new MemoryStream();
StandardOutput = new StreamReader(echoOutStream);
But the problem with that is the console.StandardOutput.Read will return 0 rather than block until there is some data. Is there anyway I can get a MemoryStream to block if there is no data available or is there a different in memory stream I could use?
Inspired by your answer, here's my multi-thread, multi-write version:
public class EchoStream : MemoryStream
{
private readonly ManualResetEvent _DataReady = new ManualResetEvent(false);
private readonly ConcurrentQueue<byte[]> _Buffers = new ConcurrentQueue<byte[]>();
public bool DataAvailable{get { return !_Buffers.IsEmpty; }}
public override void Write(byte[] buffer, int offset, int count)
{
_Buffers.Enqueue(buffer);
_DataReady.Set();
}
public override int Read(byte[] buffer, int offset, int count)
{
_DataReady.WaitOne();
byte[] lBuffer;
if (!_Buffers.TryDequeue(out lBuffer))
{
_DataReady.Reset();
return -1;
}
if (!DataAvailable)
_DataReady.Reset();
Array.Copy(lBuffer, buffer, lBuffer.Length);
return lBuffer.Length;
}
}
With your version you should Read the Stream upon Write, without any consecutively write be possible. My version buffers any written buffer in a ConcurrentQueue (it's fairly simple to change it to a simple Queue and lock it)
In the end I found an easy way to do it by inheriting from MemoryStream and taking over the Read and Write methods.
public class EchoStream : MemoryStream {
private ManualResetEvent m_dataReady = new ManualResetEvent(false);
private byte[] m_buffer;
private int m_offset;
private int m_count;
public override void Write(byte[] buffer, int offset, int count) {
m_buffer = buffer;
m_offset = offset;
m_count = count;
m_dataReady.Set();
}
public override int Read(byte[] buffer, int offset, int count) {
if (m_buffer == null) {
// Block until the stream has some more data.
m_dataReady.Reset();
m_dataReady.WaitOne();
}
Buffer.BlockCopy(m_buffer, m_offset, buffer, offset, (count < m_count) ? count : m_count);
m_buffer = null;
return (count < m_count) ? count : m_count;
}
}
I'm going to add one more refined version of EchoStream. This is a combination of the other two versions, plus some suggestions from the comments.
UPDATE - I have tested this EchoStream with over 50 terrabytes of data run through it for days on end. The test had it sitting between a network stream and the ZStandard compression stream. The async has also been tested, which brought a rare hanging condition to the surface. It appears the built in System.IO.Stream does not expect one to call both ReadAsync and WriteAsync on the same stream at the same time, which can cause it to hang if there isn't any data available because both calls utilize the same internal variables. Therefore I had to override those functions, which resolved the hanging issue.
This version has the following enhancements:
This was written from scratch using the System.IO.Stream base class instead of MemoryStream.
The constructor can set a max queue depth and if this level is reached then stream writes will block until a Read is performed which drops the queue depth back below the max level (no limit=0, default=10).
When reading/writing data, the buffer offset and count are now honored. Also, you can call Read with a smaller buffer than Write without throwing an exception or losing data. BlockCopy is used in a loop to fill in the bytes until count is satisfied.
There is a public property called AlwaysCopyBuffer, which makes a copy of the buffer in the Write function. Setting this to true will safely allow the byte buffer to be reused after calling Write.
There is a public property called ReadTimeout/WriteTimeout, which controls how long the Read/Write function will block before it returns 0 (default=Infinite, -1).
The BlockingCollection<> class is used, which under the hood combines the ConcurrentQueue and AutoResetEvent classes. Originally I was using these two classes, but there exists a rare condition where you will find that after data has been Enqueued( ), that it is not available immediately when AutoResetEvent allows a thread through in the Read( ). This happens about once every 500GB of data that passes through it. The cure was to Sleep and check for the data again. Sometimes a Sleep(0) worked, but in extreme cases where the CPU usage was high, it could be as high as Sleep(1000) before the data showed up. After I switched to BlockingCollection<>, it has a lot of extra code to handle off of this elegantly and without issues.
This has been tested to be thread safe for simultaneous async reads and writes.
using System;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;
public class EchoStream : Stream
{
public override bool CanTimeout { get; } = true;
public override int ReadTimeout { get; set; } = Timeout.Infinite;
public override int WriteTimeout { get; set; } = Timeout.Infinite;
public override bool CanRead { get; } = true;
public override bool CanSeek { get; } = false;
public override bool CanWrite { get; } = true;
public bool CopyBufferOnWrite { get; set; } = false;
private readonly object _lock = new object();
// Default underlying mechanism for BlockingCollection is ConcurrentQueue<T>, which is what we want
private readonly BlockingCollection<byte[]> _Buffers;
private int _maxQueueDepth = 10;
private byte[] m_buffer = null;
private int m_offset = 0;
private int m_count = 0;
private bool m_Closed = false;
private bool m_FinalZero = false; //after the stream is closed, set to true after returning a 0 for read()
public override void Close()
{
m_Closed = true;
// release any waiting writes
_Buffers.CompleteAdding();
}
public bool DataAvailable
{
get
{
return _Buffers.Count > 0;
}
}
private long _Length = 0L;
public override long Length
{
get
{
return _Length;
}
}
private long _Position = 0L;
public override long Position
{
get
{
return _Position;
}
set
{
throw new NotImplementedException();
}
}
public EchoStream() : this(10)
{
}
public EchoStream(int maxQueueDepth)
{
_maxQueueDepth = maxQueueDepth;
_Buffers = new BlockingCollection<byte[]>(_maxQueueDepth);
}
// we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once
public new Task WriteAsync(byte[] buffer, int offset, int count)
{
return Task.Run(() => Write(buffer, offset, count));
}
// we override the xxxxAsync functions because the default base class shares state between ReadAsync and WriteAsync, which causes a hang if both are called at once
public new Task<int> ReadAsync(byte[] buffer, int offset, int count)
{
return Task.Run(() =>
{
return Read(buffer, offset, count);
});
}
public override void Write(byte[] buffer, int offset, int count)
{
if (m_Closed || buffer.Length - offset < count || count <= 0)
return;
byte[] newBuffer;
if (!CopyBufferOnWrite && offset == 0 && count == buffer.Length)
newBuffer = buffer;
else
{
newBuffer = new byte[count];
System.Buffer.BlockCopy(buffer, offset, newBuffer, 0, count);
}
if (!_Buffers.TryAdd(newBuffer, WriteTimeout))
throw new TimeoutException("EchoStream Write() Timeout");
_Length += count;
}
public override int Read(byte[] buffer, int offset, int count)
{
if (count == 0)
return 0;
lock (_lock)
{
if (m_count == 0 && _Buffers.Count == 0)
{
if (m_Closed)
{
if (!m_FinalZero)
{
m_FinalZero = true;
return 0;
}
else
{
return -1;
}
}
if (_Buffers.TryTake(out m_buffer, ReadTimeout))
{
m_offset = 0;
m_count = m_buffer.Length;
}
else
{
if (m_Closed)
{
if (!m_FinalZero)
{
m_finalZero = true;
return 0;
}
else
{
return -1;
}
}
else
{
return 0;
}
}
}
int returnBytes = 0;
while (count > 0)
{
if (m_count == 0)
{
if (_Buffers.TryTake(out m_buffer, 0))
{
m_offset = 0;
m_count = m_buffer.Length;
}
else
break;
}
var bytesToCopy = (count < m_count) ? count : m_count;
System.Buffer.BlockCopy(m_buffer, m_offset, buffer, offset, bytesToCopy);
m_offset += bytesToCopy;
m_count -= bytesToCopy;
offset += bytesToCopy;
count -= bytesToCopy;
returnBytes += bytesToCopy;
}
_Position += returnBytes;
return returnBytes;
}
}
public override int ReadByte()
{
byte[] returnValue = new byte[1];
return (Read(returnValue, 0, 1) <= 0 ? -1 : (int)returnValue[0]);
}
public override void Flush()
{
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
}
UPDATE: this works in .NET 4.8, but the behavior was changed in .NET Core and it no longer blocks the same way.
An anonymous pipe stream blocks like a file stream and should handle more edge cases than the sample code provided.
Here is a unit test that demonstrates this behavior.
var cts = new CancellationTokenSource();
using (var pipeServer = new AnonymousPipeServerStream(PipeDirection.Out))
using (var pipeStream = new AnonymousPipeClientStream(PipeDirection.In, pipeServer.ClientSafePipeHandle))
{
var buffer = new byte[1024];
var readTask = pipeStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
Assert.IsFalse(readTask.IsCompleted, "Read already complete");
// Cancelling does NOT unblock the read
cts.Cancel();
Assert.IsFalse(readTask.IsCanceled, "Read cancelled");
// Only sending data does
pipeServer.WriteByte(42);
var bytesRead = await readTask;
Assert.AreEqual(1, bytesRead);
}
Here's my take on the EchoStream posted above. It handles the offset and count parameters on Write and Read.
public class EchoStream : MemoryStream
{
private readonly ManualResetEvent _DataReady = new ManualResetEvent(false);
private readonly ConcurrentQueue<byte[]> _Buffers = new ConcurrentQueue<byte[]>();
public bool DataAvailable { get { return !_Buffers.IsEmpty; } }
public override void Write(byte[] buffer, int offset, int count)
{
_Buffers.Enqueue(buffer.Skip(offset).Take(count).ToArray());
_DataReady.Set();
}
public override int Read(byte[] buffer, int offset, int count)
{
_DataReady.WaitOne();
byte[] lBuffer;
if (!_Buffers.TryDequeue(out lBuffer))
{
_DataReady.Reset();
return -1;
}
if (!DataAvailable)
_DataReady.Reset();
Array.Copy(lBuffer, 0, buffer, offset, Math.Min(lBuffer.Length, count));
return lBuffer.Length;
}
}
I was able to use this class to unit test a System.IO.Pipelines implementation. I needed a MemoryStream that could simulate multiple read calls in succession without reaching the end of the stream.
I was trying to use all the codes from other answers, as well as the famous EchoStream, but unfortunately, they all were not working as I need:
EchoStream stream doesn't work well with non-standard read and write sizes, causing loss of data and corrupted reads.
EchoStream limits the stream by the number of writes, but not by the count, so theoretically someone can write in tons of data.
Solution:
I've created a ThroughStream, which is limited by any specified exact buffer size. The actual size might grow up to bufferSize * 2, but not larger than that.
It works perfectly with any non-standard size reads and writes, doesn't fail in multithreading, and is quite simple and optimized.
And is available on Gist! (click here)