Reputation: 104
I want to send values from a Python program on a Raspberry Pi to an Arduino. My RPi program looks like that:
import time, serial
time.sleep(3)
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write(b'5')
My Arduino program:
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // initial "ready" signal
}
void loop() {
char inByte = ' ';
if(Serial.available()) {
inByte = Serial.read();
Serial.println(inByte);
}
delay(100)
}
My problem is that when I run the program in Python and afterwards open the serial monitor in the Arduino IDE, it only shows "ready", but not the sent value. If I want to have the monitor open at the same time as the program, there is the error:
Device or resource busy: '/dev/ttyACM0'
I am using Python 3.6 by the way.
I am sorry if this is an easy error, but I am very new to serial and Arduino.
Upvotes: 0
Views: 2023
Reputation: 2072
You can connect to your Arduino serial port from only one application at a time. When you are connected to it via Serial port Monitor, Python couldn't connect to it.
You have two variants of solution:
Use serial sniffer instead of Arduino IDE's Serial Monitor. There are another answered question on this topic: https://unix.stackexchange.com/questions/12359/how-can-i-monitor-serial-port-traffic
Don't use any serial monitor, use Python! You can just continiualy read from serial, and print what have been received, something like this:
import time, serial
time.sleep(3)
ser = serial.Serial('/dev/ttyACM0', 9600)
# Write your data:
ser.write(b'5')
# Infinite loop to read data back:
while True:
try:
# get the amount of bytes available at the input queue
bytesToRead = ser.inWaiting()
if bytesToRead:
# read the bytes and print it out:
line = ser.read(bytesToRead)
print("Output: " + line.strip())
except IOError:
raise IOError()
Upvotes: 0