Serial communication from python to arduino not working

Hi there.
This is my first post here, so I might make some errors in writing my question.

I'm trying to communicate data ( a simple int) from a python script I wrote to my Arduino Nano.

Right now I'm only working on the sending and receiving part. I pull the data from a web-API which is irrelevant to my issue.

My Arduino code is supposed to read a byte from the serial com and display it on a hex-display. The display itself works, and the code for it does as well.

This is my Arduino Code:

#include <Arduino.h>
#include <TM1637Display.h>

// Display connection pins (Digital Pins)
#define CLK 3
#define DIO 2

String inString = "";

TM1637Display display(CLK, DIO);

void setup()
{
  display.setBrightness(0x09);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      inString += (char)inChar;
    }

    if (inChar == '\n') {
      display.showNumberDec(inString.toInt());
      inString = "";
    }
  }
}

That code "works". I tried sending it numbers with the Arduino IDEs Serial Monitor and it displays those as expected.

My python code is, for now, only supposed to send a number to the Arduino.
I'm using the pySerial library and followed a tutorial explicitly for Arduino.

import serial, time

ser = serial.Serial('COM5', baudrate=9600, timeout=1)
print('start')
for i in range(5):
    ser.write(b"5")
    time.sleep(1)

print('stop')

I'm not getting any error messages, and im a bit confused why this won't work.
Any help is appreciated and big thanks in advance.

When I did serial communications from RPi->Arduino->RPi I remember my RPi code being a bit more complicated to send/receive. I do remember that using some pins from RPi->Arduino->RPi as signals really helped with getting the whole thing to work well.

This Simple Python - Arduino demo may help.

Your PC program should open the serial port, allow time for the Arduino to reset before trying to send data and should then keep the serial port open until it is completely finished with the Arduino.

...R

Robin2:
This Simple Python - Arduino demo may help.

Your PC program should open the serial port, allow time for the Arduino to reset before trying to send data and should then keep the serial port open until it is completely finished with the Arduino.

...R

I copy-pasted those codes and only changed the port my Arduino is on.
But the python programm stops at :

C:\Users\me\Desktop>python serial-writer.py
Serial port COM5 opened Baudrate 115200
Waiting for Arduino to reset
Arduino is ready

I tried hitting keys or something, but nothing happens. Is this supposed to do something or is it like a framework to work with?

jakobsachs:
I copy-pasted those codes and only changed the port my Arduino is on.
But the python programm stops at :

Please post your version of the PC program and the Arduino program as uploaded to YOUR Arduino.

Have you tried pressing the reset button on the Arduino? If that helps it may be that your Python program is not causing the Arduino to reset and you may need to add Python code to set DSR and RTS as part of Python serial setup

...R

Here is my Python code I used to communicate with a UNO, Mega, and Due

LidarPythonCode.txt (49.6 KB)