Main Content

readline

Read line of ASCII string data from Bluetooth device

Since R2020b

Description

data = readline(device) reads ASCII data until the first occurrence of the terminator from the Bluetooth® connection device and returns data as a string without the terminator. If the function is unable to return any data within the period specified by the Timeout property of device, it returns data as a 0-by-0 double []. The function suspends MATLAB® execution until the terminator is reached or a timeout occurs.

example

Examples

collapse all

Create a connection to a nearby Bluetooth device. In this example, the HC-06 Bluetooth module is configured as a loopback device.

device = bluetooth("HC-06")
device = 
  bluetooth with properties:

                 Name: "HC-06"
              Address: "98D331FB3B77"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

Check the default ASCII terminator.

device.Terminator
ans = 

    "LF"

Set the terminator to "CR" and write a string of ASCII data. The writeline function automatically appends the terminator to the data.

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

Write another string of ASCII data with the terminator automatically appended.

writeline(device,"world")

Since the device is configured as a loopback device, the data you write to the device is returned to MATLAB. Read a string of ASCII data. The readline function returns data until it reaches a terminator.

readline(device)
ans = 

    "hello"

Read a string of ASCII data again to return the second string that you wrote.

readline(device)
ans = 

    "world"

Clear the Bluetooth connection.

clear device

Input Arguments

collapse all

Bluetooth device connection, specified as a bluetooth object.

Example: data = readline(device) reads ASCII data from the Bluetooth connection device.

Version History

Introduced in R2020b