Main Content

configureTerminator

为与 Bluetooth 设备的 ASCII 字符串通信设置终止符

自 R2020b 起

说明

示例

configureTerminator(device,terminator) 定义与指定的 Bluetooth® 设备进行读写通信的终止符。允许的终止符值是 "LF"(默认值)、"CR""CR/LF" 以及 0255 的整数值。该语法设置 deviceTerminator 属性。

设置终止符后,使用 writelinereadline 写入和读取以 ASCII 字符结尾的字符串数据。

示例

configureTerminator(device,readterminator,writeterminator) 为读取和写入通信定义单独的终止符。

示例

全部折叠

创建到附近的 Bluetooth 设备的连接。

device = bluetooth("TestDevice")
device = 
  bluetooth with properties:

                 Name: "TestDevice"
              Address: "BC275C50533C"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

将读取终止符和写入终止符都设置为 "CR/LF"

configureTerminator(device,"CR/LF")

确认更改。

device.Terminator
ans = 
"CR/LF"

创建到附近的 Bluetooth 设备的连接。

device = bluetooth("TestDevice")
device = 
  bluetooth with properties:

                 Name: "TestDevice"
              Address: "BC275C50533C"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

将读取终止符设置为 "CR",将写入终止符设置为 10

configureTerminator(device,"CR",10)

确认更改。

device.Terminator
ans=1×2 cell array
    {["CR"]}    {[10]}

数组中的第一个元素是读取终止符,第二个元素是写入终止符。

创建到附近的 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

检查默认的 ASCII 终止符。

device.Terminator
ans = 

    "LF"

将终止符设置为 "CR",并写入一个 ASCII 数据字符串。writeline 函数自动将终止符追加到数据中。

configureTerminator(device,"CR")
writeline(device,"hello")

写入自动追加了终止符的另一个 ASCII 数据字符串。

writeline(device,"world")

由于设备配置为环回设备,写入设备的数据将返回到 MATLAB®。读取一个 ASCII 数据字符串。readline 函数返回数据,直到到达终止符。

readline(device)
ans = 

    "hello"

再次读取一个 ASCII 数据字符串,以返回您写入的第二个字符串。

readline(device)
ans = 

    "world"

清除 Bluetooth 连接。

clear device

输入参数

全部折叠

Bluetooth 设备连接,指定为 bluetooth 对象。

示例: configureTerminator(device,"CR") 设置 Bluetooth 连接 device 的终止符值。

读取和写入通信的 ASCII 终止符,指定为 "LF""CR""CR/LF" 或 0 到 255 之间的一个整数数值。当为读取和写入设置相同的终止符时,请使用此形式。当从终止符值为 "CR/LF" 的设备中读取时,读取会在同时出现 CRLF 时终止。当写入终止符值为 "CR/LF" 的设备时,写入通过同时添加 CRLF 这两者来终止。此输入参量设置 Terminator 属性。

示例: configureTerminator(device,"CR/LF") 将读取终止符和写入终止符都设置为 "CR/LF"

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

读取或写入通信的 ASCII 终止符,指定为 "LF""CR""CR/LF" 或 0 到 255 之间的一个整数数值。当为读取和写入设置不同的终止符时,请使用此形式。当从终止符值为 "CR/LF" 的设备中读取时,读取会在同时出现 CRLF 时终止。当写入终止符值为 "CR/LF" 的设备时,写入通过同时添加 CRLF 这两者来终止。此输入参量设置 Terminator 属性。

示例: configureTerminator(device,"CR",10) 将读取终止符设置为 "CR",将写入终止符设置为 10

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

版本历史记录

在 R2020b 中推出