[edit]: changes done according to Gus Comments[\edit]
Hi Everyone
Our application need to exchange data between EMX custom board and a computer. The problem is that the CDC seems to stall after a while. To investigate, I have created a test code that send a xml file to the computer:
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using GHI.Premium.Hardware;
using GHI.Premium.USBClient;
using GHI.Premium.IO;
namespace EMX_Application1
{
public class Program
{
// Hold a static reference in case the GC kicks in and disposes it automatically, note that we only support one in this example!
static PersistentStorage ps;
public static void Main()
{
// Subscribe to RemovableMedia events
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
// Assume SD card is inserted
// Create a new storage device
ps = new PersistentStorage("SD");
ps.MountFileSystem();
Debug.Print(Resources.GetString(Resources.StringResources.String1));
// Start CDC
USBC_CDC cdc = USBClientController.StandardDevices.StartCDC();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello world!\r\n");
int iter = 0;
while(true)
{
iter++;
// Check if connected to PC
if(USBClientController.GetState() !=
USBClientController.State.Running)
{
Debug.Print("Waiting to connect to PC...");
}
else
{
Debug.Print("Iter number" + iter);
Thread.Sleep(1000);
cdc.Write(bytes,0,bytes.Length);
FileStream f = new FileStream("\\SD\\yourfile.txt",FileMode.Open,FileAccess.Read,FileShare.None,8);
long bytesRemaining;
int bytesRead;
byte[] data;
bytesRemaining = f.Length;
Thread.Sleep(1);
while(bytesRemaining > 0)
{
data = new byte[(bytesRemaining > 64) ? 64 : bytesRemaining];
bytesRead = f.Read(data,0,data.Length);
cdc.Write(data,0,data.Length);
bytesRemaining -= bytesRead;
}
f.Close();
f.Dispose();
}
Thread.Sleep(1);
}
}
static void RemovableMedia_Eject(object sender,MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is ejected.");
}
static void RemovableMedia_Insert(object sender,MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory + "\" is inserted.");
Debug.Print("Getting files and folders:");
if(e.Volume.IsFormatted)
{
string[] files = Directory.GetFiles(e.Volume.RootDirectory);
string[] folders = Directory.GetDirectories(e.Volume.RootDirectory);
Debug.Print("Files available on " + e.Volume.RootDirectory + ":");
for(int i = 0;i < files.Length;i++)
Debug.Print(files[i]);
Debug.Print("Folders available on " + e.Volume.RootDirectory + ":");
for(int i = 0;i < folders.Length;i++)
Debug.Print(folders[i]);
}
else
{
Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
}
}
}
}
Does anyone can help in finding a solution to this problem?