【问题标题】:Sending String from Processing to Arduino Not Working将字符串从处理发送到 Arduino 不起作用
【发布时间】:2015-05-16 19:54:56
【问题描述】:

我在处理过程中收集了一些字符串信息,我正在尝试将其发送给 Arduino。我正在处理发送信息,但是,我在 arduino 中的输出很奇怪。我得到像“77789 ...”这样的数字。我不确定我做错了什么。我需要做的就是,基本上从处理中获取一个字符串并将其发送到 arduino 以将其显示在 LCD 屏幕上。

对此的任何帮助将不胜感激。

这是我的处理代码:

import processing.serial.*;

Serial myPort;

XML MSFTxml; // loading Query

XML MSFT; // results of the query
XML row; // first row in the query

XML symbol; // Name of the stock
String symbolTxt;


String val;

void setup() {
  size(200,200);
  myPort = new Serial(this,"COM3", 9600);

}

void draw() {

updateXMLCall();

delay(10000);
}
void updateXMLCall () {

   MSFTxml = loadXML("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%27http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3DMSFT%26f%3Dsl1d1t1c1ohgv%26e%3D.csv%27%20and%20columns%3D%27symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");  
   MSFT = MSFTxml.getChild("results"); //Getting the first tag "results" in the query MSFT
   row= MSFT.getChild("row"); //first child tag "row"
   symbol = row.getChild("symbol"); //name of the stock   
   symbolTxt = symbol.getContent().toString(); //converting the name of the stock into a string
   myPort.write(symbolTxt);
   println(symbolTxt);
}

这是我的 arduino 代码:

#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;

const int colorR = 50;
const int colorG = 0;
const int colorB = 0;

void setup()
{
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.setRGB(colorR, colorG, colorB);
  // Print a message to the LCD.
  lcd.setCursor(0, 1);
}

void loop()
{  
  String content = "";
  if (Serial.available()) {
    while (Serial.available()) {
      content += Serial.read();
      lcd.print(content);
      Serial.print(content);
      lcd.setCursor(0, 1);
    }
  }
  delay(100);
}

【问题讨论】:

  • 你的问题是 '+=' ,看这里解析通过串口发送的数据:stackoverflow.com/questions/5697047/…
  • @Ôrel 非常感谢!这有很大帮助:D 我让它工作了。我用 readStringUntil(/n);这似乎正是我正在寻找的东西
  • @Ôrel 你可能想把它变成一个答案而不是评论,所以它可以被接受,未来遇到同样问题的 SO 用户可以找到一个接受的答案 =)

标签: c string arduino processing


【解决方案1】:

问题是在 C 中使用 += += 不会连接字符串。 您需要连接来自Serial.read()char,例如这里: Convert serial.read() into a useable string using Arduino?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多