/* Test with I-87022 AO Module and I-87017 AI Module at Address 01 and 02 of RS-485 network seperately.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   
   i87kaio_87k.c
 
   v 1.1.0 18 Jan 2017 by Winson
     Use ttyS* replace COM*

   v1.0 2004.9.1 by Edward  
 
 */

#include<stdio.h>
#include<stdlib.h>
#include "msw.h"

char szSend[80], szReceive[80];
DWORD wBuf[12];
DWORD  wBuf7[12];
float fBuf[12];

int main()
{
	int i,j, wRetVal;
	DWORD temp;
  	
	wRetVal = Open_Com(ttyS1, 9600, Data8Bit, NonParity, OneStopBit);
	if (wRetVal > 0) {
		printf("open port failed!\n");
		return (-1);
	}	
	
	//--- Analog output ----  ****   87022 -- AO  ****
	i=0;
	wBuf[0] = ttyS1;  	// COM Port
	wBuf[1] = 0x01;		// Address
	wBuf[2] = 0x87022;	// ID
	wBuf[3] = 0;		// CheckSum disable
	wBuf[4] = 100;		// TimeOut , 10 second
	wBuf[5] = i;		// Channel Number of AO
	wBuf[6] = 0;		// string debug
	fBuf[0] = 2.5;		// AO Value
  
	wRetVal = AnalogOut_87K(wBuf, fBuf, szSend, szReceive);
		if (wRetVal)
			printf("AO of 87022 Error !, Error Code=%d\n", wRetVal);
		else	
			printf("AO of 87022 channel %d = %f \n",i,fBuf[0]);

	//--- Analog Input ----  ****   87017 -- AI  ****
	j=1;
	wBuf7[0] = ttyS1;	// COM Port
	wBuf7[1] = 0x02;	// Address
	wBuf7[2] = 0x87017;	// ID
	wBuf7[3] = 0;		// CheckSum disable
	wBuf7[4] = 100;		// TimeOut , 10 second
	wBuf7[5] = j;		//Channel Number of AI
	wBuf7[6] = 0;		// string debug
				
	wRetVal = AnalogIn_87K(wBuf7, fBuf, szSend, szReceive);
		if (wRetVal)
			printf("AI of 87017 Error !, Error Code=%d\n", wRetVal);
		else	
			printf("AI of 87017 channel %d = %f \n",j,fBuf[0]);

	Close_Com(ttyS1);
	return 0;
}
