Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
function DUT_callback(obj, event, DUT_port)
persistent stored_data;

if isempty(stored_data)
    stored_data = [];
end

if ~strcmp(DUT_port.status,'open')
	return;
end

if ~DUT_port.BytesAvailable
	return;
end

try
	new_data = fread(DUT_port,DUT_port.BytesAvailable);
catch exception
	fprintf('ERROR: Failed to read from DUT port.  Shutting down.\n');
	cleanup();
	return;
end
data_array = [stored_data;new_data];

packet.NewPacket = 1;
while packet.NewPacket == 1
	[packet,data_array] = parse_serial_data(data_array);
	% Report bad checksum if appropriate
	if packet.BadChecksum
	end
	
	% If packet was received, do stuff with it
	if packet.NewPacket == 1
		% HANDLE RAW GYRO DATA PACKET
		if packet.Address == 86
			% Extract the gyro data
			gyro_x = typecast( flipud(uint8(packet.data(1:2))), 'int16' );
			gyro_y = typecast( flipud(uint8(packet.data(3:4))), 'int16' );
			gyro_z = typecast( flipud(uint8(packet.data(5:6))), 'int16' );

			got_gyro_data = 1;
% 			fprintf('%d\t%d\t%d\n',gyro_x,gyro_y,gyro_z);
		end 

		% HANDLE TEMPERATURE DATA PACKET
		if packet.Address == 118
			temperature = typecast( flipud(uint8(packet.data(1:4))), 'single' );
			got_temperature_data = 1;
% 			fprintf('%3.2f\n',temperature);
		end

		% If we received gyro and temperature data, log it to
		% the workspace if logging is enabled.
		if got_temperature_data && got_gyro_data
			got_temperature_data = 0;
			got_gyro_data = 0;

if GDATA.logging_data == 1 && GDATA.selected_DUT > 0
		if GDATA.DUT_samples_collected < GDATA.MAX_SAMPLES
		GDATA.DUT_samples_collected = GDATA.DUT_samples_collected + 1;
			GDATA.DUT_data(GDATA.DUT_samples_collected,:) = [single([GDATA.selected_DUT,gyro_x,gyro_y,gyro_z]),temperature];
				else
		fprintf('Maximum datapoints were collected.  Logging disabled.\n');
					GDATA.logging_data = 0;
	end	end	end	end      end
Posted
Updated 19-Mar-14 3:05am
v2

A callback is a function the user provides but he/she doesn't explicitely call. Instead it is called whenever something happens. For instance, in serial port communication, it could be called in order to handle a serial event (e.g. data arrival).
 
Share this answer
 
Comments
Pervez03 19-Mar-14 9:28am    
Thanks Mr. CPallini.
Pervez03 19-Mar-14 9:30am    
Can you please give some references related with c++ programming (serial communication)?
 
Share this answer
 
Comments
Pervez03 19-Mar-14 9:28am    
Are there any c++ reference? Thanks Mr. pwasser
[no name] 19-Mar-14 9:45am    
Your example is matlab code. Maybe it's time to discover Google.
Pervez03 19-Mar-14 14:58pm    
Ha ha exactly Mr. Pwasser

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900