ADC-16 adc_16_get_value problem

Post general discussions on using our drivers to write your own software here
Post Reply
Guest

ADC-16 adc_16_get_value problem

Post by Guest »

I'm developing my own program for ADC-16 datalogger and having problems with getting data from it. Calling adc_16_get_value function in program causes the following error:

Run-Time Chack Failure #0 - The value of ESP was not properly saved across a function call.
This is usually a result of calling a function declared with one calling convention with a function pointer with a different calling convention.

I'm using Microsoft Visual Studio 2005 & Win XP

#include "stdafx.h"
#include
#include
#include
#include
#include

typedef int (_stdcall *Openptr)(short int port);
typedef int (_stdcall *Closeptr)(unsigned short int port);
typedef int (_stdcall *Valueptr)(long far * value, unsigned short int port, int resolution, short int channel);
typedef int (_stdcall *FValueptr) (long far * value, unsigned short int port, short int channel);
typedef int (_stdcall *GCycleptr) (long far * cycle, short int port);
typedef int (_stdcall *Versionptr)(short int far * version, short int port);
typedef void (_stdcall *Pollptr)(void);
typedef void (_stdcall *SetChannelptr) (unsigned short int port, unsigned short int channel,unsigned short int resolution,
unsigned short int single_ended, unsigned short int filter_factor);

int _tmain(int argc, _TCHAR* argv[])
{

HINSTANCE hLib;

Openptr DOpen;
Closeptr DClose;
Valueptr Valget;
Versionptr Vget;
SetChannelptr SetCh;
FValueptr FValue;
Pollptr Poll;
GCycleptr GCycle;

short int version; // Version of ADC16
unsigned short int port=1; // 1= COM1, 2= COM2 etc.
unsigned short int channel=1; // Current channel
unsigned short int resolution=8; // A/D resolution (bits)
unsigned short int mode=true; // False for differential mode, True = single ended
unsigned short int ffactor=1; // Filtering factor
long value;
long cycle;
int i;

/*Load .dll library*/
hLib = LoadLibrary(TEXT("adc1632.dll"));

DOpen = (Openptr) GetProcAddress (hLib, "adc16_open_unit");
DClose = (Closeptr) GetProcAddress (hLib, "adc16_close_unit");
Valget = (Valueptr) GetProcAddress (hLib, "adc16_get_value");
Vget = (Versionptr) GetProcAddress (hLib, "adc16_get_version");
SetCh = (SetChannelptr) GetProcAddress (hLib, "adc16_set_channel");
FValue = (FValueptr) GetProcAddress (hLib, "adc16_get_filtered_value");
Poll = (Pollptr) GetProcAddress (hLib, "adc16_poll");
GCycle = (GCycleptr) GetProcAddress (hLib, "adc16_get_cycle");

DOpen(port);

for (i=10; i>=0; i--)
{
Vget(&version, port);
printf("\nADC-16 Version: %04d\n" ,version);
SetCh(port, channel, resolution, mode, ffactor);
Sleep(2000);

Valget(&value, port, resolution, channel); //=============PROGRAM FAILURE HAPPENS HERE !!!
printf("\nValue of A/D-conversion: %d" ,value);

Sleep(5000);
printf("\nA/D-conversions made: %04x\n.\n.\n.\n.\n." ,GCycle(&cycle,port));
}
DClose(port);
FreeLibrary(hLib);

return 0;
}

Does anyone have a clue what I'm doing wrong ? adc16_get_version returns value 4106.

Guest

Post by Guest »

Just got rid of errors by selecting from: project > properties > configuration properties > c/c++ > code generation > basic runtime checks: Unintialised Variables option. Still having problems to get valid data from datalogger, only valid return value is version number. All the other return values are 1 or 0. What's going wrong ?

Sarah

Post by Sarah »

Hi

Thank you for your post.

The return values of the function are meant to be 1 or 0 - they show whether a reading is available or not. After the driver has been opened it will take a few seconds before this returns TRUE.

