Arduino Uno - pyserial communication

Hey, i want to read the ArduinoSerial output with pyserial, so i used:

arduino = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=.1)
while True:
data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
if data:
print(data)

it works fine, but i want to write after reading serial to arduino with:

arduino = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=.1)
def write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
return data
while True:
numx = input("Enter a number: ")
valuex = write_read(numx)
print(valuex)

but why ever it dosent work...
is there a simple solution?

my Arduino code is

// Author: BlackLeakz
// Date: 18.05.2022
// Time: 0014
// Website: https://blackzspace.org
// Version: 1
// 3x 16x2 LCD , 1xExternal LED, //-+ potentiometer

// Libarys
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <TimerOne.h>

// LCD DISPLAYS
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
LiquidCrystal_I2C lcd2 = LiquidCrystal_I2C(0x26, 16, 2);
LiquidCrystal_I2C lcd3 = LiquidCrystal_I2C(0x25, 16, 2);



// SETUP
void setup() {
  //LED + Serialmenu
 pinMode(LED_BUILTIN, OUTPUT);


 Serial.begin(115200);

 while (!Serial) {
     ; // wait for serial port to connect. Needed for native USB
   }

 delay(300);
 Serial.println("___________________________________________________________________________________________________");
 Serial.println("== nemesis_montor | full_control - v0.3 || (c)by BlackLeakz                                      ==");
 Serial.println("---------------------------------------------------------------------------------------------------");
 Serial.println("== !: Write to display  || v: version                                                            ==");
 Serial.println("== (1): Time ||  (2): ping  || (3): tracert  || (4): PIPE || (pot): Potentiometer1               ==");
 Serial.println("===================================================================================================");

 delay(1000);
//init display1
 lcd.init();
 lcd.backlight();
 lcd.setCursor(0,0);
 lcd.blink();
 lcd.print("Version: v.0.1");


// init display2
 lcd2.init();
 lcd2.backlight();
 lcd2.setCursor(0,0);
 lcd2.blink();
 lcd2.print("Lcd2 ready...");


//init display3
 lcd3.init();
 lcd3.backlight();
 lcd.setCursor(0,0);
 lcd3.blink();lcd3.blink();
 lcd3.print("Welcome...");


 delay(4000);

 lcd.clear();
 lcd.home();
 lcd.blink();
 lcd2.clear();
 lcd2.home();
 lcd2.blink();
 lcd3.clear();
 lcd3.home();
 lcd3.blink();


}


// LOOP

void loop() {
    //LED BLINK
 digitalWrite(LED_BUILTIN, HIGH);
 delay(30);
 digitalWrite(LED_BUILTIN, LOW);
 delay(30);
 digitalWrite(LED_BUILTIN, HIGH);
 delay(30);
 digitalWrite(LED_BUILTIN, LOW);
 delay(300);
 digitalWrite(LED_BUILTIN, HIGH);





 if(Serial.available() > 0)  {
 int incomingData= Serial.read(); // can be -1 if read error
    switch(incomingData) {
        case '-1':
            Serial.println("Error");
            break;

        case '!':
            Serial.println("!-: Write to LCD");
            if (Serial.available()) {
            delay(200);
            lcd.clear();
            while (Serial.available() > 0) {
            lcd.write(Serial.read());

           }
             }

        break;

       case 'v':
           Serial.println("Version: v0.1");
           lcd.clear();
           lcd.home();
           lcd.print("Version: v0.1");
           delay(1000);
           break;

       case '1':
          Serial.print("Get current time");
          lcd.clear();
          lcd.setCursor(0, 0);


       default:
          // handle unwanted input here
          break;
  }

}
}

Please post your python code using code tags. I'm quite sure that you're aware that indentations have a meaning in Python and they are now gone.

"but why ever it dosent work..." is a very vague description; what do you expect it to do and how does it differ from what is hapening.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.