سؤال

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 (belo).

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); %Reads the data sent form serialcom

end

Now, suppose I print a string whose length is greater than 2 bytes (say 10 bytes) ASYNCHRONOUSLY into the testdummy side from the serialcom side. Although I know that testdummyfunction is triggered when there are 2 bytes in the input buffer of testdummy, will the data_string also contain a string that is 2 bytes long ALWAYS? If not, how can I ensure that it only reads 2 bytes of data?

هل كانت مفيدة؟

المحلول

You can add an extra size argument to fscanf:

fscanf(serialcom,'%c',2);

Use doc serial.fscanf to get the full set of optional arguments for the function.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top