Test and Measurement Forum

calling th-03 from c#

Post your .Net discussions here

calling th-03 from c#

Postby Charly » Thu Dec 03, 2009 11:14 am

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
Charly
 

Re: calling th-03 from c#

Postby Erik » Mon Dec 07, 2009 3:36 pm

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?
Erik
PICO STAFF
PICO STAFF
 
Posts: 52
Joined: Mon Oct 23, 2006 8:16 am
Location: Cambridgeshire, UK

Re: calling th-03 from c#

Postby Charly » Tue Dec 15, 2009 5:21 pm

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;
}
}
Charly
 

Re: calling th-03 from c#

Postby Erik » Wed Dec 16, 2009 8:41 am

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.
Erik
PICO STAFF
PICO STAFF
 
Posts: 52
Joined: Mon Oct 23, 2006 8:16 am
Location: Cambridgeshire, UK

Re: calling th-03 from c#

Postby Charly » Fri Dec 18, 2009 5:57 pm

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.
Charly
 


Return to .Net Languages (C#, VB.Net, .)

Who is online

Users browsing this forum: No registered users and 0 guests