communicate betwheen Arduino & PC fails - making clear to use pyserail correct

Hi, there at all good day dear Arduino experts and friends,

I'm trying to create a communicate betwheen Arduino and my PC> The host-pc runs on windows. At the moment my code is just very simple: What is aimed: i just want to switch on a LED when i send data from the host to the Arduino: But this is actually not working. i struggle a bit with this piece of code.

By the way: i make use of pySerial.

Findings: when i try to send the data to from the host (in other words the pc )to my arduino the led TX blinks but the part of my code which is right behind the if ( serial.available()>0) doesn't run at all - i am pulling my hair.

Here is tbe full code:

Python code:

import serial

serie=serial.Serial()    
serie.baudrate=9600
serie.port= 'COM4'
serie.parity=serial.PARITY_ODD
serie.stopbits=serial.STOPBITS_ONE
serie.bytesize=serial.EIGHTBITS

if serie.isOpen():
    serie.close()
serie.open()
serie.isOpen()
serie.write("hello".encode())

and here we have the Arduino code:

int ledPin = 9;
byte incomingByte;

void setup() {
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop() {
   
    if (Serial.available() > 0) {
        incomingByte = Serial.read();
        digitalWrite(ledPin, HIGH);
    }

    delay(1000);
    digitalWrite(ledPin, LOW);}

well - i tried to look at a full python sample code that uses pyserial, i have the package and am wondering how to send the commands and read them back!

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyUSB1',
    baudrate=9600,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)

ser.isOpen()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
    # get keyboard input
    input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
    if input == 'exit':
        ser.close()
        exit()
    else:
        # send the character to the device
        # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
        ser.write(input + '\r\n')
        out = ''
        # let's wait one second before reading output (let's give device time to answer)
        time.sleep(1)
        while ser.inWaiting() > 0:
            out += ser.read(1)

        if out != '':
            print ">>" + out

again what is aimed>> Use the Arduino ide to turn the Arduino LED on and off.

Hi,

I've used this: Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

For sending between my VB program and the Uno.

Example 5 works very well for me as I'm sending three sets of numbers each time.

PC sends to Uno.
Uno breaks the text down to three variables
Uno recompiles a new text message and sends it to the PC
PC checks that what it gets back is the same as what it sent
PC send an 'OK' or an 'abort' code
Uno uses or scraps the numbers that it received.

Regards
Peter

Why do you use "PARITY_ODD? I think that Arduino does not handle PARITY.
Try with PARITY_NONE.

@OP
Most Arduinos reset when you open the serial port. That will give a delay of a few seconds during which time the Arduino can not receive data from your PC (and it will be lost).

@RudolfAtRTC
This is what the Arduino supports (from HardwareSerial.h). But OP's code does not use it :wink:

#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E

This Simple Python - Arduino demo may help.

...R