I’m back again with a problem. :’(
I know I’ve already created a topic like this a few months ago.
The problem is, when serialport is declared as STATIC. The garbage collector would ignore the serialport and the serialport ends up consuming all memory of the FEZ.
The workaround I did was to delete the static declaration and declared the serialport on each class that needs it. But as a consequence serialport.datareceived event would not fire.
I’ve attached some of the parts of the program.
class Program
{
static SerialPort Serial1;
static PersistentStorage ps;
static string finalfile;
static byte[] chbyte = new byte[1] { 0 };
static string myString;
static System.Text.Encoding enc = System.Text.Encoding.UTF8;
public static void Main()
{
Debug.GC(true);
Serial1 = new SerialPort("COM1");
Serial1.BaudRate = 115200;
Serial1.Parity = Parity.None;
Serial1.StopBits = StopBits.One;
Serial1.DataBits = 8;
Serial1.Handshake = Handshake.None;
Serial1.Open();
Debug.Print(DateTime.Now.ToString());
Serial1.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
USBHostController.DeviceDisconnectedEvent += DeviceDisconnectedEvent;
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
Thread.Sleep(1000);
ps = new PersistentStorage(device);
ps.MountFileSystem();
}
}
private static void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Debug.Print("DataReceived Event");
try
{
Serial1.Read(chbyte, 0, 1);
Thread.Sleep(200);
myString = new string(enc.GetChars(chbyte));
}
catch
{
}
if (string.Compare(myString, "r") == 0)
{
Debug.Print("receiving file from the computer");
receivefile();
}
if (string.Compare(myString, "s") == 0)
{
Debug.Print("sending file to the computer");
byte[] name = new byte[100];
Serial1.Read(name, 0, 100);
string myfile = new string(enc.GetChars(name));
finalfile = myfile.Trim();
Sendfile();
}
else if (string.Compare(myString, "d") == 0)
{
Debug.Print("Sending filenames");
fileexplore();
}
else if (string.Compare(myString, "p") == 0)
{
Debug.Print("ping");
byte[] ping = new byte[1];
ping = enc.GetBytes("p");
Serial1.Write(ping, 0, 1);
}
}
static void Sendfile()
{
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
string originalFile = rootDirectory + @ "\" + finalfile;
FileStream fs1 = new FileStream(originalFile, FileMode.Open, FileAccess.Read);
byte[] end1 = new byte[2];
int xx = 0, bytes = 6000;
string size;
byte[] filesize = new byte[20];
size = fs1.Length.ToString();
size = String.Concat(size, " ");
size = size.Substring(0, 20);
filesize = enc.GetBytes(size);
if (fs1.Length > bytes)
{
while (xx < (fs1.Length / bytes))
{
byte[] ByteArray = new byte[bytes];
fs1.Read(ByteArray, 0, bytes);
Thread.Sleep(500);
Serial1.Write(ByteArray, 0, bytes);
xx++;
if (xx == ((fs1.Length / bytes) - 1))
{
fs1.Read(ByteArray, 0, ((int)fs1.Length % bytes));
Serial1.Write(ByteArray, 0, ((int)fs1.Length % bytes));
}
}
}
if (fs1.Length <= 5000)
{
byte[] ByteArray = new byte[fs1.Length];
fs1.Read(ByteArray, 0, (int)fs1.Length);
int arraySize = (int)fs1.Length;
Serial1.Write(ByteArray, 0, arraySize);
}
end1 = enc.GetBytes("\r\n");
Serial1.Write(end1, 0, 2);
Thread.Sleep(2000);
Debug.Print("Fs1 closed");
fs1.Close();
// Compare size of original an new file
FileInfo fileInfo1 = new FileInfo(originalFile);
Debug.Print("Size of original file: " +
fileInfo1.Length.ToString());
}
}
[quote]GC: 3msec 63024 bytes used, 1356 bytes available
Type 0F (STRING ): 636 bytes
Type 11 (CLASS ): 4104 bytes
Type 12 (VALUETYPE ): 108 bytes
Type 13 (SZARRAY ): 8412 bytes
Type 15 (FREEBLOCK ): 1356 bytes
Type 17 (ASSEMBLY ): 17112 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 828 bytes
Type 1C (DELEGATELIST_HEAD ): 96 bytes
Type 1D (OBJECT_TO_EVENT ): 336 bytes
Type 1E (BINARY_BLOB_HEAD ): 25920 bytes
Type 1F (THREAD ): 1152 bytes
Type 20 (SUBTHREAD ): 144 bytes
Type 21 (STACK_FRAME ): 1404 bytes
Type 27 (FINALIZER_HEAD ): 528 bytes
Type 31 (IO_PORT ): 504 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1596 bytes
Failed allocation for 10 blocks, 120 bytes[/quote]