<p>Works for me if you set buffering to 0 and get rid of the newlines.</p>

<pre><code>import time

# command to start motor on port A at speed 20
start_motor = '\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01'

# command to stop motor on port A
stop_motor = '\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00'

# send commands to EV3 via bluetooth
with open('/dev/tty.EV3-SerialPort', 'w', 0) as bt:
    bt.write(start_motor)
    time.sleep(5)
    bt.write(stop_motor)
</code></pre>

CC BY-SA 3.0
2014-06-21T21:20:47.607Z
It worked! I could swear I had tried buffering=0, but clearly I was wrong.
Parzival
CC BY-SA 3.0
2014-06-21T19:18:04.180Z
2014-06-21T19:18:04.180Z
David Lechner
<p>I want to use Python to send commands to an EV3 brick. In C++ things work as expected - for instance, this starts the motor on port A and then stops it after a while:</p>

<pre><code>#include &lt;unistd.h&gt;
#include &lt;fcntl.h&gt;

int main()
{

    // command to start motor on port A at speed 20
    char start_motor[] = "\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01";

    // command to stop motor on port A
    char stop_motor[] = "\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00";

    // send commands to EV3 via bluetooth
    int bt = open("/dev/tty.EV3-SerialPort", O_RDWR);
    write(bt, start_motor, 14);
    usleep(500000);
    write(bt, stop_motor, 11);
    close(bt);
}
</code></pre>

<p>When I try the same thing in Python, however, only the first command is executed (the motor starts but it doesn't stop):</p>

<pre><code>import time

# command to start motor on port A at speed 20
start_motor = '\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01' + '\n'

# command to stop motor on port A
stop_motor = '\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00' + '\n'

# send commands to EV3 via bluetooth
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(start_motor)
    time.sleep(5)
    bt.write(stop_motor)
</code></pre>

<p>I have also tried: suppressing the <code>\n</code>; using <code>os.open('dev/tty.EV3-SerialPort', os.O_RDWR)</code>; using <code>bt.flush()</code>; <code>using os.fsync(bt)</code>; using <code>bt = open(... (instead of with)</code>; and changing the mode and buffering arguments of the open method. Nothing worked.</p>

<p>Well, it does work if I close the tty and open it again, but this sounds inefficient:</p>

<pre><code># ...
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(start_motor)
time.sleep(5)
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(stop_motor)
</code></pre>

<p>So, what am I missing here?</p>

<p>(OS X 10.9.3, Python 2.7.6, EV3 [31313])</p>

<ev3><programming><python>
Can't send multiple commands to EV3 using Python
Parzival
# Can't send multiple commands to EV3 using Python

(Asked by: username_0 on 2014-06-21)

I want to use Python to send commands to an EV3 brick. In C++ things work as expected - for instance, this starts the motor on port A and then stops it after a while:
#include 
#include 

int main()
{

    // command to start motor on port A at speed 20
    char start_motor[] = "\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01";

    // command to stop motor on port A
    char stop_motor[] = "\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00";

    // send commands to EV3 via bluetooth
    int bt = open("/dev/tty.EV3-SerialPort", O_RDWR);
    write(bt, start_motor, 14);
    usleep(500000);
    write(bt, stop_motor, 11);
    close(bt);
}

When I try the same thing in Python, however, only the first command is executed (the motor starts but it doesn't stop):
import time

# command to start motor on port A at speed 20
start_motor = '\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01' + '\n'

# command to stop motor on port A
stop_motor = '\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00' + '\n'

# send commands to EV3 via bluetooth
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(start_motor)
    time.sleep(5)
    bt.write(stop_motor)

I have also tried: suppressing the \n; using os.open('dev/tty.EV3-SerialPort', os.O_RDWR); using bt.flush(); using os.fsync(bt); using bt = open(... (instead of with); and changing the mode and buffering arguments of the open method. Nothing worked.
Well, it does work if I close the tty and open it again, but this sounds inefficient:
# ...
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(start_motor)
time.sleep(5)
with open('dev/tty.EV3-SerialPort', mode = 'w') as bt:
    bt.write(stop_motor)

So, what am I missing here?
(OS X 10.9.3, Python 2.7.6, EV3 [31313])

**Tags:** ev3, programming, python


---

## 1 answers

### Answer by username_1 on 2014-06-21. Score: 4 | Accepted answer

Works for me if you set buffering to 0 and get rid of the newlines.
import time

# command to start motor on port A at speed 20
start_motor = '\x0C\x00\x00\x00\x80\x00\x00\xA4\x00\x01\x14\xA6\x00\x01'

# command to stop motor on port A
stop_motor = '\x09\x00\x01\x00\x80\x00\x00\xA3\x00\x01\x00'

# send commands to EV3 via bluetooth
with open('/dev/tty.EV3-SerialPort', 'w', 0) as bt:
    bt.write(start_motor)
    time.sleep(5)
    bt.write(stop_motor)

> **Comments:**
> **username_0**: It worked! I could swear I had tried buffering=0, but clearly I was wrong.  

---

