Unable to download Accelerometer log data on Windows

Board Information:
Hardware revision: 0.4
Firmware revision: 1.4.4
Model number: 5
Host Device Information: Laptop with Windows 10 x64, version 1903
SDK: I am using Visual Studio 2017, version 15.9.17

I am trying to connect to my MMR sensor by creating a console application on VS 2017.
1. The sensor hardly connects like 2 times out of 10 tries.
2. I am trying the log the data and then download but I am getting exception on download.
3. My requirement is to log the data to sql database, so how can I do it ?

Below is the code I have tried, kindly provide your valuable guidelines
`
namespace SmartGym
{
class StreamAccelerometer
{
internal static async Task ConnectToDevice(string mac)
{
Console.WriteLine($"Started Connection class..");
var m = await ScanConnect.Connect(ScanForMetaWear());

        m.GetModule<ISettings>()?.EditBleConnParams(maxConnInterval: 7.5f);
        await Task.Delay(1500);

        Console.WriteLine($"Connected to {mac}");
        return m;
    }

    internal static async Task<IMetaWearBoard> PrepareBleConn(string mac)
    {
        throw new NotImplementedException();
    }

    internal static async Task Setup(IMetaWearBoard metawear, Dictionary<IMetaWearBoard, uint> samples)
    {
        var acc = metawear.GetModule<IAccelerometer>();

        acc.Configure(odr: 100f, range: 16f);

        Console.WriteLine($"Clearing logs data..");
        var debug = metawear.GetModule<IDebug>();
        debug.ResetAfterGc();

        await acc.Acceleration.AddRouteAsync(source => source.Log(data => {
            Console.WriteLine($"{metawear.MacAddress} -> {data.Value<Acceleration>()}");
            samples[metawear]++;
        }));

        // Tell firmware to start logging
        metawear.GetModule<ILogging>().Start();
        acc.Acceleration.Start();
        acc.Start();


    }

    static async Task DownloadData(IMetaWearBoard metawear)
    {
        var logging = metawear.GetModule<ILogging>();
        var acc = metawear.GetModule<IAccelerometer>();

        acc.Stop();
        acc.Acceleration.Stop();
        logging.Stop();

        Console.WriteLine("Creating anonymous routes");
        var routes = await metawear.CreateAnonymousRoutesAsync();

        Console.WriteLine($"{routes.Count} active loggers discovered");
        foreach (var r in routes)
        {
            r.Subscribe(_ =>
                Console.WriteLine($"identifier: {r.Identifier}, time: {_.FormattedTimestamp}, data: [{BitConverter.ToString(_.Bytes).ToLower().Replace("-", ", 0x")}]")
            );
        }

        metawear.GetModule<ISettings>().EditBleConnParams(maxConnInterval: 7.5f);
        await Task.Delay(1500);
        await logging.DownloadAsync();
    }

    internal static void StartStream(IMetaWearBoard metawear)
    {
        var acc = metawear.GetModule<IAccelerometer>();
        acc.Acceleration.Start();
        acc.Start();


        throw new NotImplementedException();
    }

    internal static Task StopStream(IMetaWearBoard metawear)
    {
        var acc = metawear.GetModule<IAccelerometer>();
        acc.Stop();
        acc.Acceleration.Stop();

        // Have remote device close the connection
        return metawear.GetModule<IDebug>().DisconnectAsync();
    }
    public static string ScanForMetaWear()
    {
        var devices = new List<ScanResult>();
        var seen = new HashSet<string>();
        // Set a handler to process scanned devices
        Scanner.OnResultReceived = item => {
            // Filter devices that do not advertise the MetaWear service or have already been seen
            if (item.HasServiceUuid(Constants.METAWEAR_GATT_SERVICE.ToString()) && !seen.Contains(item.Mac))
            {
                seen.Add(item.Mac);

                Console.WriteLine($"[{devices.Count}] = {item.Mac} ({item.Name})");
                devices.Add(item);
            }
        };
        int selection;
        do
        {
            seen.Clear();
            devices.Clear();

            Console.WriteLine("Scanning for devices...");
            Scanner.Start();

            Console.WriteLine("Press [Enter] to stop the scan");
            Console.ReadLine();
            Scanner.Stop();

            Console.Write("Select your device (-1 to rescan): ");
            selection = int.Parse(Console.ReadLine());
            // Repeat until user selects a device
        } while (selection == -1);

        // return selected mac address
        return devices[selection].Mac;

        throw new NotImplementedException();
    }


    static async Task RunAsync(string[] args)
    {
        var metawear = await ConnectToDevice(args[0]);
        //var metawear = await SacnConnect Connect(ScanForMetaWear());
        var samples = new Dictionary<IMetaWearBoard, uint> {
            { metawear, 0 }
        };

        // Connect and prepare the BLE connection
        await Setup(metawear, samples);
       // StartStream(metawear);

        // Stream for 30
        await Task.Delay(15000);


        Console.WriteLine("Downloading data");
        await DownloadData(metawear);
        // Stop accelerometer and disconnect from the board
        //await StopStream(metawear);

        Console.WriteLine("Resetting device...");
        await metawear.GetModule<IDebug>().ResetAsync();

        foreach (var (k, v) in samples)
        {
            Console.WriteLine($"{k.MacAddress} -> {v}");
        }
    }
}

}
`

Sign In or Register to comment.