Serial port read response interference?

4 views (last 30 days)
Mark
Mark on 1 Jul 2014
Dear all,
We do use a Matlab GUI to communicate with a third party device over a serial port. In initialising the serial port communication in the opening function of the GUI we also add a TimerFcn to the serial object, to be called every 500 ms. This is a device requirement in order to maintain communication. The serial port commnunication settings as specified by the manufacturer are as follows:
  • Baud Rate: 9600 bps
  • Data: 8 bits
  • Stop: 1 bit
  • Parity: None
  • Flow Control: None
However whilst this TimerFcn is running and called every 500 ms various other commands are send over the serial port in order to enable / disable different functionalities of the device. The device responses have to be used to check proper functioning. However, it is these responses that are very 'unstable' in that they vary in length, and are therefore difficult to use. Sometimes the response is 10 characters, sometimes 20 and sometimes only 1 character. Sometimes it is made up of just the first x characters of the expected response, or gives the expected response but than repeated 3 times in one read out. The manufacturer of the device tells me it is a Matlab coding problem, as the device works as it should, but I am unable to work out what the problem would be. I can see the TimerFcn running in the background interfering with other commands send over the serial port and therefore disrupts the normal write/read cycle but I do not see a way to circumvent this. I have added an example of a function that is used for the serial communication to enable a certain function of the dev
Does anyone have any suggestions how to improve this?
Thanks!
% Send command
fprintf(serialPortObj, 'x@G')
% Wait for response
startTime = clock;
elapsed = 0;
while ~serialPortObj.BytesAvailable && elapsed < 0.5
currentTime = clock;
elapsed = etime(currentTime, startTime);
end
% Read response
if serialPortObj.BytesAvailable
outMain = fread(serialPortObj, serialPortObj.BytesAvailable)
end

Answers (0)

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!