📄 dosoft.cpp
字号:
/*
****************************************************************************************
* 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);
//Thread function declare:
void UserThread(LPVOID );
static PT_EnableEvent ptEnableEvent; // Enable event
static PT_CheckEvent ptCheckEvent; // Check event
long lDriverHandle;
void main()
{
DWORD dwErrCde;
ULONG lDevNum;
USHORT usState;
USHORT usChan;
USHORT usPort;
USHORT usMaxPort;
char chIput;
DEVFEATURES devFeatures;
PT_DeviceGetFeatures ptDevGetFeatures;
PT_DioWriteBit ptDioWriteBit;
HANDLE hThreadHandle;
ULONG ulWatchdogCounter;
short gnNumOfDevices;
short nOutEntries;
DEVLIST DeviceList[100 ];
DWORD dwThreadID;
int i;
DWORD dwWatchdogCounter;
//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");
// Add type of PC Laboratory Card
dwErrCde = DRV_DeviceGetList(&DeviceList[0], 100, &nOutEntries);
// Return the number of devices which you install in the system using
// Device Installation
dwErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
printf("\nThis is the installed device list:\n");
for (i = 0; i < gnNumOfDevices; i++)
{
printf(" %3.3d %s\n",DeviceList[i].dwDeviceNum,DeviceList[i].szDeviceName);
}
//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.usMaxDOChl /8 - 1;
printf("nPlease input port(0~%X):", usMaxPort);
scanf("%d", &usPort);
printf("Output Channel (0 to 7): ");
scanf("%d", &usChan);
printf("Output State (0 or 1): ");
scanf("%d", &usState);
printf("Watchdog time(ms): ");
scanf("%d", &dwWatchdogCounter);
dwWatchdogCounter = dwWatchdogCounter*10000;
ulWatchdogCounter = dwWatchdogCounter;
DRV_DeviceSetProperty( lDriverHandle, CFG_WatchdogCounter, &ulWatchdogCounter,sizeof(ULONG) );
ptEnableEvent.EventType = ADS_EVT_WATCHDOG_OVERRUN;
ptEnableEvent.Enabled = 1;
ptEnableEvent.Count = 1;
if ((dwErrCde = DRV_EnableEvent(lDriverHandle,
(LPT_EnableEvent)&ptEnableEvent)) != 0)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
hThreadHandle = CreateThread(0, 0,
(LPTHREAD_START_ROUTINE) UserThread,
NULL, 0, &dwThreadID);
DRV_WatchdogStart( lDriverHandle,NULL );
do{
printf("Press Esc key to exit;\n");
printf("Press Space key to feed the watchdog;\n");
printf("Press Enter key to restart the watchdog.\n");
chIput = getch();
if( chIput == VK_RETURN )
DRV_WatchdogStart( lDriverHandle,NULL );
DRV_WatchdogFeed( lDriverHandle );
// 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);
}while(chIput!=VK_ESCAPE);
// Step 6: Close device
DRV_WatchdogStop( lDriverHandle );
TerminateThread( hThreadHandle, 0);
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");
printf("\nPress any key to exit....");
//Close device
DRV_DeviceClose(pDrvHandle);
exit(1);
}//ErrorStop
/*********************************************************************
* Function: UserThread
* Parameters: pISamples, IN, the count of DMA convertion number
* Return value: none
* Purpose: Check the event and handle it
********************************************************************/
void UserThread(LPVOID pISamples)
{
USHORT usEventType;
LONG ErrCde;
while (TRUE)
{
ptCheckEvent.EventType = &usEventType;
ptCheckEvent.Milliseconds = INFINITE;
if (ErrCde = DRV_CheckEvent(lDriverHandle, (LPT_CheckEvent)&ptCheckEvent)!=0)
{
printf("Check event error!\n");
return;
}
if (usEventType & ADS_EVT_WATCHDOG_OVERRUN )
{
printf("Watchdog Overrun!\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -