Output (looks random to me):
63
51
I received:
63
51
I received:
63
154
I received:
63
154
I received:
63
211
I received:
63
211
I received:
63
243
I received:
Notes: I am not typing anything into the comport. Serial.available is always > 0 with a value of 63 and serial.read seems random.
Tried: I went to using another board for the serial communication. (IE: Hooked a second board into pc and hooked second board rx and tx to the board in question(board 1) ) The data looks the same regardless if rx is connected.
Serial.println(analog value) works flawlessly...So im not sure exactly what the problem is stemming from.
[quote]Desired result: I type a number in the comport, hit enter, the comport replies the number back to me.[/quote]
So, that's exactly what your code is doing. Sort of.
[code] Serial.println(Serial.available());
Anonymous output has always irritated me. This is no exception.
Serial.println(Serial.read());
Serial.read() returns an int, not a char. Serial.print(), given an int, converts it to a string. Given a char, it does something completely different. If you expect a char output, do not give the function an int.
Serial.print("I received: \n");
Why would you print this AFTER you printed what you got?[/code]
I can't help but think that the 63 you keep seeing has something to do with the Serial buffer size, which is 64. Try modifying the code to use Serial.ReadBytesUntil(), passing in a newline character ('\n'), and a buffer that you have allocated for 70 chars (e.g., char buffer[70]) and then print out what you get.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
/*Very simple serial test code. Copy and paste into the IDE code
area. Upload via clicking the IDE upload arrow. When upload
finished, open the serial monitor by clicking the serial monitor
icon in the upper right of the IDE. Ensure 9600 baud rate is
selected in the serial monitor. Type something in the serial
monitor text box and send. What was typed in the text box should
be sent back to the serial monitor display. */
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}