Я пытался написать скрипт Python, который прослушивает монитор Arduino serie через COM3 для обнаружения строки.
После обнаружения строки сценарий Python выполняет следующий сценарий.
Код Arduino
Код: Выделить всё
String receivedString = ""; // Variable to store the received word
void setup() {
// Initialize the serial port at 9600 baud
Serial.begin(9600);
Serial.println("Enter a word in the serial monitor:");
}
void loop() {
// If there is data available on the serial port
if (Serial.available()) {
// Read the incoming characters
char inChar = (char)Serial.read();
// If the user presses 'Enter' (newline character)
if (inChar == '\n') {
// Print the received word
Serial.println(receivedString);
// Reset the string for a new input
receivedString = "";
// Prompt the user to enter a new word
Serial.println("");
}
// Otherwise, add the character to the received word
else {
receivedString += inChar;
}
}
}
Код: Выделить всё
import serial
import subprocess
ser = serial.Serial('COM3', 9600, timeout=1)
while True:
# read data send by the arduino
data = ser.readline().decode().strip()
if data == "camera": # if the command camera is written on the monitor serie
print("python script execution")
else:
print(f"Donnée reçue : {data}") # display data
Следующая ошибка:
"serial. Serialutil.SerialException: не удалось открыть порт «COM3»: PermissionError(13, «Отказ в доступе.», Нет, 5)»
Я не знаю, хорошее ли мое решение ... Я подумал, что будет проще выполнить код Python из небольшого события Arduino в качестве обнаружения датчика PIR.
Подробнее здесь: https://stackoverflow.com/questions/790 ... uino-event