Technical question about the auto-reset feature

I use an Arduino Uno Rev 2. What I was using it was for making a gmail notifier which used a python script on my computer to send serial data to the arduino using the pySerial library. Now, I've read around that when a serial data is sent to the arduino it resets automatically thus erasing any data it may have received. However just to test the hardware section with an LED connected to pin 12 I used the HyperTerminal program. My arduino code is as follows:

int outPin = 12; // Output connected to digital pin 12
int mail = LOW; // Is there new mail?
int val; // Value read from the serial port

void setup()
{
    pinMode(outPin, OUTPUT); // sets the digital pin as output
    Serial.begin(9600);
    Serial.flush();
}

void loop()
{
    // Read from serial port
    if (Serial.available())
    {
        val = Serial.read();
        if (val == 'M') mail = HIGH;
        else if (val == 'N') mail = LOW;
    }

    // Set the status of the output pin
    digitalWrite(outPin, mail);
}

Now when I tested it using the HyperTerminal (without disabling the auto-reset feature) and it works fine. However when I send data using the python script, it doesn't work.

Question is: why does it work in HyperTerminal without disabling auto-reset whereas when I use the python script it doesnt? Do I HAVE TO disable the feature for the script to work?

This is the python script I've used to capture my gmail feeds (for reference)

import serial, urllib2, sys, feedparser

def main():

    try:
     # the best way to find this out is to launch the Arduino environment
     # and see what it says under Tools -> Serial Port
        ser = serial.Serial(port='COM17', baudrate=9600)
    except serial.serialutil.SerialException:
        sys.exit()
    pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    pwdmgr.add_password("New mail feed", 'http://mail.google.com/', 'my-username', 'my-password')
    auth = urllib2.HTTPBasicAuthHandler(pwdmgr)
    opener = urllib2.build_opener(auth)
    data = opener.open('http://mail.google.com/mail/feed/atom')
    d = feedparser.parse(data)
    newmails = d.feed.fullcount
    print newmails
    if newmails > 0: ser.write('M')
    else: ser.write('N')
    ser.close()
    
if __name__=="__main__":
    main()

I don't see how auto reset is an issue here. By the time you send the serial character, the Arduino should be ready to receive. You could try putting a delay in python code to wait 2 or 3 seconds before sending the character, to make sure the Arduino is ready.

Drop your use of Serial.flush() it isn't doing what you think.

A 120 Ohm resistor from +5 to Reset will disable reset.

The 2 methods use different ways of attaching to the serial port. One will cycle RTS and that causes the reset. Try No Hardware Handshaking.

This piece of info was crucial. I added a one second delay and the code worked just fine.

Also I did not remove Serial.flush() from the code but I'd like to know what you meant when you said that it wasn't doing what I thought it was doing?

What do you think it does?

Which version of the IDE are you using?

karanjthakkar:
Also I did not remove Serial.flush() from the code but I'd like to know what you meant when you said that it wasn't doing what I thought it was doing?

Read the reference page for it.

Hopefully I'm not too late.

You can disable the auto-reset feature using PySerial by instantiating the object with the "dsrdtr=True" argument.

i.e.

arduino = serial.Serial("COM1", 57600, timeout=1, dsrdtr=True)