PySerial doesn't work in script
Question: I have problem with this script:

    
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import serial
    import time
    
    ser = serial.Serial("COM3", 9600, timeout=1)
    ser.write("test")
    print "Test started\n"
    time.sleep(1)
    ans = ser.read(4)
    print ans
    

In Arduino is echo program (everything what has been sent to Arduino has been
sent back to computer). With serial monitor everything works fine.

When I'm running my python script it stops on ser.read() (it is waiting for
incoming data). But when commands from my script I've written directly to
python console everything works without problems.

Why my code started from file didn't work?

My OS: Windows 8.1 (64 bits) Python version: 2.7.9 (64 bits)


Answer: Try this way

    
    
    import serial
    import time
    
    ser = serial.Serial("COM3", 9600, timeout=1)
    ser.write("test")
    print "Test started - data sent \n"
    while True:
        ans = ser.read(4)
        if and:
            print and
        time.sleep(1)
    

