function contec_multiao(AoData);
%  multiao.m
%  The calling syntax is:
%  contec_multiao(AoData);
%  Kazuyuki Kobayashi   Feb 21, 2005
%  Copyright (c) 2005 Gerox(c)
%  All Rights Reserved
%  ADA16-8/2(CB)L AD128PM でテスト済み
% Windows版高機能アナログ入出力ドライバ API-AIO(WDM)がサポートしているカードで動作可
% ただし、出力レンジなどの設定をチューニングする必要あり、
if nargin ~=1
		fprintf('   Contec DA card driver for MATLAB Version 6.5 later \n');
		fprintf('                       Gerox(c) 2005 All Rights Reserved\n');
		fprintf('                                    http://www.gerox.com\n');
		fprintf('   In order to use this function,please install         \n');
		fprintf('   API-AIO(WDM) (Contec web site )  and copy caio.h in matlab current working directory\n');
		fprintf('                  default DeviceName is AIO000\n\n');
		fprintf('   Usage: contec_multiao(AoData)\n\n');
		fprintf('   contec_multiao(2.0)\n\n');
		fprintf('   contec_multiao([0 1])\n\n');
end	
if exist('caio.h')~= 2;
	disp('これを使用する際には、コンテック社のドライバAPI-AIO(WDM)に含まれるcaio.hが必要です。');
	return;
end
loadlibrary('caio.dll','caio.h');
Id=0;DeviceName='AIO000';
AoChannels=length(AoData);
[Ret,DeviceName,Id]=calllib('caio','AioInit',DeviceName, Id);
if Ret ~= 0
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioInit = %d : %s\n',Ret,ErrorString);
	fprintf('AioInit = %d \n',Ret);
	unloadlibrary('caio');
	return;
end
AoRange=0;
Ret = calllib('caio','AioSetAoRangeAll',Id,AoRange);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioSetAiRangeAll = %d : %s\n',Ret,ErrorString);
	fprintf('AioSetAiRangeAll = %d\n',Ret);
	unloadlibrary('caio');
	return;
end
MaxAoChannels = 0;
[Ret,MaxAoChannels]=calllib('caio','AioGetAoMaxChannels',Id,MaxAoChannels);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioGetAoMaxChannels = %d : %s\n',Ret,ErrorString);
	fprintf('AioGetAoMaxChannels = %d \n',Ret);
	Ret = calllib('caio','AioExit',Id );
	unloadlibrary('caio');
	return;
end
if(MaxAoChannels < AoChannels) AoChannels = MaxAoChannels;end
pAoData=libpointer('singlePtr',AoData);
[Ret,AoData] = calllib('caio','AioMultiAoEx',Id, AoChannels, AoData);
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioAoData = %d : %s\n',Ret,ErrorString);
	fprintf('AioAoData = %d \n',Ret);
	unloadlibrary('caio');
	return;
end
Ret = calllib('caio','AioExit',Id );
if Ret~=0;
%	[Ret,ErrorString]=calllib('caio','AioGetErrorString',Ret,char(zeros(1,256)));
%	fprintf('AioExit = %d : %s\n',Ret,ErrorString);
	fprintf('AioExit = %d \n',Ret);
	unloadlibrary('caio');
	return;
end
if libisloaded('caio');unloadlibrary('caio');end
