Not getting data from python

Thread Starter

eonshopact

Joined Sep 2, 2023
19
I am trying to get data from Python to Arduino. In the serial monitor(Arduino Tools->serial monitor), i am receiving the data. When the data is sent from python, i am getting the message, "Error in getting the data"

I am using Arduino Mega.

Code:
void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}

void loop() {
  if (Serial.available()) {
    String message = Serial.readStringUntil('\n'); // Read message until newline
    Serial.println("Received message: " + message); // Send a response back to Python
  } else {
    Serial.println("Error in getting the data");
   }
}
Code:
import serial

SERIAL_PORT = "COM20"  # Replace with your Arduino's serial port
BAUD_RATE = 9600

with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
  message = "Hello from Python!"
  ser.write(message.encode() + b'\n')  # Send message with newline

  while True:
    response = ser.readline().decode().strip()
    if response:
      print("Received from Arduino:", response)
      break  # Exit loop after receiving response

  print("Message sent and received successfully!")
 

djsfantasi

Joined Apr 11, 2010
9,163
You are extremely optimistic! You are expecting data to be available in line 6 immediately and if the data isn’t ready, you print “Error in getting the data”.

Realistically, if Seral.available() is false, you would just go back and see if it’s available later. You wouldn’t assume there’s an error.
 

WBahn

Joined Mar 31, 2012
30,076
Consider how long it takes to get a single 8-bit value across a 9600 baud serial link. That's ten bits (including start and stop and assuming you have no parity). That's about 1 ms for EACH character.

How many clock cycles is that?
 

k1ng 1337

Joined Sep 11, 2020
960
You are trying to send serial data from Python while the Arduino port is already in use via the serial monitor in Arduino IDE. Close Arduino IDE serial monitor and run the Python code with Thonny. I edited your code a bit and it works for me.

C-like:
void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}

void loop() {
  delay(1000);
  if (Serial.available()) {
    String message = Serial.readStringUntil('\n'); // Read message until newline
    Serial.println("Received message: " + message); // Send a response back to Python
  } else {
   }
}
Python:
import serial

SERIAL_PORT = "COM3"  # Replace with your Arduino's serial port
BAUD_RATE = 9600

with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
  message = "Hello from Python!"
  #ser.write(message.encode() + b'\n')  # Send message with newline

  while True:
    ser.write(message.encode() + b'\n')  # Send message with newline
    response = ser.readline().decode().strip()
    if response:
      print("Received from Arduino:", response)
      break  # Exit loop after receiving response
Untitled.png
 

Thread Starter

eonshopact

Joined Sep 2, 2023
19
You are trying to send serial data from Python while the Arduino port is already in use via the serial monitor in Arduino IDE. Close Arduino IDE serial monitor and run the Python code with Thonny. I edited your code a bit and it works for me.

C-like:
void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}

void loop() {
  delay(1000);
  if (Serial.available()) {
    String message = Serial.readStringUntil('\n'); // Read message until newline
    Serial.println("Received message: " + message); // Send a response back to Python
  } else {
   }
}
Python:
import serial

SERIAL_PORT = "COM3"  # Replace with your Arduino's serial port
BAUD_RATE = 9600

with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
  message = "Hello from Python!"
  #ser.write(message.encode() + b'\n')  # Send message with newline

  while True:
    ser.write(message.encode() + b'\n')  # Send message with newline
    response = ser.readline().decode().strip()
    if response:
      print("Received from Arduino:", response)
      break  # Exit loop after receiving response
View attachment 316651
Thank you. It is working.
 
Top