What read() function with <<1,'uint8'>> arguments returns afterall?

5 ビュー (過去 30 日間)
José Gama
José Gama 2020 年 1 月 9 日
コメント済み: Walter Roberson 2020 年 1 月 10 日
I'm trying to read data from a serial port.
So, my code is something like this:
ser = serialport('COM2',2400);
ser.DataBits = 8;
ser.StopBits = 1;
ser.ByteOrder = 'big-endian';
data = read(ser,1,'uint8');
My Question: What data type 'data' is?
From what I thought it would be an unsigned integer from 0 to 128.
But then when I make de2bi(data) it shows an error saying: "Error using bi2de>validateInputs (line 44). Expected input number 1, binary vector b, to be nonempty.".
Is perahps read() function returning some kind of data array?!
Thanks for help!
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 1 月 9 日
The read() is not returning any data.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2020 年 1 月 10 日
When you call the serialport function then call read on the ouput of that function, you're calling this version of the read function. According to that documentation page "For all numeric precision types, data is a row vector of double values. For the text type precision values of 'char' or 'string', data is of the specified type."
But based on the text of the error message you received I suspect Walter is correct, that it is an empty row vector because the serialport object timed out and so didn't return any data.
  2 件のコメント
José Gama
José Gama 2020 年 1 月 10 日
So does that mean I'm still "thinking" right with this code implementation? I note when aplying de2bi, I must index the value I want from that vector so it will be like:
dataBits = de2bi(data(1));
Am I right?
All because I'm not actually using the hardware right now, I'm using Virtual Serial Port Driver to emulate some data flowing in to test the script while I don't have access to the laboratory. Maybe there's a problem with it but that's OK... I just want the code to be working fine...
Thank you and Walter!
Walter Roberson
Walter Roberson 2020 年 1 月 10 日
You should test the result of read() for empty so your code will not crash when you go to index the variable.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by