read
Bluetooth デバイスからのデータの読み取り
説明
例
Bluetooth デバイスでのデータの読み取りと書き込み
近くの Bluetooth デバイスへの接続を作成します。この例では、HC-06 Bluetooth モジュールはループバック デバイスとして設定されます。
device = bluetooth("HC-06")
device = bluetooth with properties: Name: "HC-06" Address: "98D331FB3B77" Channel: 1 NumBytesAvailable: 0 NumBytesWritten: 0 Show all properties
値 1:10
をデバイスに書き込みます。
write(device,1:10)
デバイスがループバック デバイスとして設定されているため、デバイスに書き込むデータは MATLAB に返されます。すべてのデータを読み取ります。
read(device,10)
ans = 1×10 1 2 3 4 5 6 7 8 9 10
Bluetooth デバイスで書き込みおよび読み取りするデータ型の指定
近くの Bluetooth デバイスへの接続を作成します。この例では、HC-06 Bluetooth モジュールはループバック デバイスとして設定されます。
device = bluetooth("HC-06")
device = bluetooth with properties: Name: "HC-06" Address: "98D331FB3B77" Channel: 1 NumBytesAvailable: 0 NumBytesWritten: 0 Show all properties
string "helloworld"
をデバイスに書き込みます。
write(device,"helloworld","string")
デバイスがループバック デバイスとして設定されているため、デバイスに書き込みまれるデータが MATLAB に返されます。データの最初の 5 つの値を string として読み取ります。
read(device,5,"string")
ans = "hello"
read
コマンドをもう一度使用して、データの次の 5 つの値を読み取ります。
read(device,5,"string")
ans = "world"
入力引数
device
— Bluetooth デバイスの接続
bluetooth
オブジェクト
Bluetooth デバイスの接続。bluetooth
オブジェクトとして指定します。
例: data = read(device,5)
は、Bluetooth 接続 device
からデータを読み取ります。
count
— 読み取る値の数
数値
読み取る値の数。正の整数値として指定されます。count
が device
の NumBytesAvailable
プロパティより大きい場合、関数は MATLAB の実行を一時停止し、指定された量のデータが読み取られるかタイムアウトが発生するまで待機します。
例: read(device,2)
は uint8
データの 2 つの値を読み取ります。
データ型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
datatype
— 各値のサイズと形式
"uint8"
(既定値) | "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "single"
| "double"
| "char"
| "string"
各値のサイズと形式。文字ベクトルまたは string として指定します。datatype
は、各値について読み取るバイト数と、MATLAB データ型としてのそれらのバイトの解釈を決定します。
例: read(device,1,"uint16")
は、uint16
データの 1 つの値を読み取ります。各 uint16 値は 2 バイトです。
データ型: char
| string
バージョン履歴
R2020b で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)