Serial comm with Arduino

Hi. I’m trying to establish communication between an Arduino and an RPi via GPIO serial, bidirectional.
The ‘read’ Rpi part works fine but the ‘write’ do not. Nothing is received from Arduino.
Here is the python code on RPi:

#!/usr/bin/env python3
import time, serial, subprocess, signal, sys
from picamera2 import Picamera2, Preview
from picamera2.controls import Controls

def sigint_handler(signal, frame):
    print('Killed')
    sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)

ser = serial.Serial("/dev/ttyS0",9600)
# ser.flush() # Get rid of garbage/incomplete data

picam2 = Picamera2()
config = picam2.create_preview_configuration()
picam2.configure(config)
picam2.start()

time.sleep(1)
picam2.stop_preview()

print("\nWaiting for Arduino commands")
msg = "RPi started"
ser.write(msg.encode('utf-8'))
time.sleep(3)

while True:
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()
        linelist = line.split(",")
        item0 = int(linelist[0])
        print(linelist)
        if (item0 == 1):
            # picam2.start_preview(Preview.QTGL, x=10, y=200, width=800, height=600)
            test_string = "item 1 received"
            # strBytes = bytes(test_string, 'utf-8')
            strBytes = test_string.encode('utf-8')
            ser.write(strBytes)
        elif (item0 == 2):
            picam2.stop_preview()

and here is the Arduino code (part of):

// SoftwareSerial mySerial(RX, TX)
// Bluetooth module's TXD pin to Arduino 8, RXD pin to Arduino 9
SoftwareSerial BT(8, 9);
// RPi's (UART0) TXD GPIO14 to Arduino 10, RXD GPIO15 to Arduino 11
// RPi's (UART5) TXD GPIO12 to Arduino 10, RXD GPIO13 to Arduino 11
SoftwareSerial RPi(10, 11);

void setup() {
    Serial.begin(9600);
	RPi.begin(9600);
	BT.begin(9600);

	Serial.println("Hello from PTS's USB hardware serial");
	// BT.print(F("^PTS started on Arduino")); // DO NOT CHANGE the string 
	// RPi.println("Hello from Arduino"); // per ora posso spedire solo int
}

void loop() {
	// By default, the last initialized port is listening.
	// when you want to listen on a port, explicitly select it:
	// BT.listen();
	if (BT.available() >0) DataFromBT(); // data from BT

	// RPi.listen();
	if (RPi.available() >0) DataFromRPi(); // data from RPi
}
void DataFromRPi() {
Serial.println("Data from RPi!");
}

To clarify, which direction is working and which isn’t, A or B?

       data
A: RPI ---> Arduino
B: RPi <--- Arduino

Are you connecting two of the RPi’s UARTs to the same port on the Arduino? Why? UART is typically a point-to-point connection; connecting more than two ports to the same UART bus can easily cause collisions or other problems.

Arduino to RPi (B) is working while B doesn’t work
Sorry, do not consider the two commented lines in Arduino code. I mean, only one UART is actuallu connected. Also, for clarification, the Bluetooth is another port I use to communicate between Arduino and my smartphone (AppInventor android app) …
Where I’m completely lost is RPi to Arduino serial data transfer.

Does the problem still occur with this minimal example code?

import serial

arduino = serial.Serial("/dev/ttyS0", 9600)
print(arduino.readline())
arduino.write("Hello from RPi\n".encode("utf-8"))
print(arduino.readline())
#include SoftwareSerial.h

SoftwareSerial RPi(10, 11);

void setup(void) {
    RPi.begin(9600);
    RPi.println("Hello from arduino");
}

void loop(void) {
    if (RPi.available()) {
        while (RPi.available()) {
            RPi.read(); // Consume data.
        }
        RPi.println("Received message from RPi");
    }
}

What output do you get when running the Python script?

b'\x00Hello from arduino\r\n'
b'Received message from RPi\r\n'

This is the output of the RPi shell, immediately after upload the Arduino code to the Arduino board.
Then the RPi shell close

Then it looks like the arduino successfully received the data sent from the RPi. Something in your original code must be causing the issue, on either the arduino or RPi side.

Try to eliminate pieces of your code until the problem dissappears. Then you will have identified the source of the problem.