Arduino串口输入整数存储与显示问题求助
Fixing Your Arduino Serial Input & Integer Storage Issues
Hey there, let's work through each of your problems and get your code working as expected! Here's a breakdown of what went wrong and how to fix it:
Why Your Current Code Isn't Working
Let's start with the root causes of your three issues:
- Single digits stored instead of full integers: You're reading each character one by one and storing them directly in the array. So "123" becomes three separate characters '1', '2', '3', which get converted to 1,2,3 instead of the full integer 123.
- Garbage output (乱码): In your else block, you're passing the newline character (
'\n') toConvertinteger, which doesn't handle non-digit characters—this leads to undefined values being printed, hence the garbage symbols. Also, you're trying to print a single converted digit instead of the full integer. - Space-separated inputs aren't counted correctly: Your code doesn't recognize spaces as separators between integers, so it would treat "123 234" as a stream of characters including the space, leading to wrong counts and invalid data.
The Fixed Code
Here's a revised version of your code that addresses all three issues. It reads full lines of input, splits them into space-separated integers, stores them in your array, and prints the count and values correctly:
const int MAX_INTEGERS = 500; int myint[MAX_INTEGERS]; int integerCount = 0; void setup() { Serial.begin(115200); while (!Serial) { ; // Wait for serial port to connect (for native USB boards) } Serial.println("Ready to accept input! Enter integers separated by spaces, then press Enter."); } void loop() { if (Serial.available() > 0) { String inputLine = Serial.readStringUntil('\n'); // Read full line until newline inputLine.trim(); // Remove leading/trailing whitespace (like spaces or newlines) if (inputLine.length() == 0) { Serial.println("Empty input! Please enter integers separated by spaces."); return; } integerCount = 0; int currentPos = 0; // Split the input line into individual number strings while (currentPos < inputLine.length() && integerCount < MAX_INTEGERS) { // Skip any leading spaces while (currentPos < inputLine.length() && inputLine.charAt(currentPos) == ' ') { currentPos++; } // Find the end of the current number int endPos = currentPos; while (endPos < inputLine.length() && isDigit(inputLine.charAt(endPos))) { endPos++; } // Extract the number substring and convert to integer if (endPos > currentPos) { String numStr = inputLine.substring(currentPos, endPos); myint[integerCount] = numStr.toInt(); integerCount++; } currentPos = endPos; } // Print the results Serial.println("\n--- Input Summary ---"); Serial.print("Size of input data = "); Serial.print(integerCount); Serial.println(" integers"); Serial.println("Input values:"); for (int i = 0; i < integerCount; i++) { Serial.print("myint["); Serial.print(i); Serial.print("] = "); Serial.println(myint[i]); } Serial.println("\nReady for next input! Enter integers separated by spaces, then press Enter."); } }
Key Improvements Explained
- Reading full lines: Using
Serial.readStringUntil('\n')grabs the entire input line at once, so we can process complete numbers instead of individual characters. - Splitting by spaces: The code skips spaces and extracts consecutive digit sequences as individual number strings, which are then converted to integers with
toInt(). - Proper counting: Each valid number string increments
integerCount, so "123 234" correctly counts as 2 integers. - No more garbage output: We're printing the full converted integers directly, so no undefined values or乱码.
- Array bounds protection: We check that we don't exceed the 500-integer limit of the array.
Testing the Code
- When you enter
123and press Enter, you'll see:--- Input Summary --- Size of input data = 1 integers Input values: myint[0] = 123 Ready for next input! ... - When you enter
123 234and press Enter:--- Input Summary --- Size of input data = 2 integers Input values: myint[0] = 123 myint[1] = 234 Ready for next input! ...
内容的提问来源于stack exchange,提问作者Hansama628
相关产品推荐
相关产品推荐

