calling th-03 from c#

Post your .Net discussions here
Post Reply
Charly

calling th-03 from c#

Post by Charly »

Hi
Could you tell me the correct declarations to use with th-03 for calling from c# (WPF application).

I currently have it defined as:

[DllImport("th0332.dll", ExactSpelling = true, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.StdCall)]
static extern unsafe short th03_get_temp(
out int temp,
short port,
short channel,
short filtered);

and I call it using:

Int32 temp;
th03_get_temp(out temp, 1, 1, 1);

I get the error "Attempt to read or write protected memory" when I call th03_get_temp.

Do you have the C# interface definitions for th03 interface, or failing having the full set, could you help on the above call?

Thanks for your help.

Charly

Erik
PICO STAFF
PICO STAFF
Posts: 53
Joined: Mon Oct 23, 2006 8:16 am
Location: Cambridgeshire, UK

Re: calling th-03 from c#

Post by Erik »

I don't believe there is a problem with your import.

Could you put all your driver calls up to this point in a simple console application. If that can reproduce your problem, could you please attach that .cs file to this thread?

Charly

Re: calling th-03 from c#

Post by Charly »

Hi, here is a C# console showing the problem I hit.
Thanks for your help.
regards
Charly
____________________________________________________

using System;
using System.Runtime.InteropServices;

namespace th03
{
class Program
{
static void Main(string[] args)
{
picoTh3 th3 = new picoTh3();
// open the device
if (!th3.Open())
{
Console.WriteLine("Error: initialising Pico device");
return;
}

Console.WriteLine("Press any key to stop..");
while (Console.KeyAvailable == false)
{
// try to read the temp
int temp = 0;
try
{
temp = th3.GetTemp(0);
}
catch (Exception ex)
{
// show any exceptions
Console.WriteLine(ex.Message + "\n\n\n" + ex.StackTrace);
}
// show the temp
Console.WriteLine(String.Format("temp = {0:0}", temp));
// sleep for a sec
System.Threading.Thread.Sleep(1000);
}
}
}

class picoTh3
{
[DllImport("th0332.dll",
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
static extern unsafe short th03_open_unit(
short port
);

[DllImport("th0332.dll",
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
static extern unsafe short th03_close_unit(
short port
);

[DllImport("th0332.dll",
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
static extern unsafe void th03_set_channel(
short port,
short channel,
short sensor_type,
short filter_factor);

[DllImport("th0332.dll",
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
static extern unsafe short th03_get_temp(
out int temp,
short port,
short channel,
short filtered);

public bool Open()
{
if (m_bOpened)
return m_bOpened;
short iRet = th03_open_unit(TH03_PORT);
m_bOpened = (0 != iRet);
if (m_bOpened)
{
// set thermistor type and filtering for each channel
for (short c = 0; c < TH03_MAX_CHANNELS; c++)
{
th03_set_channel(TH03_PORT, (short)(c + 1), 1, 10);
}
}
return m_bOpened;
}

public void Close(short port)
{
if (m_bOpened)
th03_close_unit(TH03_PORT);
m_bOpened = false;
}

// read temp from channel, return 0 on error
public unsafe int GetTemp(short channel)
{
if (!m_bOpened)
return 0;
Int32 temp;
th03_get_temp(out temp, TH03_PORT, channel, 1);
return temp;
}


public bool IsOpen()
{
return m_bOpened;
}

private const short TH03_PORT = 1;
private const short TH03_MAX_CHANNELS = 3;
private bool m_bOpened = false;
}
}

Erik
PICO STAFF
PICO STAFF
Posts: 53
Joined: Mon Oct 23, 2006 8:16 am
Location: Cambridgeshire, UK

Re: calling th-03 from c#

Post by Erik »

Found your problem.
The index for the channels starts on 1, not 0.

Change: temp = th3.GetTemp(0);
to: temp = th3.GetTemp(1);

The driver should not throw an exception when passing in 0 so we will have to fix that at some point.

Let me know if you have any other problems.

Charly

Re: calling th-03 from c#

Post by Charly »

Thats solved it.
I should have spotted it myself, but I had mistakenly gone down the route of thinking it was the interop I'd got wrong.
Thanks for your help.

Post Reply