Arduino UNO restarts when using serial port outside the IDE

3

My Arduino UNO is restarting when I use its serial port with a Python script. But when I open the serial terminal through the Arduino IDE and run the same Python script, it works normally. Why does this happen and how do I resolve it?

Arduino Code:

#define LED 10

char c;
int led = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  while(!Serial) {

  }
}

void loop() {
  if(Serial.available()) {
    c = (char)Serial.read();
    Serial.println(c);
    if(c == 'a') {
      led = HIGH;
    } else if(c == 'd') {
      led = LOW;
    }
    digitalWrite(LED, led);
  }
}

Python code:

#! /usr/bin/env python
import serial
import sys

if len(sys.argv) > 1:
    command = sys.argv[1]
    ser=serial.Serial('/dev/ttyACM0',9600)
    if not ser.isOpen():
        ser.open()
    print command
    ser.write(command)
    ser.close()
else:
    print 'usage: luz.py a|d'
    
asked by anonymous 22.06.2014 / 05:54

1 answer

3

I'm starting with Arduino so I can not speak properly. But I did some research on Google and found that Arduino does a Auto Reset On Serial Connection after establishing a serial connection.

There are a few ways to avoid this, the most common being Jumper , or doing board changes . What intrigues me is how IDE can turn off Auto Reset On Serial Connection without changing the physical part of the board.

Looking at some scripts in Python (I do not know how to program in Python), I realized that many use a loop after connecting to the card .

from time import sleep
import serial

ser = serial.Serial('/dev/tty.usbmodem1d11', 9600)
while True:
    ser.write(Dados) 
    print ser.readline() 
    sleep(.1)

This causes the data to be sent at all times, this way after the board restarts (if it restarts). Data will be available.

Another method I found to be valid is that you could do setup of Arduino write on the serial portal that he is ready . And wait for this message in the Python script. Only after receiving this message would you send the message to the card. According to other users this option works perfectly.

If you need more information about how to disable the hardware reset, you can visit this link .

    
22.06.2014 / 17:51