To get the values, you need to look at the first argument in the function - the actual reading is assigned to this variable when it becomes available.

I hope this helps

Best Regards

Guest

Post by Guest »

Still having problems with functions. Only adc16_get_version and adc16_open_unit return true so these are working. I can't get any of others to return true and value of the arguments that are supposed to change during the functions don't change.

Guest

Post by Guest »

I made some minor changes to it, still won't work. Current code is here:

#include "stdafx.h"
#include

typedef int (_stdcall *Openptr) (short int port);
typedef int (_stdcall *Closeptr) (unsigned short int port);
typedef int (_stdcall *Valueptr) (long far * value, unsigned short int port, short int channel);
typedef int (_stdcall *FValueptr) (long far * value, unsigned short int port, short int channel);
typedef int (_stdcall *GCycleptr) (long far * cycle, short int port);
typedef int (_stdcall *Versionptr) (short int far * version, short int port);
typedef void (_stdcall *Pollptr) (void);
typedef void (_stdcall *SetChannelptr) (unsigned short int port, unsigned short int channel,
unsigned short int resolution, unsigned short int single_ended,
unsigned short int filter_factor);
void wait(int frv);

int main(int argc, _TCHAR* argv[])
{
HINSTANCE hLib;

Openptr DOpen;
Closeptr DClose;
Valueptr Valget;
Versionptr Vget;
SetChannelptr SetCh;
FValueptr FValue;
Pollptr Poll;
GCycleptr GCycle;

short int version; // Version of ADC16
unsigned short int port=1; // 1= COM1, 2= COM2 etc.
unsigned short int channel=1; // Current channel
unsigned short int resolution=13; // A/D resolution (bits)
unsigned short int mode=1; // False for differential mode, True = single ended
unsigned short int ffactor=1; // Filtering factor
long far value=0;
long far cycle=0;
int i;
int frv=0;

/*Load .dll library*/
hLib = LoadLibrary(TEXT("adc1632.dll"));

DOpen = (Openptr) GetProcAddress (hLib, "adc16_open_unit");
DClose = (Closeptr) GetProcAddress (hLib, "adc16_close_unit");
Valget = (Valueptr) GetProcAddress (hLib, "adc16_get_value");
Vget = (Versionptr) GetProcAddress (hLib, "adc16_get_version");
SetCh = (SetChannelptr) GetProcAddress (hLib, "adc16_set_channel");
FValue = (FValueptr) GetProcAddress (hLib, "adc16_get_filtered_value");
Poll = (Pollptr) GetProcAddress (hLib, "adc16_poll");
GCycle = (GCycleptr) GetProcAddress (hLib, "adc16_get_cycle");

printf("Opening unit\n");
frv = DOpen(port);
wait(frv);
printf("Done !\n");

printf("Sending control byte...\n");
SetCh(port, channel, resolution, mode, ffactor);
printf("Done !\n");
frv = Vget(&version, port);
wait(frv);

printf("ADC-16 Version: %d\n" ,version);

for (i=10; i>=0; i--)
{
frv = Valget(&value, port, channel);
wait(frv);
printf("Value of A/D-conversion: %i\n" ,value);
Sleep(1000);
frv = FValue(&value, port, channel);
wait(frv);
printf("Value of filtered A/D-conversion: %i\n" ,value);
Sleep(1000);
frv = GCycle(&cycle,port);
wait(frv);
printf("A/D-conversions made: %i\n.\n.\n" ,cycle);
Sleep(1000);
}

frv = DClose(port);
wait(frv);
printf("Unit closed !!!");

FreeLibrary(hLib);
return 0;
}

void wait (int frv)
{
do {
Sleep(500);
if (frv==1) break;
Sleep(500);
if (frv==1) break;
Sleep(500);
if (frv==1) break;
Sleep(500);
if (frv==1) break;
printf("Invalid return value !!!\n");
}while (frv == 0);
}

What is wrong with it ? Am I still doing something wrong or is there some kind of compatibility issue ?

Post Reply