2019-12-02 05:58:26 +01:00
|
|
|
# receive String
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
Find the next string sent by radio from another @boardname@.
|
2016-05-25 03:23:33 +02:00
|
|
|
|
|
|
|
```sig
|
|
|
|
radio.receiveString()
|
|
|
|
```
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~ hint
|
|
|
|
|
|
|
|
**Deprecated**
|
|
|
|
|
|
|
|
This API has been deprecated! Use [on received string](/reference/radio/on-received-string) instead.
|
|
|
|
|
|
|
|
## ~
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Returns
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
* the first [string](/types/string) that was sent. If no
|
2016-07-18 23:04:15 +02:00
|
|
|
string was sent, then this function returns an empty (blank) string.
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example: Simple receiver
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
Show the string sent by another @boardname@.
|
2016-05-25 03:23:33 +02:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
radio.onDataReceived(() => {
|
|
|
|
basic.showString(radio.receiveString());
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example: Two-way radio
|
2016-05-27 21:03:54 +02:00
|
|
|
|
2016-11-02 01:44:37 +01:00
|
|
|
If you load this program onto two or more @boardname@s, you can send a code word from one of them to the others by pressing button `A`.
|
|
|
|
The other @boardname@s will receive the code word and then show it.
|
2016-05-27 21:03:54 +02:00
|
|
|
|
|
|
|
```blocks
|
2020-08-19 22:03:58 +02:00
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-05-27 21:03:54 +02:00
|
|
|
radio.sendString("Codeword: TRIMARAN")
|
|
|
|
basic.showString("SENT");
|
|
|
|
})
|
|
|
|
|
|
|
|
radio.onDataReceived(() => {
|
|
|
|
basic.showString(radio.receiveString());
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~hint
|
2016-05-27 21:03:54 +02:00
|
|
|
|
|
|
|
A radio that can both transmit and receive is called a _transceiver_.
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## ~
|
2016-05-27 21:03:54 +02:00
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## Example: Mood radio
|
2016-05-27 21:03:54 +02:00
|
|
|
|
|
|
|
This is a simple program to send whether you are happy or sad over ```radio```.
|
|
|
|
Use the `A` or `B` button to select an emotion.
|
|
|
|
|
|
|
|
This program will also receive your friend's mood.
|
2016-05-25 03:23:33 +02:00
|
|
|
|
|
|
|
```blocks
|
|
|
|
let data: string = "";
|
2020-08-19 22:03:58 +02:00
|
|
|
input.onButtonPressed(Button.A, () => {
|
2016-05-25 03:23:33 +02:00
|
|
|
radio.sendString("H");
|
|
|
|
});
|
2020-08-19 22:03:58 +02:00
|
|
|
input.onButtonPressed(Button.B, () => {
|
2016-05-25 03:23:33 +02:00
|
|
|
radio.sendString("S");
|
|
|
|
});
|
|
|
|
radio.onDataReceived(() => {
|
|
|
|
data = radio.receiveString();
|
|
|
|
if ("H" == data) {
|
|
|
|
basic.showLeds(`
|
|
|
|
. . . . .
|
|
|
|
. # . # .
|
|
|
|
. . . . .
|
|
|
|
# . . . #
|
|
|
|
. # # # .
|
|
|
|
`);
|
|
|
|
} else if ("S" == data) {
|
|
|
|
basic.showLeds(`
|
|
|
|
. . . . .
|
|
|
|
. # . # .
|
|
|
|
. . . . .
|
|
|
|
. # # # .
|
|
|
|
# . . . #
|
|
|
|
`);
|
|
|
|
} else {
|
|
|
|
basic.showString("?");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2019-12-02 05:58:26 +01:00
|
|
|
## See also
|
2016-05-25 03:23:33 +02:00
|
|
|
|
2016-05-27 21:03:54 +02:00
|
|
|
[send string](/reference/radio/send-string), [on data received](/reference/radio/on-data-received)
|
2016-08-09 17:28:08 +02:00
|
|
|
|
|
|
|
```package
|
2016-10-23 06:29:31 +02:00
|
|
|
radio
|
2016-08-09 17:28:08 +02:00
|
|
|
```
|