function my_callback1(obj1,event)
global data;
global obj2;
global sendbuff;
global data1;
global data2;
global data3;
global data4;
global data5;
global data6;
global n;
n=n+1;
data= fread(obj1, 24)
if lenth(data)==24
if data(1) == 68
if data(2) == 5
data1 = (data(3)*256+data(4))/1000;
data2 = (data(5)*256+data(6))/1000;
data3 = (data(7)*16777216+data(8)*65536+data(9)*256+data(10))/100;
data4 = (data(11)*16777216+data(12)*65536+data(13)*256+data(14))/100;
data5 = (data(15)*16777216+data(16)*65536+data(17)*256+data(18))/100;
data6 = (data(19)*16777216+data(20)*65536+data(21)*256+data(22))/100;
end
end
if data(1) == 5
data1 = (data(2)*256+data(3))/1000;
data2 = (data(4)*256+data(5))/1000;
data3 = (data(6)*16777216+data(7)*65536+data(8)*256+data(9))/100;
data4 = (data(10)*16777216+data(11)*65536+data(12)*256+data(13))/100;
data5 = (data(14)*16777216+data(15)*65536+data(16)*256+data(17))/100;
data6 = (data(18)*16777216+data(19)*65536+data(20)*256+data(21))/100;
end
sendbuff(1)=floor((data1+data4)/256);
sendbuff(2)=floor((data1+data4));
sendbuff(3)=floor((data2+data5)/256);
sendbuff(4)=floor((data2+data5));
sendbuff(5)=floor((data3+data6)/256);
sendbuff(6)=floor((data4+data6));
fwrite(obj2, sendbuff, 'uint8');
%my_fwrite(obj2, sendbuff, 'uint8');
end
end
function my_fwrite(obj, varargin)
%FWRITE Write binary data to instrument.
%
% FWRITE(OBJ, A) writes the data, A, to the instrument connected to
% interface object, OBJ.
%
% The interface object must be connected to the instrument with the
% FOPEN function before any data can be written to the instrument
% otherwise an error will be returned. A connected interface object
% has a Status property value of open.
%
% FWRITE(OBJ,A,'PRECISION') writes binary data translating MATLAB
% values to the specified precision, PRECISION. The supported
% PRECISION strings are defined below. By default the 'uchar'
% PRECISION is used.
%
% MATLAB Description
% 'uchar' unsigned character, 8 bits.
% 'schar' signed character, 8 bits.
% 'int8' integer, 8 bits.
% 'int16' integer, 16 bits.
% 'int32' integer, 32 bits.
% 'uint8' unsigned integer, 8 bits.
% 'uint16' unsigned integer, 16 bits.
% 'uint32' unsigned integer, 32 bits.
% 'single' floating point, 32 bits.
% 'float32' floating point, 32 bits.
% 'double' floating point, 64 bits.
% 'float64' floating point, 64 bits.
% 'char' character, 8 bits (signed or unsigned).
% 'short' integer, 16 bits.
% 'int' integer, 32 bits.
% 'long' integer, 32 or 64 bits.
% 'ushort' unsigned integer, 16 bits.
% 'uint' unsigned integer, 32 bits.
% 'ulong' unsigned integer, 32 bits or 64 bits.
% 'float' floating point, 32 bits.
%
% FWRITE(OBJ, A, 'MODE')
% FWRITE(OBJ, A, 'PRECISION', 'MODE') writes data asynchronously
% to the instrument when MODE is 'async' and writes data synchronously
% to the instrument when MODE is 'sync'. By default, the data is
% written with the 'sync' MODE, meaning control is returned to
% the MATLAB command line after the specified data has been written
% to the instrument or a timeout occurs. When the 'async' MODE is
% used, control is returned to the MATLAB command line immediately
% after executing the FWRITE function.
%
% The byte order of the instrument can be specified with OBJ's
% ByteOrder property.
%
% OBJ's ValuesSent property will be updated by the number of values
% written to the instrument.
%
% If OBJ's RecordStatus property is configured to on with the RECORD
% function, the data written to the instrument will be recorded in
% the file specified by OBJ's RecordName property value.
%
% Example:
% s = visa('ni', 'ASRL2::INSTR');
% fopen(s);
% fwrite(s, [0 5 5 0 5 5 0]);
% fclose(s);
%
% See also ICINTERFACE/FOPEN, ICINTERFACE/FPRINTF, ICINTERFACE/RECORD,
% ICINTERFACE/PROPINFO, INSTRHELP.
%
% MP 7-13-99
% Copyright 1999-2012 The MathWorks, Inc.
% Error checking.
if ~isa(obj, 'icinterface')
error(message('instrument:fwrite:invalidOBJInterface'));
end
if length(obj)>1
error(message('instrument:fwrite:invalidOBJDim'));
end
% Parse the input.
switch nargin
case 1
error(message('instrument:fwrite:invalidSyntaxA'));
case 2
cmd = varargin{1};
precision = 'uchar';
mode = 0;
case 3
% Original assumption: fwrite(obj, cmd, precision);
[cmd, precision] = deal(varargin{1:2});
mode = 0;
if ~(isa(precision, 'char') || isa(precision, 'double'))
error(message('instrument:fwrite:invalidArg'));
end
if strcmpi(precision, 'sync')
% Actual: fwrite(obj, cmd, mode);
mode = 0;
precision = 'uchar';
elseif strcmpi(precision, 'async')
% Actual: fwrite(obj, cmd, mode);
mode = 1;
precision = 'uchar';
end
case 4
% Ex. fprintf(obj, format, cmd, mode);
[cmd, precision, mode] = deal(varargin{1:3});
if ~ischar(mode)
error(message('instrument:fwrite:invalidMODE'));
end
if strcmpi(mode, 'sync')
mode = 0;
elseif strcmpi(mode, 'async')
mode = 1;
else
error(message('instrument:fwrite:invalidMODE'));
end
otherwise
error(message('instrument:fwrite:invalidSyntaxArgv'));
end
% % % % Error checking.
% % % if ~isa(precision, 'char')
% % % error(message('instrument:fwrite:invalidPRECISIONstring'));
% % % end
% % % if ~(isnumeric(cmd) || ischar(cmd))
% % % error(message('instrument:fwrite:invalidA'));
% % % end
% Convert the data to the requested precision.
switch (precision)
case {'uchar', 'char'}
cmd = uint8(cmd);
type = 5;
signed = 0;
case {'schar'}
cmd = int8(cmd);
type = 5;
signed = 1;
case {'int8'}
cmd = int8(cmd);
type = 0;
signed = 1;
case {'int16', 'short'}
cmd = int16(cmd);
type = 1;
signed = 1;
case {'int32', 'int', 'long'}
cmd = int32(cmd);
type = 2;
signed = 1;
case {'uint8'}
cmd = uint8(cmd);
type = 0;
signed = 0;
case {'uint16', 'ushort'}
cmd = uint16(cmd);
type = 1;
signed = 0;
case {'uint32', 'uint', 'ulong'}
cmd = uint32(cmd);
type = 2;
signed = 0;
import java.lang.Long
for iLoop = 1:length(cmd)
tmp(iLoop) = Long(cmd(iLoop)); %#ok<AGROW>
end
cmd=tmp;
case {'single', 'float32', 'float'}
cmd = single(cmd);
type = 3;
signed = 1;
case {'double' ,'float64'}
cmd = double(cmd);
type = 4;
signed = 1;
otherwise
error(message('instrument:fwrite:invalidPRECISION'));
end
% i2c does not support async mode
if mode == 1
error(message('instrument:fwrite:i2cAyncNotSupported'));
end
% Call the write java method.
try
fwrite(igetfield(obj, 'jobject'), cmd, length(cmd), type, mode, signed);
catch aException
newExc = MException('instrument:fwrite:opfailed', aException.message);
throw(newExc);
end