openyou / libomron

Libraries for accessing data from Omron medical devices

Home Page:http://qdot.github.com/libomron

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

C# DLLImports for omron.h

gRoberts84 opened this issue · comments

Hey there,

A while back I tried getting this to port to C# so I can access my 790/M10-IT?

I managed to get my own code working but I'm now looking to improve on it and make it more stable. Your code works great using C/C++, but I need to use it in C#.

Using the omron.dll that's created when building the project that cmake creates, I have created my own DLLImports but it's not working as expected and unfortunately, my PInvoke generator is moaning on about missing header files.

Apparently in cmake you can create the correct output but that fails for me as well.

Could you take a look at my class and see where I'm going wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace OmronLibTest
{
    class DLLWrapper
    {
    public struct omron_device_impl
    {
        public IntPtr _dev;
        public int _is_open;
        public int _is_inited;
    }

    public const int OMRON_VID = 0x0590;
    public const int OMRON_PID = 0x0028;

    public const int OMRON_OUT_ENDPT = 0x02;
    public const int OMRON_IN_ENDPT = 0x81;

    public enum omron_mode
    {
        NULL_MODE = 0x0000,
        DEVICE_INFO_MODE = 0x1111,
        DAILY_INFO_MODE = 0x74bc,
        WEEKLY_INFO_MODE = 0x1074,
        PEDOMETER_MODE = 0x0102
    }

    public struct omron_device
    {
        public omron_device_impl device;
        public omron_mode device_mode;
    }

    public struct omron_device_info
    {
        public byte[] version;
        public byte[] prf;
        public byte[] srl;
    }

    public struct omron_bp_day_info
    {
        public string day;
        public string month;
        public string year;
        public string hour;
        public string minute;
        public string second;
        public byte[] unknown_1;
        public string sys;
        public string dia;
        public string pulse;
        public byte[] unknown_2;
        public byte present;
    }

    public struct omron_bp_week_info
    {
        public byte unknown_1;
        public byte unknown_2;
        public string year;
        public string month;
        public string day;
        public byte unknown_3;
        public int sys;
        public int dia;
        public int pulse;
        public byte present;
    }

    public struct omron_pd_profile_info
    {
        public byte[] unknown_1;
        public string weight;
        public string stride;
        public byte[] unknown_2;
    }

    public struct omron_pd_count_info
    {
        public int daily_count;
        public int hourly_count;
        public byte unknown_1;
    }

    public struct omron_pd_daily_data
    {
        public int total_steps;
        public int total_aerobic_steps;
        public int total_walking_time;
        public int total_calories;
        public float total_distance;
        public float total_fat_burn;
        public int day_serial;
        public byte unknown_1;
    }

    public struct omron_pd_hourly_data
    {
        public int day_serial;
        public int hour_serial;
        public byte is_attached;
        public int regular_steps;
        public int aerobic_steps;
    }


    [DllImport("omron.dll")]
    public static extern omron_device omron_create();

    [DllImport("omron.dll")]
    public static extern void omron_delete(omron_device dev);

    [DllImport("omron.dll")]
    public static extern int omron_get_count(omron_device dev, int VID, int PID);

    [DllImport("omron.dll")]
    public static extern int omron_open(omron_device dev, int VID, int PID, UInt32 device_index);

    [DllImport("omron.dll")]
    public static extern int omron_close(omron_device dev);

    [DllImport("omron.dll")]
    public static extern int omron_set_mode(omron_device dev, omron_mode mode);

    [DllImport("omron.dll")]
    public static extern int omron_read_data(omron_device dev, ref byte[] input_report);

    [DllImport("omron.dll")]
    public static extern int omron_write_data(omron_device dev, ref byte[] output_report);

    [DllImport("omron.dll")]
    public static extern int omron_get_device_serial(omron_device dev, ref byte[] data);

    [DllImport("omron.dll")]
    public static extern int omron_get_device_version(omron_device dev, ref byte[] data);

    [DllImport("omron.dll")]
    public static extern int omron_get_bp_profile(omron_device dev, ref byte[] data);

    [DllImport("omron.dll")]
    public static extern omron_bp_day_info omron_get_daily_bp_data(omron_device dev, int bank, int index);

    [DllImport("omron.dll")]
    public static extern omron_bp_week_info omron_get_weekly_bp_data(omron_device dev, int bank, int index, int evening);

    [DllImport("omron.dll")]
    public static extern omron_pd_profile_info omron_get_pd_profile(omron_device dev);

    [DllImport("omron.dll")]
    public static extern omron_pd_count_info omron_get_pd_data_count(omron_device dev);

    [DllImport("omron.dll")]
    public static extern int omron_get_daily_data_count(omron_device dev, int bank);

    [DllImport("omron.dll")]
    public static extern omron_pd_daily_data omron_get_pd_daily_data(omron_device dev, int day);

    [DllImport("omron.dll")]
    public static extern omron_pd_hourly_data omron_get_pd_hourly_data(omron_device dev, int day);
    }
}

Below is the code i'm using to call the above class/code.

DLLWrapper.omron_device dev = DLLWrapper.omron_create();
int ret;
byte[] version = new byte[30];
byte[] prf = new byte[30];

ret = DLLWrapper.omron_get_count(dev, DLLWrapper.OMRON_VID, DLLWrapper.OMRON_PID);
if (ret == 0)
{
    MessageBox.Show("Please ensure you have the device connected!");
    return;
}
ret = DLLWrapper.omron_open(dev, DLLWrapper.OMRON_VID, DLLWrapper.OMRON_PID, 0);
if (ret < 0)
{
    MessageBox.Show("Failed to connect to the device!");
    return;
}
ret = DLLWrapper.omron_get_device_version(dev, ref version);
if (ret < 0)
{
    MessageBox.Show("Failed to get the device version!");
    return;
}
ret = DLLWrapper.omron_get_bp_profile(dev, ref prf);
if (ret < 0)
{
    MessageBox.Show("Failed to get the device profile!");
    return;
}
ret = DLLWrapper.omron_get_daily_data_count(dev, (radioButton1.Checked ? 0 : 1));
if (ret < 0)
{
    MessageBox.Show("There was an issue getting the data from the device!");
    return;
}
for (int i = ret - 1; i >= 0; i--)
{
    DLLWrapper.omron_bp_day_info data = DLLWrapper.omron_get_daily_bp_data(dev, (radioButton1.Checked ? 0 : 1), i);
    if (data.present == null)
    {
        i = i + 1;
        continue;
    }
    listBox1.Items.Add(string.Format("{0}/{1}/20{2} {3}:{4}:{5} SYS: {6} DIA: {7} PULSE: {8}", data.day, data.month, data.year, data.hour, data.minute, data.second, data.sys, data.dia, data.pulse));
}
ret = DLLWrapper.omron_close(dev);

omron_get_count seems to already return 0, even though it works fine using your 790 test app.

Any ideas?

Cheers

Gavin

commented

Well, that looks sane to me (though I haven't worked in C# since like, 2005 :) ). I'm not sure why it wouldn't be able to pull a count correctly when using PInvoke versus just regularly accessing the DLL.

Thanks for getting back ;)

I've got a feeling my conversions from your header file to my c# wrapper is the cause.

I haven't used c/c++ so have no idea what uints etc convert to. Do you know a quick/simple piece of code that'll output some debug text when the dllimport calls your header methods? That way I can play and see what I can find out?

Cheers

Ooops closed it by mistake, sorry

Hi Robert, that works for you, i'm trying to do it but is defficult for me i use a program to comunicate to the 791 but never responds at last works for you, help me please

i can get the omrom dll where did you find it?