如何将处理中生成的字符串数组打印到Arduino

时间:2019-01-30 18:56:46

标签: string arduino processing

我正在尝试将处理中生成的字符串发送给Arduino,以便可以将其打印在LCD屏幕上。使用此方法,LCD上无任何显示。我尝试了各种方法,根据我的研究,我正在尝试的方法是:

#include <LiquidCrystal.h>

char inData[9]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,1);
Serial.begin(9600); // Start serial communication at 9600 bps
}

 char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
                               // there you know there is data
{
    if(index < 8) // One less than the size of the array
    {
        inChar = Serial.read(); // Read a character
        inData[index] = inChar; // Store it
        index++; // Increment where to write next
        inData[index] = '\0'; // Null terminate the string
    }
}

if (strcmp(inData,This)  == 0) {
    for (int i=0;i<8;i++) {
        inData[i]=0;
    }
    index=0;
    return(0);
}
else {
    return(1);
}
}

void loop() 
{
if(Serial.available() > 0){
lcd.print(inData[index]);
}
}

1 个答案:

答案 0 :(得分:0)

我不确定我的anwser,但这可能是因为indata在其均匀打印之前就被终止了。尽管我对回答感到不安,但希望能有所帮助。

终止:

    inData[index] = '\0'; // Null terminate the string