// filename: mainSamp.cpp
// The code shown below is ready for compilation. Just create a .cpp and .h file, copy the code below into those files, compile it in your IDE, and run it.
// There is also ready to test sample application containing the following code downloadable from our website .
// Demonstrates non-scan mode usage. Note the use of "EX_GetOneConversion( )" which is the function call into the DLL used to get a single voltage
// when not in scanning mode. If you've just started the application, the very first call to this and other I/O functions will first perform a signon of the device.
// The signon establishes serial communication and sets some default configuration parameters.
// In order to get the voltage from the correct channel, you may also need to call "EX_SetPolledModeChan( )". The arguments that are passed are the main
// channel as well as the sub-channel. The sub channel is also often referred to as the external "control code" since it can be used to "control" a multiplexer .
// Also shown here is how to send and read the digital input value using "EX_SendDigout( )" and "EX_GetDigin( )".
#include <windows.h>
#include <stdlib.h>
#include <wtypes.h>
#include <conio.h>
#include <stdio.h>
#include "mainSamp.h"
BOOLEAN InitLibFuncs()
{
hLib201 = LoadLibrary ("M201_SP.dll");
if( !hLib201 ) return FALSE;
EX_SendDigout = (P_DLL_EX_SendDigout)GetProcAddress( hLib201, "EX_SendDigout" );
if( !EX_SendDigout ) return FALSE;
EX_GetDigin = (P_DLL_EX_GetDigin)GetProcAddress( hLib201, "EX_GetDigin" );
if( !EX_GetDigin ) return FALSE;
EX_SendChan = (P_DLL_EX_SetPolledModeChan)GetProcAddress( hLib201, "EX_SetPolledModeChan" );
if( !EX_SetPolledModeChan ) return FALSE;
EX_GetOneConversion = (P_DLL_EX_GetOneConversion)GetProcAddress( hLib201, "EX_GetOneConversion" );
if( !EX_GetOneConversion ) return FALSE;
return TRUE;
}
NOTE: Although there are many lines of code below, most of them are required for nothing more than managing the console display
int __cdecl main()
{
if( !InitLibFuncs() ){ printf("Failed to find load required DLL and/or find required functions" ); getch(); return 0;}
printf("Success finding required functions\nStarting RUN - please wait. . .");
int iKeyChar = 0; char szOutVal[32], szTempBuff[32]; BYTE bDigInput; double dblMilliVolts;
BOOLEAN fRedraw = FALSE; char cUserInputVal;
while(true)
{
printf("\n\nINITIALIZED\nPress one of the following keys:\n"
"'Q' exit\n"
"'R' redraw the screen\n"
"'G' get digital input\n"
"'S' send digital output (follow by value 255 or less)\n"
"'V' get one voltage - it will come from preconfigured channel\n"
"'C' set the channel\n");
while((cUserInputVal != 'Q') && (cUserInputVal != 'q') && (cUserInputVal != 27))
{
cUserInputVal = (char)getch();
switch(cUserInputVal)
{
case 'Q': case 'q':
fRedraw = TRUE; system("cls"); break;
case 'R': case 'r':
break;
case 'G': case 'g':
if(!EX_GetDigin(&bDigInput)) printf("\nfailed to get digital input\n");
else(printf("\ndigital input: %d\n", bDigInput)); break;
case 'V': case 'v':
if(!EX_GetOneConversion(&dblMilliVolts)) printf("\nfailed to get voltage\n");
else(printf("\nvoltaget: %2.5lf", dblMilliVolts / 1000.0)); break;
case 'S': case 's':
sprintf(szOutVal, ""); printf("\nenter value: ");
while(TRUE)
{
iKeyChar = getche(); if(iKeyChar == 13) break;
sprintf(szTempBuff, "%u", ((BYTE)iKeyChar) - '0');
strcat(szOutVal, szTempBuff); if(strlen(szOutVal) == 3) break;
}
if(((UINT)(atoi(szOutVal))) > 255)
{ printf("\nvalue entered greater than 255. New value: 255"); sprintf(szOutVal, "255"); }
if(!EX_SendDigout((BYTE)(atoi(szOutVal)))) printf("\nfailed to send digital input\n");
else printf("\ndigital output sent: %d\n", (BYTE)(atoi(szOutVal)));
sprintf(szOutVal, ""); break;
case 'C': case 'c':
sprintf(szOutVal, ""); printf("\nenter channel: ");
while(TRUE)
{
iKeyChar = getche(); if(iKeyChar == 13) break;
sprintf(szTempBuff, "%u", ((BYTE)iKeyChar) - '0');
strcat(szOutVal, szTempBuff); if(strlen(szOutVal) == 1) break;
}
if(((UINT)(atoi(szOutVal))) > 15)
{ printf("\nvalue entered greater than 15. New value: 7"); sprintf(szOutVal, "7"); }
if(!EX_SetPolledModeChan((BYTE)(atoi(szOutVal)))) printf("\nfailed to send channel\n");
else printf("\nchannel sent: %d\n", (BYTE)(atoi(szOutVal)));
sprintf(szOutVal, ""); break;
}
}
if(!fRedraw) break;
}
return 1;
}
|