< >
Home » Arduino库教程 » Arduino库教程-Bridge-Console Read

Arduino库教程-Bridge-Console Read

Console Read

  • 这个例子介绍了yun设备通过Console.read()读取来自Bridge的数据,并且把它保存到一个字符串里。

  • 为了看控制台,在Port菜单里选择你yun的名字和IP地址,然后代开串口监视器。通过打开终端窗口且输入:ssh root@ yourYunsName.local 'telnet localhost 6571',然后按enter,你可以看到它。

  • 当运行这个例子时,确保你的电脑和yun设备是在同一个网络上。

硬件要求

  • Yún 开发板 或 shield

电脑和yun设备是在同一个网络

电路

  • 这个例子没有额外的电路
    请输入图片描述
    图由 Fritzing 软件绘制

样例代码

  • 包括Console库(来自Bridge)
#include <Console.h>

Create a string to hold the information from the Bridge
String name;

In setup() initialize the Bridge and Console. Wait for a Console connection, then ask for some information :

void setup() {
  Bridge.begin();
  Console.begin(); 

  while (!Console); 

  Console.println("Hi, what's your name?");
}
[Get Code]
In loop(), check to see if there are bytes in the Console buffer. If there's something available, read the the oldest character into a local variable.

void loop() {
  if (Console.available() > 0) {
    char c = Console.read(); 
 
[Get Code]
If the character is a newline ("\n"), it is the last character in the incoming string. Print out the string to the Console, ask for more information, and clear the string.

if (c == '\n') {
      Console.print("Hi ");
      Console.print(name);
      Console.println("! Nice to meet you!");
      Console.println();
      Console.println("Hi, what's your name?");
      name = ""; 
    } 
}
[Get Code]
If the character in the buffer is not a newline, add it to the end of the string.

else {      
      name += c; 
    }
  }
}
[Get Code]
The complete sketch is below :

/*
Console Read example for Yún101/YunShield/Yún

 Read data coming from bridge using the Console.read() function
 and store it in a string.

 To see the Console, pick your Yún's name and IP address in the Port menu
 then open the Port Monitor. You can also see it by opening a terminal window
 and typing:
 ssh root@ yourYunsName.local 'telnet localhost 6571'
 then pressing enter. When prompted for the password, enter it.

 created 13 Jun 2013
 by Angelo Scialabba
 modified 16 June 2013
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/ConsoleRead

 */

#include <Console.h>

String name;

void setup() {
  // Initialize Console and wait for port to open:
  Bridge.begin();
  Console.begin();

  // Wait for Console port to connect
  while (!Console);

  Console.println("Hi, what's your name?");
}

void loop() {
  if (Console.available() > 0) {
    char c = Console.read(); // read the next char received
    // look for the newline character, this is the last character in the string
    if (c == '\n') {
      //print text with the name received
      Console.print("Hi ");
      Console.print(name);
      Console.println("! Nice to meet you!");
      Console.println();
      // Ask again for name and clear the old name
      Console.println("Hi, what's your name?");
      name = "";  // clear the name string
    } else {     // if the buffer is empty Cosole.read() returns -1
      name += c; // append the read char from Console to the name string
    }
  } else {
    delay(100);
  }
}

[Get Code]

更多

  • Bridge: 从网页浏览器进入开发板的引脚。
  • Console ASCII Table: 示范了怎样打印多种格式到控制台。
  • Console Pixel: 通过控制台控制一个LED灯。
  • Console Read: 从控制台那里分析信息,然后重复发送返回。
  • Datalogger: 在SD卡上保存传感器信息。
  • File Write Script: 示范怎样在Process上写入和执行外壳脚本。
  • HTTP Client: 建造一个简单的客户端,可以下载网页并且打印到串口监视器。
  • HTTP Client Console: 建造一个简单的客户端,可以下载网页并且用控制台通过WIFI打印到串口监视器。
  • Mailbox Read Messages: 用REST API通过一个网页发送文本信息到。
  • Process: 示范怎么用Process运行 Linux 命令。
  • Remote Due Blink: 示范怎么远程上传程序到DUE开发板上。
  • Shell Commands: 用Process 来运行 shell 命令。
  • SpacebrewYun: 在Arduino IDE软件的例子上看更多关于 Spacebrew 文档信息。
  • Temboo: 在Arduino IDE软件的例子上看更多关于 Temboo 文档信息。
  • Temperature Web Panel: 当浏览者要求时,粘贴传感数据到网页上。
  • Time Check: 从网络的时间服务器获得时间,并且打印到串口监视器。
  • WiFi Status: 运行一个预配置的脚本,报告返回当前wifi网络的强度。
  • Yun First Config: 用串口监视器不费力地连接你的云产品到wifi网络,并且在上面回答一些简单的问题。
  • Yun Serial Terminal: 通过串口监视器进入Linux终端。

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: arduino库教程, arduino console read