function [data_Re, data_Im, data_all_Re, data_all_Im] ...
					= f_rp_tran_acq( ...
							data_in_Re, data_in_Im, cmplx_data, data_in_samples, ...
							out_width, txrx_no, bytes_to_read, thld, host, port)
%%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
		%Inputs:
		% - data_in_Re = Real part of input data
		% - data_in_Im = Imaginary part of input data
		% - cmplx_data = if 1, then Real and Complex part of number must be
		% passed as input argument, data are then interleaved
		% - out_width = Width of data to be send over TCP in number of bits
		% included sign
		% - txrx_no = Number of transmit and receive loops
		% - bytes_to_read = Number of Bytes to read from TCP server
		% - thld = Treshold Voltage received data
		% - host = IP address of TCP host
		% - port = TCP port of host
		%Outputs:
		% - data_Re = Real part of valid received data
		% - data_Im = Imaginary part of received valid data
		% - data_all_Re = Real part of all received data
		% - data_all_Im = Imaginary part of all received data
		
%% Convert input data into string
%Convert Voltage (double) to out_width-1 bit wide values 
%(out_width-1 = without sign bit)
data_Re_i16 = int16((2^(out_width-1)-1)*data_in_Re);
if cmplx_data
	data_Im_i16 = int16((2^(out_width-1)-1)*data_in_Im);
else
	data_Im_i16 = int16(0);
end
%Preallocate
data_ReIm = int16(zeros(1, 2*numel(data_Re_i16)));
%Data interleaving
data_ReIm(1, 1:2:end) = data_Re_i16;
data_ReIm(1, 2:2:end) = data_Im_i16;

%Convert data to uint8
data_u8 = typecast(data_ReIm, 'uint8');
%Convert data to string of ascii characters
data_str = char(data_u8);

%% Open connection with your Red Pitaya
%pnet MEX C library

con = pnet('tcpconnect', host, port);

if con==-1
	error 'Bad IP or server down...'; 
end
disp(['Connected to: ' host]);

pnet(con,'setwritetimeout',1);
pnet(con,'setreadtimeout',10);
data_all_Re = 0;
data_all_Im = 0;
for i=1:txrx_no
	%Send data to server
	pnet(con, 'write', data_str);
	
	%Receive data from server
	data_str_rx = (pnet(con,'read',bytes_to_read));
	%Convert string to unsigned 8bit integer
	data_ui8 = uint8(mod(data_str_rx,256));
	%Convert data to 16 bit signed integer
	data_i16 = typecast(data_ui8, 'int16');
	%Read interleaved data
	data_i16_re = data_i16(1:2:end);
	data_i16_im = data_i16(2:2:end);
	%Convert integer values to Voltage double numbers
	data_all_Re = [data_all_Re, 100.*double(data_i16_re)./(2^(out_width-1)-1)];
	data_all_Im = [data_all_Im, 100.*double(data_i16_im)./(2^(out_width-1)-1)];
	
end

pnet(con,'close');

idx_re = find(data_all_Re > thld, 1);
data_Re = data_all_Re(idx_re:idx_re+data_in_samples);

%idx_im = find(data_all_Im > thld, 1);
data_Im = data_all_Im(idx_re:idx_re+data_in_samples);




end

