Displaying Received serial data

Hi
I was testing out a stand alone FTDI board to send data between the Arduino and a PC
not via the USB lead on the Arduino

I wanted to send a character from a terminal program on the PC , to be echoed back by the
Arduino to be displayed on the terminal on the PC .

The connection is working fine , I send data , and it sends it back but the problem is , I
am not seeing what I expected to see

Here is the code

int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
}

void loop() {

	// send data only when you receive data:
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();
                // say what you got:
		Serial.print("I received: ");

		Serial.println(incomingByte);
	}
}

When I send a capital letter "A" ( without quotes)
I see on the monitor ....

I received : 66

Firstly I wanted to see a letter "A" just like I sent so I guess I need to format it in some way
so it shows the character not its ascii code on the terminal.

Secondly and something I cant figure out is "A" has ascii code 65 not 66 which is what I get,
all the codes are incremented by one

can you tell me why this happens and how to recode it so it returns the letter I send

Regards
Don

either declare:

char incoming_byte;

then the print will print out the character,

or cast the int to a char, e.g. Serial.print((char) incoming_byte);
The cast is not good practice really, but useful for debugging.

As for why the code is one out, I have no idea - is it always one out for every character? Is your computer set up 9600, 8 bit, no parity?

how to recode it so it returns the letter I send

The Serial class derives from the Print class. The Print::print() method has several overloads, to print different kinds of data. You are using int overload (because you call the function with an int) and expecting the function to print a char. You need to either store the value returned by Serial.read() in the appropriate type variable (char), or cast the int to a char when printing.

As far as the off-by-one issue, you need to tell us more about this:

I wanted to send a character from a terminal program on the PC

Which terminal program? How is the serial communication configured?

Try using the Serial Monitor, to see if the problem manifests itself there, too.

Thank you both for your replies

I didnt know about casting to a different variable type , so I tried both ideas , declare the variable as a char
and that worked fine , and also casting it as Serial.println((char)incomingByte); which of course also worked

as for why I got 66 as a value when it should be 65, well now I feel quite silly . let me explain.

I have a piece of kit that has 3 FT2232D chips in it. It gives me a usb lead to the PC and on plugging it in
I get 6 comms ports , 2 off each chip .
I have one of the comms ports connected to pins 0 and 1 on the arduino
I call up a terminal using PUTTY and the plan was to send a character from putty to the arduino , and then
have the arduino send it back . To prove to my doubting friend that it really made the trip my plan was to add one to its value in the arduino
so sending an A returns a B sending B returns a C etc

When it failed to give me the characters only ascii code numbers I altered the prog , including taking out the bit of code
that adds 1 to the value . What I didnt notice was that when I uploaded the code , although it appeared to succeed , at the bottom
of the page it said that AVR dude was out of sync , or something like that . so really the code never did update and the
ADD one to the received value remained in force .I know why this happened now . I had the outside comms port still connected
to arduino pins 0 and 1 while trying to upload a program, so it failed

So although I feel silly for making the mistake , I still learned about Casting and also to disconect things from pins 0 and one
before uploading , so it was really a good thing because I learned from it

Again , thanks for the help

Don