Seite 1 von 1

C++ Code für DIO-Karte, wer kann helfen?

Verfasst: 29.07.2005 15:43
von Voni
Hallo C++ Programmierer!

Ich habe habe hier einen Beispielcode für eine dig. I/O-Karte, den ich nicht so ganz verstehe, da ich nur wenig C beherrsche und Pointer und Structures auch nicht so mein Ding sind. Wer kann mir erkären, was in dem Code abgeht, damit ich in die Lage komme, dieses Bsp. in Purebasic umzusetzen? Die Karte habe ich schon erfolgreich geöffnet, aber ab Step 4 (Get features) stehe ich auf dem Schlauch.

Code: Alles auswählen

/*
 ****************************************************************************************
 * Program        : DIGOUT.CPP                                                          *
 * Description    : Demo program for digital output function                            *
 * Boards Supp.   : PCL-818 series/816/1800/812PG/711B, PCM-3718/3724                   *
 *                  PCI-1710/1720/1750/1751/1760, PCL-813B/725/730/722/724/731          *
 *                  PCL-734/735/720/836, MIC-2750/2752/2760                             *
 *                  ADAM-4011/4011D/4012/4014D/4016/4060/4050/5060/4080D/5050/4050      *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_DioWriteBit                     *
 *                  DRV_GetErrorMessage                                                 *
 * Revision       : 1.00                                                                *
 * Date           : 7/8/1999                   Advantech Co., Ltd.                      *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>

#include "..\..\..\include\driver.h"

/******************************
 * Local function declaration *
 ******************************/
void ErrorHandler(DWORD dwErrCde);
void ErrorStop(long*, DWORD);

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    USHORT usState;
	USHORT usChan;
	USHORT usPort;
	USHORT usMaxPort;
	char   chIput;
	DEVFEATURES devFeatures;
	PT_DeviceGetFeatures ptDevGetFeatures;
	PT_DioWriteBit ptDioWriteBit;

	//Step 1: Display hardware and software settings for running this example
	printf("Before running this example, please\n");
	printf("use the device installation utility to add the device.\n");

	//Step 2: Input parameters
	printf("\nPlease input parameters:");
	printf("\nDevice Number (check the device installation utility): ");
	scanf("%d", &lDevNum);

	//Step 3: Open device
	dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle);   
	if (dwErrCde != SUCCESS)
	{
		ErrorHandler(dwErrCde);
		printf("Program terminated!\n");

		printf("Press any key to exit....");
		getch();
		exit(1);
	}

	// Step 4: Get features

	ptDevGetFeatures.buffer = &devFeatures;
	ptDevGetFeatures.size = sizeof(DEVFEATURES);
	dwErrCde = DRV_DeviceGetFeatures(lDriverHandle,(LPT_DeviceGetFeatures)&ptDevGetFeatures);
    if (dwErrCde != SUCCESS)
    {
        ErrorStop(&lDriverHandle, dwErrCde);
        return;
    }
	usMaxPort = devFeatures.usMaxDIChl/8 - 1;
	
	do{
	printf("\nPlease input port:(0~%d)", usMaxPort);
	scanf("%d", &usPort);
	printf("\nOutput Channel (0 to 7): ");
	scanf("%d", &usChan);
	printf("\nOutput State (0 or 1): ");
	scanf("%d", &usState);
	
	
	// Step 4: Turn on/off the specified channel
	ptDioWriteBit.port  = usPort;  // output port: 0
	ptDioWriteBit.bit   = usChan;  // output channel
	ptDioWriteBit.state = usState; // output state
    
	
	dwErrCde = DRV_DioWriteBit(lDriverHandle,
						 (LPT_DioWriteBit)&ptDioWriteBit);

	if (dwErrCde != SUCCESS)
	{
		ErrorStop(&lDriverHandle, dwErrCde);
		return;
	}

	// Step 5: Display ouptut data
	printf("\nOutput data = %d\n", usState);
	printf("\nPress any key to continue, press Esc to exit...");
	chIput = getch();
	}while(chIput!=VK_ESCAPE);
	// Step 6: Close device
	dwErrCde = DRV_DeviceClose(&lDriverHandle);

	printf("\nPress any key to exit....");
	getch();

}//main

/**********************************************************************
 * Function: ErrorHandler
 *           Show the error message for the corresponding error code
 * input:    dwErrCde, IN, Error code
 * return:   none
 **********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
    char szErrMsg[180];

    DRV_GetErrorMessage(dwErrCde, szErrMsg);
    printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
}//ErrorHandler

/**********************************************************************
 * Function:   ErrorStop
 *             Release all resource and terminate program if error occurs 
 * Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
 *             dwErrCde, IN, Error code.
 * return:     none             
 **********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde)
{
    //Error message 
    ErrorHandler(dwErrCde);
    printf("Program terminated!\n");
    
    //Close device
    DRV_DeviceClose(pDrvHandle);
    exit(1);
}//ErrorStop


DRV_DeviceGetFeatures:
status = DRV_DeviceGetFeatures (DriverHandle,lpDevFeatures)

Purpose
Retrieves the device-specific features and returns them in buffer.

Parameters
Name / Direction / Type / Description
DriverHandle / Input / long pointer / assigned by DRV_DeviceOpen
lpDevFeatures / Output / long pointer to PT_DeviceGetFeatures / the storage address of the Device Features and size of Device Features

Parameter Details

lpDevFeatures is the storage address of the device's features and size of Device Features. The storage layout refers to DRIVER.H.

Return
1. SUCCESS if successful
2. InvalidDriverHandle if DriverHandle is NULL
3. BoardIDNotSupported if Board ID is not supported


DRV_DioWriteBit
status = DRV_DioWriteBit( DriverHandle, lpDioWriteBit )

Purpose
Write a bit data to a digital output port on DriverHandle specified device.

Parameters
The target DO port, bit number, and bit status are all stored in lpDioWriteBit pointed structure (PT_DioWriteBit).

Here, the range of target DIO port (USHORT port) is from zero (the first DIO port) to its maximum DIO port number subtract one. For example, the range can be set from 0 to 3 on a card with 4 DIO ports.

Bit number (USHORT bit) is used to indicate the bit for operation. The setting range is from 0 (bit 0) to 7 (bit 7).

Name / Direction / Type / Description
DriverHandle / Input long / Pointer retrieved from DRV_DeviceOpen that specifies the target device.
lpDioWriteBit / Input-Output / long pointer to PT_DioWriteBit / Pointer that indicates the structure that stores the target DO port (USHORT port), bit order number (USHORT bit) and the bit status to write(USHORT state).

Return
1. SUCCESS if successful.
2. InvalidDriverHandle if DriverHandle is NULL.
3. BoardIDNotSupported if the function is not supported for this device.
4. InvalidChannel if the port number is out of range.
5. InvalidInputParam if bit number is greater than 7.


Besten Dank im Voraus für eure Hilfe!
Ralf