I am trying to build a serial port device in Matlab. I have 2 devices 'COM1' and 'COM2'. 'COM1' ASYNCHROUNOUSLY writes data into the serial port 'COM2'. I have alternative names for 'COM1' and 'COM2', as follows:
global serialcom
serialcom=serial('COM1'); %Serial Communication portal COM 1
global testdummy
testdummy=serial('COM2'); %Serial Communication portal COM 2
The number of Bytes in the input buffer of testdummy which triggers the testdummyfunction is 2, and this is specified using the testdummy.BytesAvailableFcnCount field (below).
testdummy.BytesAvailableFcnMode = 'Byte';
testdummy.BytesAvailableFcnCount = 2;
testdummy.BytesAvailableFcn = @testdummycomfunction;
I have a function "testdummyfunction" on the testdummy side which is triggered using the BytesAvailable callback property in Matlab.The structure of this function is as follows:
function testdummyfunction(testdummy,BytesAvailable)
% TESTDUMMYFUNCTION(testdummy,...BytesAvailable)
% INPUTS:
% TESTDUMMY:refers to the serial port testdummy
% BYTESAVAILABLE:Refers to the callback function 'BytesAvailablefunction'
global serialcom;
data_string=fscanf(serialcom,'%c',2); %Reads the data sent form serialcom
end
Now, suppose the string which i print into testdummy is greater than 2 bytes, say 10 bytes. Since i write the data into testdummy asynchronously, the first time the bytes available function is triggered, 2 bytes are read from it.(these 2 bytes act like a syncbyte for me, if they are correct, it means that i can read the rest).
Now, i want to change the testdummy.BytesAvailableFcnCount property to 8; so that i can read the reamaining 8 bytes. However, Matlab says that i must first close the serial port to change the testdummy.BytesAvailableFcnCount property. If i do this, then everything in my input buffer is lost! How do i still ensure that i change the property and not lose the data in the input buffer?