function serial_com(time_array,com_port,cycles,too_fast,too_slow)
%serial_com transmits the necessary information to the PIC to control the
%   arms motion
%time_array is the array of delay times (us)
%   positive for forward direction
%   negative for backward direction
%   magnitude is magnitude of delay in us
%com_port is the output port of the rs232 cable (eg. 'COM4')
%cycles is the number of times you want the time_array to be called in PIC
%   which is the number of times the arm does one cycle of its motion
%too_fast controls how how this program handles velocities that are too
%   large
%   enter 0 if you want it to output an error if velocity is too fast
%   enter 1 if you want it to replace velocity with max velocity
%   any other inputs will default to 1
%too_slow controls how how this program handles velocities that are too
%   small
%   enter 0 if you want it to output an error if velocity is too slow
%   enter 1 if you want it to replace velocity with min velocity
%   any other inputs will default to 1

%set system limits
min_delay_time=3000; %determined by how fast motor can go without skipping
max_delay_time=2^16-1; %determined by max variable length for delay time in PIC
max_num_cycles=2^16-1; %determined by max variable length for delay time in PIC
max_array_length=500; %determined by max array length for delay times in PIC
pause_time=0.01; %lenghts of puases in transmition

%make sure cycle number is vaild
if cycles~=fix(cycles) %make sure cycle number is an integer
    Warning('Inputed cycle amount is not an integer.  Program will run with this value rounded to nearist integer.');
    cycles=fix(cycles);
end
if cycles > max_num_cycles %make sure cycles number is not too large
    Warning('Inputed cycle amount is too large.  Program will set cycle number to largest cycle number possible.');
    cycles = max_num_cycles;
end
%parse cycle number to a string
time_str=num2str(cycles,'%05u');

%find array length and change it to string
alength=length(time_array);
time_array=sign(time_array).*(abs(time_array)-1000);%make sure times take into account 1000us dealy already in PIC code
length_send_char=num2str(alength,'%05u');
if alength>max_array_length %make sure array length is not too large
    error('Inputed array is too long, please try an array with a shorter period!');
end

%set delay time and direction arrays
direction_sum=0;
for iter=1:alength
    if abs(time_array(iter))<min_delay_time %make sure velocity is not too fast
        if too_fast == 0
            error('Inputed velocity is too high, ABORT!  Inputed Time Delay=%d  Min Allowable Time Delay = %d\n',time_array(iter),min_delay_time+1000);
        else
            warning('Inputed velocity is too high, will replace with highest velocity possible. Inputed Time Delay=%d  Min Allowable Time Delay = %d\n',time_array(iter)+sign(time_array(iter))*1000,min_delay_time+1000);
            if time_array(iter) < 0 %make sure min_delay_time has right direction sign
                time_array(iter)=-min_delay_time;
            else
                time_array(iter)=min_delay_time;
            end
        end
    elseif abs(time_array(iter))>max_delay_time %make sure velocity is not too slow
        if too_slow == 0
            error('Inputed velocity is too slow, ABORT! Inputed Time Delay=%d  Max Allowable Time Delay = %d\n',time_array(iter),max_delay_time+1000);
        else
            warning('Inputed velocity is too slow, will replace with slowest velocity possible. Inputed Time Delay=%d  Max Allowable Time Delay = %d\n',time_array(iter),max_delay_time+1000);
            if sign(time_array(iter)) < 0 %make sure max_delay_time has right direction sign
                time_array(iter)=-max_delay_time;
            else
                time_array(iter)=max_delay_time;
            end
        end
    end
    %parse delay time to string
    list_t_str(iter,:)=num2str(abs(time_array(iter)),'%05u');
    %set direction
    if time_array(iter)>0
        direction_str(iter,:)='1';
        direction_sum=direction_sum+1;
    else
        direction_str(iter,:)='0';
        direction_sum=direction_sum-1;
    end
    time_array(iter);
    direction_str(iter,:);
end

if direction_sum~=0
    error('Forward and backward distances are not the same!');
end

est_time=((alength*6+10)*pause_time)/60*2.7;
fprintf('Estimated Transmition Time = %3.2f min\n',est_time);

%send information to pic
s = serial(com_port,'BAUD',19200);% Create serial object (PORT Dependent)
fopen(s)% Open the serial port for r/w
for j=1:5 %send array length
    fprintf(s, '%s', length_send_char(j));
    %fscanf(s);
    pause(pause_time);
end
fprintf('Finished sending array length\n');
for i=1:alength %send delay time
    for j=1:5
        fprintf(s, '%s', list_t_str(i,j));
        %fscanf(s);
        pause(pause_time);
    end
end
fprintf('Finished sending delay time array\n');
for i=1:alength %send direction
    fprintf(s, '%s', direction_str(i));
    direction_str(i);
    %fscanf(s);
    pause(pause_time);
end
fprintf('Finished sending direction array\n');
for i=1:5 %send cycle number
    fprintf(s, '%s', time_str(i));
    %fscanf(s);
    pause(pause_time);
end
fprintf('Finished sending cycle number\n');
fprintf('Finished transmition\n');
fclose(s);% Close the serial port
delete(s);% Delete the serial object