USBのセンサから出​力されたバイナリデー​タの変換方法を教えて​下さい

조회 수: 2 (최근 30일)
Daisaiku Senoo
Daisaiku Senoo 2016년 12월 21일
댓글: Walter Roberson 2016년 12월 22일
USB出力のジャイロセンサをPCに接続して使用しています。 Matlabのコマンドウィンドウから、以下のようにRAWデータの要求を行いデータが返ってきました。 しかしながら、肝心な測定データがバイナリデータのため文字化けしてしまいました。
また、RAWデータフォーマットは以下のようになっています。
データ型:X(ASCII) U(Unsigned) I(Signed)
バイナリデータから測定値に変換する方法を教えてください。 よろしくお願いします。
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 12월 21일
편집: Walter Roberson 2016년 12월 21일
Approximate translation:
USB output gyro sensor is connected to the PC. From the command window of Matlab, we requested RAW data and returned the data as follows. However, since the measurement data which is important is binary data, it got garbled.
The RAW data format is as follows.

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 21일
You should use fread() rather than fscanf() when you are working with binary. You should also be reading a specific number of bytes, rather than looking for a line terminator, as the line terminator can accidentally occur in the binary. You should configure BytesAvailableFcnMode to 'bytes'.
  댓글 수: 2
Daisaiku Senoo
Daisaiku Senoo 2016년 12월 22일
Thank you for your answer. I tried fread(), and the result is as below. I think the data is follow the format table in my question. Now I'm thinking how I can know correct data of angle, velocity and acceleration because there are two values in each data.
Walter Roberson
Walter Roberson 2016년 12월 22일
The "Data Size" appears to start counting from the '$' but not to include the CR LF.
It appears that the data is in Big-Endian format, so you probably need to swapbytes() on the data.
angular_x = swapbytes( typecast(out(18:19), 'int8') );
angular_y = swapbytes( typecast(out(20:21), 'int8') );
angular_z = swapbytes( typecast(out(22:23), 'int8') );
acceleration_x = swapbytes( typecast(out(24:25), 'int8') );
acceleration_y = swapbytes( typecast(out(26:27), 'int8') );
acceleration_z = swapbytes( typecast(out(28:29), 'int8') );
and so on.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by