Gibberish test when my python script prints Arduino Serial output

Hi,

I need to read some values from Arduino and use them in Python. But, when I read string from Arduino and print it in Python, python prints gibberish text. Can you help identify what the problem and how to solve it? I tried some .decode("UTF-8") in python but also did not help.

Here is Arduin code:

void setup(){
// Open serial connection.
Serial.begin(9600);
 
}
 
void loop(){
Serial.print("Hello world");
delay(10); // ms
 
}

Here is Python code:

import serial
import time
ser = serial.Serial('COM6', 9600, timeout=0) #in Windows port no. starts from 0. So COM6 is COM7 in Arduino.
 
while 1:
 try:
  print ser.readline()
  time.sleep(1)
 except ser.SerialTimeoutException:
  print('Data could not be read')
  time.sleep(1)

Here is what I get (both Serial Monitor and the Python output are shown):

The second picture (Python output) looks like NMEA data. Like from a GPS. Python is not connected to the right port. You probably can't have Python and serial monitor connected to the same port at the same time.

Try COM7. Numbering might start from 0, but strings like COMx might start with X from 1. Not a python programmer but it would make sense to me.

You definitely shouldn't have two programs talking the same serial device at the same time!

You do not check if there is data available and still you read from the serial.

seems a good cause for Gibberish()

#in Windows port no. starts from 0. So COM6 is COM7 in Arduino.

Nonsense. COM6 on your PC is connected to something giving NMEA sentences from a GPS. COM7 is the Arduino.
But you can't read COM7 with Python when you've already got the Arduino Serial Monitor reading COM7. If Arduino is on COM7, change the Python code to use COM7. Then run the Arduino code but do not start the Serial Monitor. Now start the python code on COM7.

Pete