简体   繁体   English

arduino char数组序列

[英]arduino char array serial

I'm Having a little bit of trouble with this bit of code here for my arduino. 我在为我的arduino编写的这段代码有点麻烦。

Basically what I'm trying to do is send in a series of characters, turn it into an char array, and use that to run the rest of the program. 基本上,我想做的是发送一系列字符,将其转换为char数组,然后使用它来运行程序的其余部分。 I'm close because I know everything is working perfectly internally, but when I boot from the serial port the message isn't getting in. 我很亲密,因为我知道所有内部功能都很好,但是当我从串行端口启动时,消息并没有进入。

I assume that at this point it probably is how I am constructing the array... or some oddity. 我认为此时可能是我构造数组的方式……或有些奇怪。 Probably just a simple error in how I put the code together but I'm completely struck. 关于如何将代码组合在一起,可能只是一个简单的错误,但我完全被打击了。 (I was previously using a string, but because of how the arduino works with them, it pretty much makes using them for memory purposes impossible). (我以前使用的是字符串,但是由于arduino如何与它们配合使用,因此几乎不可能将它们用于存储目的)。

I'm using a java program (ardulink) to send the information into the program with a customized version I've edited. 我正在使用Java程序(ardulink)将信息与经过编辑的自定义版本一起发送到程序中。 So simply put, the input has to be a series of characters, and I need it stored in an array. 简而言之,输入必须是一系列字符,我需要将其存储在数组中。

void serialEvent ()
{
  int arrayPostion = 0;
  int i;
  int maxArraySize = 20;
  char CharArrayInLocal[20];
  while (Serial.available() && !stringComplete) 
  {
    char inChar = (char)Serial.read();
    CharArrayInLocal[arrayPostion] = inChar;
    arrayPostion++;
    if (inChar == '\n') 
    {
      stringComplete = true;
    }
  }
  for (int i = 0; i<=19; i++)
  {
    CharArrayIn[i] = CharArrayInLocal[i];
  }
}

This worked for me, 这对我有用

String CharArrayInLocal[20];
String inputString = "";         
boolean stringComplete = false;  
int i=0;

void serialEvent() {
while (Serial.available()) {

char inChar = (char)Serial.read();

    if (inChar == '\n') {

      CharArrayInLocal[i]=inputString;

      i++;
      stringComplete = true;
      inputString="";
    }
    else{
      inputString += inChar;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM