Urgent !!: Beginner help needed with serial communication..

Hi all,

I am new to arduino & programming. I want to send Hex data thru TX pin and read it at RX pin. TX and RX are connected externally by wire. Below is my code:

void setup() {
Serial.begin(9600);
}

void loop() {
int note = 0x2CA1;
int incoming = 0x00;
delay(100);
Serial.write(note);
Serial.print("I sent: ");
Serial.println(note, HEX);
if (Serial.available() > 0) {
delay(50);
// read the incoming byte from TX at RX:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incoming, HEX);
}
}

Here is the output:

I sent: 2CA1
I received: 49

No matter what hex data I transmit, the RX pin always reads 49. I am confused as to why is this happening. I want the RX pin to read the same data that is being transmitted via TX. Please HELP !!! :~ :~ :~

Thanks

Infrax

Are you catching the final 1 of "0x2CA1"? (which you don't seem to be sending as hex). 1 is char 49 in ASCII?

I tried sending other hex data like 0x1E or 0x2F but I still get 49.

It doesn't matter what hex data I send, it always shows 49 at RX !!

I ran your code (I had to change incomingByte to just incoming as I think that's what you meant) also - all of your variables are integers. I have no idea what you are trying to achieve but I got a lot of different values coming out once I'd made the required changes. I'm not entirely sure what to expect as you seem to want to use the same serial lines for two things at once.

Yes you are correct. The "incomingByte" had to be changed to "incoming". But when you run the code initially, you will get lots of different values for sometime (due to noise), but the result finally stabilizes to 49. It takes about 1 min.

I tried initializing variables as byte but I get the same result.

My final goal receive serial data in hex from circuit "A", do some stuff with it and then transmit the result out to circuit "B". I don't have circuits A&B with me currently. So to test if I can transmit and receive data correctly I wrote the code below.

Pardon me if I am not using correct electronics lingo, I am new to this field.

I tried sending other hex data like 0x1E or 0x2F but I still get 49.

First question is how are you sending the bytes? Are you sure you are sending only a single byte or are you sending four bytes thinking it is just one?

I am trying to send as 1 single byte.

I am trying to send as 1 single byte.

Bzzzzt! I did not ask what you are trying to do, I asked how are you attempting to send your byte to to the arduino.

zoomkat:

I am trying to send as 1 single byte.

Bzzzzt! I did not ask what you are trying to do, I asked how are you attempting to send your byte to to the arduino.

In his original posting:

TX and RX are connected externally by wire.

In his original posting:
Quote
TX and RX are connected externally by wire.

Sounds like what would normally a loop in the arduino. So how is he aware of the existance of the "49"?

Here is the snap of Serial Monitor...

I'm not sure this experiment is doing you much good. Why not practice sending and receiving data to and from the serial monitor instead of connecting Rx and Tx?

I think using a serial monitor is a good idea. Check you can send characters to it and receive characters from it before trying to send and receive at once on the Arduino. If you don't have a serial terminal/monitor then put the baud rate down low , 300 baud, and put an LED on the output pin (or use a serial breakout box). If you are managing to output bits you will see the LED flash and you may even be able to tell characters apart.

What does Serial.write actually do? Is it using a hardware serial UART ? If it is you will be able to send and receive because the UART will have buffers. If the Arduino processor is doing the reading and writing then you probably will not.

Take a look at this;