explaining serial.begin(9600)?

I have an IR remote and the IR receiver. I have managed to get it working without any problems on my Arduino Mega.

I have programmed it's speed to:

serial.begin(9600)

I have figured out that this is the communication speed the Arduino and the device in bits.

The question is: What decides the transfer speed? If I choose to set it at 57600 instead, how can I make sure the devices can keep up? Is this a trial and error thing?

If I set the speed "too low" do I make a safer connection, as in fewer dropouts?

If the speed is too high, what happens? Do I lose all communications or do I get a hacked up message, as the sender can't send fast enough?

The sender and receiver have to use the same baud rate or the receiver will get garbage.

Serial communication is asynchronous, meaning that you need to set the speed the same on both ends to have good communication of a single byte. The hardware serial can receive one byte without problems. The Serial Class adds an additional buffer so it will under “normal” circumstances receives any stream of bytes without problem.

IN practice you can have buffer overflow if the Serial input buffer is not processed in time. Furthermore often you want to a multiple of bytes that form a logical unit e.g. a four digit number, a command etc.

Then you need to find the start and the end of that logical unit (aka packet). The way is to add a begin and end marker to the stream. Like a CR LF at the end of a text line. Often used are < and > like in XML .

There is a lot to learn to get Serial communication really robust. This is a nice intro

I have an IR remote and the IR receiver. I have managed to get it working without any problems on my Arduino Mega.

I have programmed it's speed to:

serial.begin(9600)

Is the IR receiver really connected to the Arduino by a serial link ? Can you provide a link to the device that you are using ?

Thank you all for the answers.

UKHeliBob: I found a datasheet on the IR: https://arduino-info.wikispaces.com/file/view/IR-Receiver-AX-1838HS.pdf (there's a picture on the top of page 2)

It was the standard IR receiver, I got, when I bought the package (Mega + stuff) a couple of years back. I am not sure, I completely understand serial communcations yet, but it works with the code... I will be doing some readups on serial comms (and eventually I2C communication too!)

Much as I suspected that is not a serial device.

It is more than likely that what you are doing is to use the Serial monitor of the Arduino IDE to see the output of the program. The speed (baud rate) of the sending and receiving serial devices have to match in order for communication between them to work, as do other parameters. Within reason the speed of the serial interface does not matter as long as the receiving end is set to the same baud rate as the transmitting end.

The default speed of the IDE serial monitor is 9600 baud but you can change it at the bottom/right of the monitor screen. If you do that then you need to make a corresponding change to Serial.begin() in the program.

Please post the program that you are using so that we can confirm what I am suggesting.