@ leforban -
Sorry for the delay. First priority has been babysitting my grandchildren.
Anyway, I threw together some code using MemoryStream and Seek.
I’m not sure what you really need but the code ‘may’ get you started?
I am NOT a professional programmer… I just hack away until something ‘seems to work’.
I am not saying this is the ‘correct’ way to do it, just my way on the first try.
Using a Fez Spider with a CP7 display and a button so you will need to change the code for your needs.
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using MSPC = Microsoft.SPOT.Presentation.Media;
using System.IO;
using System.Text;
namespace GadgeteerAppSeekTest
{
public partial class Program
{
Font MyFontB = Resources.GetFont(Resources.FontResources.NinaB); //Big font to use
Font MyFontS = Resources.GetFont(Resources.FontResources.small); //Small font to use
// This method is run when the mainboard is powered up or reset.
void ProgramStarted( )
{
Startbutton.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(Startbutton_ButtonPressed);
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started. Press start button.");
display_CP7.SimpleGraphics.DisplayText("Press start button.", MyFontB, MSPC.Colors.White, 1, 1);
}
private void Startbutton_ButtonPressed(Button sender, Button.ButtonState state)
{
string teststring1 = "This is the first string.";
string teststring2 = "This is the second string.";
string teststring3 = ","; // decimal 44 - Hex 2C
string teststring4 = " "; // decimal 32 - Hex 20
String bytesconverted = String.Empty;
Debug.Print("Test strings to place into MemoryStream");
Debug.Print(teststring1 + " Length = " + teststring1.Length.ToString());
Debug.Print(teststring2 + " Length = " + teststring2.Length.ToString());
Debug.Print(teststring3 + " Length = " + teststring3.Length.ToString());
byte[] byteArray1 = System.Text.Encoding.UTF8.GetBytes(teststring1);
byte[] byteArray2 = System.Text.Encoding.UTF8.GetBytes(teststring4 + teststring2);
byte[] byteArray3 = System.Text.Encoding.UTF8.GetBytes(teststring3);
byte[] AReadBuffer = new byte[256];
int BytesRead = 0;
int BytesToRead = 0;
//Expandable memory stream, create MemoryStream instance without parameterized constructor.
MemoryStream AMemoryStream = null;
AMemoryStream = new MemoryStream();
bool canread = AMemoryStream.CanRead;
Debug.Print("canread = " + canread);
bool canwrite = AMemoryStream.CanWrite;
Debug.Print("canwrite = " + canwrite);
bool canseek = AMemoryStream.CanSeek;
Debug.Print("canseek = " + canseek);
bool cantimeout = AMemoryStream.CanTimeout;
Debug.Print("cantimeout = " + cantimeout);
//Exception System.ArgumentException
//if you do not Seek to the beginning of the memory buffer before a new write
AMemoryStream.Seek(0, SeekOrigin.Begin);
AMemoryStream.Write(byteArray1, 0, teststring1.Length);
AMemoryStream.Flush();
//Return to origin before read
AMemoryStream.Seek(0, SeekOrigin.Begin);
BytesRead = AMemoryStream.Read(AReadBuffer, 0, (int)AMemoryStream.Length);
bytesconverted = new String(System.Text.Encoding.UTF8.GetChars(AReadBuffer));
Debug.Print("1) MemoryStream: " + bytesconverted);
display_CP7.SimpleGraphics.DisplayText("1) MemoryStream: " + bytesconverted, MyFontS, MSPC.Colors.White, 1, 20);
AMemoryStream.Seek(0, SeekOrigin.Current);
AMemoryStream.Write(byteArray2, 0, teststring2.Length);
AMemoryStream.Flush();
Array.Clear(AReadBuffer, 0, 256);
AMemoryStream.Seek(0, SeekOrigin.Begin);
BytesToRead = BytesRead;
BytesRead = AMemoryStream.Read(AReadBuffer, 0, (int)AMemoryStream.Length);
bytesconverted = new String(System.Text.Encoding.UTF8.GetChars(AReadBuffer));
Debug.Print("2) MemoryStream: " + bytesconverted);
display_CP7.SimpleGraphics.DisplayText("2) MemoryStream: " + bytesconverted, MyFontS, MSPC.Colors.White, 1, 30);
Array.Clear(AReadBuffer, 0, 256);
BytesToRead = BytesRead;
//Go to position 47 in memory
AMemoryStream.Seek(0, SeekOrigin.Begin);
//Writes a , at AMemoryStream buffer[0]. This overwrites the current data
AMemoryStream.Write(byteArray3, (int)AMemoryStream.Position, teststring3.Length);
AMemoryStream.Seek(0, SeekOrigin.Begin);
BytesRead = AMemoryStream.Read(AReadBuffer, 0, (int)AMemoryStream.Length);
bytesconverted = new String(System.Text.Encoding.UTF8.GetChars(AReadBuffer));
Debug.Print("3) MemoryStream comma written to pos[0]> " + bytesconverted);
display_CP7.SimpleGraphics.DisplayText("3) MemoryStream comma written to pos[0]> " + bytesconverted, MyFontB, MSPC.Colors.White, 1, 50);
Array.Clear(AReadBuffer, 0, 256);
//Go to position 47 in memory
AMemoryStream.Seek(47, SeekOrigin.Begin);
//Writes a , at AMemoryStream buffer[47]. This overwrites the current data
AMemoryStream.Write(byteArray3, 0, byteArray3.Length);
AMemoryStream.Seek(0, SeekOrigin.Begin);
BytesRead = AMemoryStream.Read(AReadBuffer, 0, (int)AMemoryStream.Length);
bytesconverted = new String(System.Text.Encoding.UTF8.GetChars(AReadBuffer));
Debug.Print("4) MemoryStream comma written to pos[47]> " + bytesconverted);
display_CP7.SimpleGraphics.DisplayText("4) MemoryStream comma written to pos[47]> " + bytesconverted, MyFontB, MSPC.Colors.White, 1, 64);
display_CP7.SimpleGraphics.DisplayText("See new comma in each string", MyFontB, MSPC.Colors.White, 1, 76);
}
}
}
End of code
Debug output from VS2010
Using mainboard GHI Electronics FEZSpider version 1.0
Program Started. Press start button.
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
*
Test strings to place into MemoryStream
This is the first string. Length = 25
This is the second string. Length = 26
, Length = 1
*
canread = True
canwrite = True
canseek = True
cantimeout = False
*
1) MemoryStream: This is the first string.
2) MemoryStream: This is the first string. This is the second string
3) MemoryStream comma written to pos[0]> ,his is the first string. This is the second string
4) MemoryStream comma written to pos[47]> ,his is the first string. This is the second st,ing