仅限Arduino只读一个字符。(Arduino Only Reads only one character.)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
仅限Arduino只读一个字符。(Arduino Only Reads only one character.)

该程序用于读取输入,并在Arduino的串行监视器中写出。 它只在串行监视器中写入一个字符的问题。

void setup() { Serial.begin(9600); //Set the serial monitor. lcd.begin(16, 2); //Set the LCD } // Alignment variables boolean left = true; //Set boolean left to true to begin to display text in middle of screen. boolean right = false; //Other possible align booleans set to false boolean select = false; //Text show/hide variables boolean show1 = true; //Both values set to true to display on start up. boolean show2 = true; //Serial input char serialinput [4] = {0}; //For 3 value input, and null character to end. char line1; void loop() { if (Serial.available() > 0) { //If the serial monitor is open it will read a value. line1 = Serial.read(); Serial.print(line1); memmove (serialinput, &serialinput[1], 3); //copy the value to memory serialinput [2] = Serial.read(); //value is read. //if statements for each possible input. }

This program is meant to read an input, and write it out in the Arduino's serial monitor. The issue that it only write one character in the serial monitor.

void setup() { Serial.begin(9600); //Set the serial monitor. lcd.begin(16, 2); //Set the LCD } // Alignment variables boolean left = true; //Set boolean left to true to begin to display text in middle of screen. boolean right = false; //Other possible align booleans set to false boolean select = false; //Text show/hide variables boolean show1 = true; //Both values set to true to display on start up. boolean show2 = true; //Serial input char serialinput [4] = {0}; //For 3 value input, and null character to end. char line1; void loop() { if (Serial.available() > 0) { //If the serial monitor is open it will read a value. line1 = Serial.read(); Serial.print(line1); memmove (serialinput, &serialinput[1], 3); //copy the value to memory serialinput [2] = Serial.read(); //value is read. //if statements for each possible input. }

最满意答案

Serial.read不等待字符串结束。 如果你试图读取一些字符(由[4]暗示),你只需要在serial.read之后包含一个短延迟(),以便有足够的时间让缓冲区完全转储字符串。

if (Serial.available() > 0) { //If the serial monitor is open it will read a value. line1 = Serial.read(); delay(100); Serial.print(line1);

Serial.read doesn't wait for the end of a string. If you're trying to read a few characters (implied by [4]) you only need to include a short delay() after serial.read to allow enough time for the buffer to completely dump the string.

if (Serial.available() > 0) { //If the serial monitor is open it will read a value. line1 = Serial.read(); delay(100); Serial.print(line1);

更多推荐

本文发布于:2023-04-24 12:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/58a27caf527276a440687cfb52b965cc.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:仅限   字符   character   Arduino   Reads

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!