PySerial Communication Garbling Data

Hello! I am working with PySerial for the first time, and attempting to follow this tutorial\ to send and receive data from an ESP32 in Python. Here is the code from the tutorial that I am running:

Arduino

void setup() {
  Serial.begin(115200);
  pinMode(pin, OUTPUT);

}

void loop() {

  while(Serial.available()){
     Serial.write(Serial.read());
     x = Serial.read();
    
  }
  delay(10);
}

Python

import serial 
import time 

ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM4'
ser.open()

values = bytearray([4, 9, 62, 144, 56, 30, 147, 3, 210, 89, 111, 78, 184, 151, 17, 129])
ser.write(values)

total = 0

while total < len(values):
    print(ord(ser.read(1)))
    total=total+1
    
ser.close()

When I run this code, the data has the right length but is just a random series of numbers, like in this screenshot:

As far as I can tell I am ser.read correctly, so I'm not sure if this is a decoding error. I have tried just sending a single number, with the same result, the output is different than what I sent. I have also tried using a different ESP, a different USB port on my computer, and a different USB cable, the result is the same. What is going on here?

Thank you!

You are missing every 2nd character read, as you are doing 2 Serial.read() statements one after the other?

The example you are following is perhaps not the best, I'm not saying mine is the greatest but see if you get better results. What I have done here is create a loop in the Python that sends and receives data every 5 seconds, eventually any bad data in the tx/rx buffer will clear and you should begin to receive the correct values, this should happen fairly quickly. The Arduino is left basically unchanged just removed the delay and the line mentioned by red_car

ARDUINO

void setup() {
  Serial.begin(115200);
}
 
void loop() {
 
  while(Serial.available()){
    Serial.write(Serial.read());
  }
 
}

PYTHON

import serial
import time
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM4'
ser.open()

values = bytearray([4, 9, 62, 144, 56, 30, 147, 3, 210, 89, 111, 78, 184, 151, 17, 129])

while True:
	time.sleep(5)
	ser.write(values)
	try:
		print(f'Characters in ser buffer = {ser.in_waiting}')
		for i in range(ser.in_waiting):
			print (ord(ser.read()))
	except:
		print("pass")
 
print("done")
 
ser.close()

This fixed the issue, thank you so much for the code and explanation!