setup()
, function Serial.begin(9600)
sets up the baud rate for serial data communication. In this example, a baud rate of 9600 is used. Other values can be read here: Arduino Serial.begin() functionloop()
, the first message we would like to send is 'Hello World!'. This message is transmitted by using Serial.println('Hello World!')
as it will send this string to serial port in ASCII format. At the end of the message, there are Carriage Return (CR, r)
and Newline character (n)
. Also, a delay of 100 milliseconds is used each time program prints to serial port.readline()
function. A delay of 100 milliseconds is also used here as same as in Arduino sketch. Please notice that pyserial readline()
function requires a timeout when opening a serial port (pyserial documentation: PySerial ReadLine).