/******************************************************************************
//                                                                           //
//  Project: Software Interface for COM-DIO Devices                          //
//                                                                           //
//  CGC Instruments, (c) 2009-2017. Version 1.30. All rights reserved.       //
//                                                                           //
//  Definition for COM_DIO DLL Routines                                      //
//                                                                           //
******************************************************************************/

#ifndef __COM_DIO_H__
#define __COM_DIO_H__


#ifdef __cplusplus
extern "C" {
#endif


#define COM_DIO_MAX_PORT 8 /* Maximum PortNumber */

/*
	The listed routines accept the number of the port PortNumber in the range 0..COM_DIO_MAX_PORT-1,
	thus maximum COM_DIO_MAX_PORT independent communication channels (ports) can be defined

	Each communication channel must be opened before the first usage. The Open routine assigns a specified COMx port to the channel.
	The ComNumber specifies the number of the port - 1 stands for COM1, 2 for COM2, etc.

	If necessary, the channel may be closed and assigned by the Open routine to a new COMx port.
	The channels are closed automatically at the end of the program, the main program does not need to take care about it.

	The return values of the routines are the following defines COM_DIO_ERR_XXX
		 0: routine finished without any error (COM_DIO_NO_ERR)
		 1: COM_DIO_CheckInput returns 1=COM_DIO_NO_DATA if no new data is available
		<0: errors COM_DIO_ERR_XXX

	The last error code can be obtained by calling COM_DIO_State.
	The routine COM_DIO_ErrorMessage returns a pointer to a zero-terminated character string.

	If a communication error occurred, the error code can be red again by COM_DIO_IO_State,
	The routine COM_DIO_IO_ErrorMessage provides the corresponding error message.
*/

#define COM_DIO_NO_ERR               0 /* No error occured         */
#define COM_DIO_NO_DATA              1 /* No new data is available */

#define COM_DIO_ERR_PORT_RANGE      -1 /* PortNumber out of range */
#define COM_DIO_ERR_OPEN            -3 /* Error opening the port  */
#define COM_DIO_ERR_CLOSE           -4 /* Error closing the port  */
#define COM_DIO_ERR_PURGE           -5 /* Error purging the port  */
#define COM_DIO_ERR_COMMAND_SEND    -6 /* Error sending command   */
#define COM_DIO_ERR_COMMAND_RECEIVE -7 /* Error receiving command */
#define COM_DIO_ERR_COMMAND_WRONG   -8 /* Wrong command received  */
#define COM_DIO_ERR_ARGUMENT_WRONG  -9 /* Wrong argument received */

#define COM_DIO_ERR_DEBUG_OPEN    -400 /* Error opening the file for debugging output */
#define COM_DIO_ERR_DEBUG_CLOSE   -401 /* Error closing the file for debugging output */


/* General */

WORD FAR _export __pascal COM_DIO_Version(); /* Get the COM-DIO software version */

int FAR _export __pascal COM_DIO_Open  (WORD PortNumber, WORD ComNumber); /* Open the port  */
int FAR _export __pascal COM_DIO_Close (WORD PortNumber);                 /* Close the port */

int FAR _export __pascal COM_DIO_Purge (WORD PortNumber); /* Clear data buffers for the port */


/* Digital I/O */

int  FAR _export __pascal COM_DIO_GetInput     (WORD PortNumber, WORD & Data); /* Read input state                                 */
int  FAR _export __pascal COM_DIO_AutoInput    (WORD PortNumber);              /* Set automatic transmission of input state        */
int  FAR _export __pascal COM_DIO_CheckInput   (WORD PortNumber, WORD & Data); /* Check for new input state data                   */
WORD FAR _export __pascal COM_DIO_RetInput     (WORD PortNumber);              /* Pascal optimized: read input state               */
WORD FAR _export __pascal COM_DIO_RetInputData (WORD PortNumber);              /* Pascal optimized: check for new input state data */

int  FAR _export __pascal COM_DIO_SetOutput  (WORD PortNumber, WORD   Data); /* Set output state                    */
int  FAR _export __pascal COM_DIO_GetOutput  (WORD PortNumber, WORD & Data); /* Read output state                   */
WORD FAR _export __pascal COM_DIO_RetOutput  (WORD PortNumber);              /* Pascal optimized: read output state */


/* System */

int FAR _export __pascal COM_DIO_FW_Ver  (WORD PortNumber, WORD & Version);            /* Get the COM-DIO firmware version       */
int FAR _export __pascal COM_DIO_FW_Date (WORD PortNumber, char * FAR DateString);     /* Get the COM-DIO firmware date          */
int FAR _export __pascal COM_DIO_prod_ID (WORD PortNumber, char * FAR Identification); /* Get the COM-DIO product identification */
int FAR _export __pascal COM_DIO_prod_No (WORD PortNumber, DWORD & Number);            /* Get the COM-DIO product number         */


/* Communication port */

int          FAR _export __pascal COM_DIO_State           (WORD PortNumber); /* Get software interface state                                           */
const char * FAR _export __pascal COM_DIO_ErrorMessage    (WORD PortNumber); /* Get the error message corresponding to the software interface state    */
int          FAR _export __pascal COM_DIO_IO_State        (WORD PortNumber); /* Get serial port interface state                                        */
const char * FAR _export __pascal COM_DIO_IO_ErrorMessage (WORD PortNumber); /* Get the error message corresponding to the serial port interface state */


/* Debugging output */

int FAR _export __pascal COM_DIO_OpenDebugFile  (WORD PortNumber, const char * FAR FileName); /* Open a text file for debugging output, return 0 on success */
int FAR _export __pascal COM_DIO_CloseDebugFile (WORD PortNumber);                            /* Close the file for debugging output, return 0 on success   */

#ifdef __cplusplus
}
#endif


#endif//__COM_DIO_H__
