Get only numbers from serial port without any str2num() conversion

조회 수: 1 (최근 30일)
Hi all!
In the serial com port channel values comes from 0 to 1023, this example from Arduino: https://monosnap.com/file/TiTehc6zWhu0zszPnCALbnjIMAfpzf
On the MATLAB side I parse it via function:
function sPs = GetsP()
global obj1;
sPs = 0;
while(obj1.BytesAvailable)
Psvoltage = fscanf(obj1);
if (~isempty(Psvoltage))
sPs = str2num(Psvoltage) * (100 / 1023);
break;
end
end
But I dont like this variant, because str2num() conversion is very slow. If I try to use Psvoltage = fread(obj1, 1, 'ushort'); I didnt see correct data, because fread() reads binary data.
So the question is: How can I try to read direct number data, without any slow string to number conversion? How can I need to use fread() to get numbers?
  댓글 수: 3
Sergey Makovkin
Sergey Makovkin 2016년 3월 28일
Arduino outputs a fluorescence value from a photomultiplier tube via COM port. The image has been created on the scanner by this data. We design two-photon laser microscope for medical purposes, therefore, a very important data transmission speed. Slow place here is the serial port, but these are the conditions of the project.
Walter Roberson
Walter Roberson 2016년 3월 28일
Reprogram the arduino code to send uint16 instead of text. You only need 10 bits of the 16, so you could even pack in some flags like "Beginning of Row" to help synchronize. Or you could take advantage of the extra 6 bits to add error correcting codes to make it easier to recover from potential dropped bytes.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 25일
str2num() uses a full eval() . str2double() is faster. You can also use fscanf with a format specifier,
fscanf(obj1, '%d')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by