2
votes

I'm using python to send a short serial message to an Arduino UNO over USB. This is run from terminal on an OSX machine.

import serial

ser = serial.Serial('/dev/tty.usbmodem14121', 9600)
ser.write('H')
ser.close()

Previously the Arduino was running code to read this message and react to it, but this was causing the Arduino to crash and restart. Simplifying the code led to the exact same result.

int ledRed = 3;
int ledGreen = 5;
int ledBlue = 6;

void setup()
{
    pinMode(ledRed, OUTPUT);
    pinMode(ledGreen, OUTPUT);
    pinMode(ledBlue, OUTPUT);
}

void red()
{
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledGreen, LOW);
    digitalWrite(ledBlue, LOW);
}

void green()
{
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, HIGH);
    digitalWrite(ledBlue, LOW);
}

void loop() 
{
    green();
    delay(1000);
    red();
    delay(1000);
}

Can anyone shed a light on why sending serial to the board is causing it to crash and restart?

Thanks All

1
You should be setting the baud rate in setup(), maybe there's a mismatch? What happens if you add Serial.begin(9600) to setup()?Roger Rowland
Gave it a try, unfortunately the board still crashesXmasRights
Ok, I'm out of ideas then - but make sure you don't have the Arduino Serial Monitor window open at the same time, that's been reported as causing problems.Roger Rowland

1 Answers

1
votes

Late answer, but just had a similar problem for Java. The problem is that the Arduino (by design) resets when the computer opens the serial port. This is probably a good thing in most cases. When it is not desired, the board hardware can easily be modified to not reset. See the Arduino forum