void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
char data = Serial.read();
Serial.println(data);
// Example: Perform some operation with the received data
// For simplicity, let's just send the data back
Serial.write(data);
}
}
import serial
import time
# Replace 'COMx' with the actual COM port where your Arduino is connected
ser = serial.Serial('COMx', 9600, timeout=1)
def send_data(data):
ser.write(data.encode())
time.sleep(1) # Allow time for the Arduino to process
def receive_data():
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting).decode('utf-8')
return data
return None
# Example usage
send_data('A') # Send data to Arduino
received_data = receive_data() # Receive data from Arduino
if received_data:
print(f"Received from Arduino: {received_data}")
ser.close()