Skip to main content
added 296 characters in body; edited title
Source Link
Leo Ervin
  • 127
  • 2
  • 3
  • 9

make Arduino tell when the Python program stops reading from the Serial connection is established and terminated(crash, etc)

So I have no idea how I need to word this but I've been told to ask a new question so here, editHere's the title accrdingly.Arduino code:

void setup() {
  Serial.begin(9600);
  
  pinMode(7, INPUT_PULLUP);
}

void loop() {
    int val = digitalRead(7);
    if (val == HIGH) {
        Serial.print("1");
    } else {
        Serial.print("0");    
    }
    Serial.println("");  // new line
  
}

Python code:

import time
import serial

# setup Arduino USB communication
try:
    arduinoSerialData = serial.Serial('com3', 9600)
    
except: # not connected/damaged
    pass
    
while True:
    if arduinoSerialData.inWaiting() > 0:
        datastr = arduinoSerialData.readline()
        print datastr
        time.sleep(1)

WHat I needwant to do digitalWrite(7, HIGH); in Arduino when thereadd is a Serial connection between Arduino and a, if Python program (PySerial) via USB. When the USB cable is disconnectedexits, or crashes, let the Arduino has to do do digitalWrite(7, LOW); How can I know when Serial connection is "established" and when it is "disconnected"somehow. More specifically turn on a LED in Arduino?pin 8.

Arduino tell when Serial connection is established and terminated

So I have no idea how I need to word this but I've been told to ask a new question so here, edit the title accrdingly.

I need to do digitalWrite(7, HIGH); in Arduino when there is a Serial connection between Arduino and a Python program (PySerial) via USB. When the USB cable is disconnected, Arduino has to do do digitalWrite(7, LOW); How can I know when Serial connection is "established" and when it is "disconnected" in Arduino?

make Arduino tell when the Python program stops reading from the Serial (crash, etc)

Here's the Arduino code:

void setup() {
  Serial.begin(9600);
  
  pinMode(7, INPUT_PULLUP);
}

void loop() {
    int val = digitalRead(7);
    if (val == HIGH) {
        Serial.print("1");
    } else {
        Serial.print("0");    
    }
    Serial.println("");  // new line
  
}

Python code:

import time
import serial

# setup Arduino USB communication
try:
    arduinoSerialData = serial.Serial('com3', 9600)
    
except: # not connected/damaged
    pass
    
while True:
    if arduinoSerialData.inWaiting() > 0:
        datastr = arduinoSerialData.readline()
        print datastr
        time.sleep(1)

WHat I want to add is, if Python program exits, or crashes, let the Arduino know somehow. More specifically turn on a LED in pin 8.

Source Link
Leo Ervin
  • 127
  • 2
  • 3
  • 9

Arduino tell when Serial connection is established and terminated

So I have no idea how I need to word this but I've been told to ask a new question so here, edit the title accrdingly.

I need to do digitalWrite(7, HIGH); in Arduino when there is a Serial connection between Arduino and a Python program (PySerial) via USB. When the USB cable is disconnected, Arduino has to do do digitalWrite(7, LOW); How can I know when Serial connection is "established" and when it is "disconnected" in Arduino?