file io - Changing the BytesAvailableFcnCount during reading from a serial port in MATLAB -
i trying build serial port device in matlab. have 2 devices 'com1' , 'com2'. 'com1' asynchrounously writes data serial port 'com2'. have alternative names 'com1' , 'com2', 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 input buffer of testdummy triggers testdummyfunction 2, , specified using testdummy.bytesavailablefcncount field (below).
testdummy.bytesavailablefcnmode = 'byte'; testdummy.bytesavailablefcncount = 2; testdummy.bytesavailablefcn = @testdummycomfunction;
i have function "testdummyfunction" on testdummy side triggered using bytesavailable callback property in matlab.the structure of function follows:
function testdummyfunction(testdummy,bytesavailable) % testdummyfunction(testdummy,...bytesavailable) % inputs: % testdummy:refers serial port testdummy % bytesavailable:refers callback function 'bytesavailablefunction' global serialcom; data_string=fscanf(serialcom,'%c',2); %reads data sent form serialcom end
now, suppose string print testdummy greater 2 bytes, 10 bytes. since write data testdummy asynchronously, first time bytes available function triggered, 2 bytes read it.(these 2 bytes act syncbyte me, if correct, means can read rest).
now, want change testdummy.bytesavailablefcncount property 8; can read reamaining 8 bytes. however, matlab says must first close serial port change testdummy.bytesavailablefcncount property. if this, in input buffer lost! how still ensure change property , not lose data in input buffer?
you set bytesavailablefcncount 1. use variable global serialmode = 1;
switch between different modes:
function testdummyfunction(testdummy,bytesavailable) global serialcom; if (serialmode==1 && get(serialcom,'bytesavailable')>=2) data_string=fscanf(serialcom,'%c',2); %reads data sent serialcom elseif (serialmode==2 && get(serialcom,'bytesavailable')>=8) data_string2=fscanf(serialcom,'%c',8); %reads data sent serialcom end
alternatively, quicker solution if send 10 bytes set bytesavailablefcncount 10, read 2 8 bytes.
another quick solution set remote device send 8 bytes instead of 2 (you send 6 zeros after initial 2 sync bytes), , use bytesavailablefcncount = 8 everything.
Comments
Post a Comment