C/C++ Sample source code
-- showing console style application for simplicity --
-----   Level 4   -----
( demonstrates non-scanning mode of operation with I/O functionality )

A little about this document
Quick jump to
About this document    Screenshots    .cpp code sample    .h header sample

This documentation was created using the sample application found within     .  
It's intended be used as a reference to understand the simplicity of interracting with the  Model 201   using the combination of VisualC++ and our   M201_SP.DLL   dynamically linked library. The code shown could easily be converted to the very basic ANSI C format  ( most of it already is just that )  since a console output   is used instead of a GUI interface. The Library is included in the archive linked to above and all available   function calls   are well documented.

Shown here is the code required to do various I/O functions. Rather than start up running in "scan mode" the application just signs on and then waits for a command. In regard to the data acquisition, this mode of operation can be referred to as "Polled Mode" since you can "Poll" the device anytime for a voltage reading (or other I/O) but the device is not actively scanning for voltages.

The samples shown below show how to get one voltage using   EX_GetOneConversion,  change the channel using   EX_SetPolledModeChan,  send a digital output using   EX_SendDigout,  and read the digital input using   EX_GetDigin  

This documention, shows some screen shots of the application running, the main source code file, and the header file, that is created. Follow the  "Quick Jump"  links just above to jump to each.

The Application Screenshots
Quick jump to
About this document    Screenshots    .cpp code sample    .h header sample

Application has loaded the DLL and is waiting
This sample application uses all default configuration parameters.
It will sign on automatically when the first command is entered.

The only thing that has happened is the DLL has loaded and the existance
of function calls that will be used has been verified.
Polled-Mode commands have been entered
the following commands  (shown in source code below)  have been entered:
EX_GetDigin  (1st occurrence)     EX_SendDigout      EX_GetDigin  (2nd occurrence)
EX_GetOneConversion  (1st occurrence chan-0)      EX_SetPolledModeChan
EX_GetOneConversion  (2nd occurrence chan-6)

NOTE:  digital output is connected to the digital input

The C++ Source Code file
Quick jump to
About this document    Screenshots    .cpp code sample    .h header sample

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

The C++ Header file
Quick jump to
About this document    Screenshots    .cpp code sample    .h header sample

//filename: mainSamp.h

typedef BOOLEAN(*P_DLL_EX_InitDev) (void);
P_DLL_EX_InitDev EX_InitDev = NULL;

typedef BOOLEAN(*P_DLL_EX_SendDigout) (BYTE bDigOut);
P_DLL_EX_SendDigout EX_SendDigout = NULL;

typedef BOOLEAN(*P_DLL_EX_GetDigin) (BYTE* bDigIn);
P_DLL_EX_GetDigin EX_GetDigin = NULL;

typedef BOOLEAN(*P_DLL_EX_SetPolledModeChan) (BYTE bPolledChan);
P_DLL_EX_SetPolledModeChan EX_SetPolledModeChan = NULL;

typedef BOOLEAN(*P_DLL_EX_GetOneConversion) (double* dblVolts);
P_DLL_EX_GetOneConversion EX_GetOneConversion = NULL;

Contact us for sales or technical assistance

on the web

© Lawson Labs Inc. All rights reserved
Last modified 11-8-04 Tim Van Dusen