s=serial('COM1'); % Create a serial port object
%Configure property values
s.InputBufferSize=50000;
s.OutputBufferSize=50000;
s.BaudRate=9600;
fopen(s); %open the port
%flush the input buffer
flushinput(s);
pause(0.01);
% Here the beginning of the sending binary data -----------------------------------------------------------
binary_data_in_8bits=00001100;%12 in decimal
decimal_data=bin2dec(binary_data_in_8bits)% conversion of the data from binary to decimal
fwrite(s,decimal_data,'uint8','async');
%End of sending code-------------------------------------------------------------------------------------
% Here the beginning of the reading binary data -----------------------------------------------------------
index = 1;
nb_bytes = get(s,'BytesAvailable');
while (index < get(s,'inputBuffersize'))
if nb_bytes
out= fread(s, nb_bytes,'uint8');
index = index + nb_bytes;
end;
pause(0.2);
end;
%End of reading code-------------------------------------------------------------------------------------------
%Disconnect and clean up
fclose(s);
delete(s);
clear s;