Trigger brick program from Python

HomeForumsMonoBrick Communication LibraryTrigger brick program from Python


Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #3777

    Happy New Year! And congratulations on the great website. I have a (hopefully) simple question. I have an EV3 set and I want to trigger a normal Lego Mindstorms program to run on the EV3 brick, from a python script on a PC. Basically this python script runs unattended, and I want it to be able to trigger the EV3 brick to press a physical button.

    Communications via either bluetooth or USB would be fine. The question is: do I need a whole SW stack on the PC side to do the communication, or is there perhaps some shortcut I could use directly with the python pybluez module? I can already discover the EV3 using pybluez. But how could I trigger a program to run?

    Many thanks,

    Author Image
    Anders Søborg

    Although this forum is related to MonoBrick communication here are some Phyton code to send a message to the EV3 from a PC over Bluetooth

    import struct
    import ctypes
    import binascii
    import socket
    import time
    import StringIO
    import serial
    import struct
    from array import array
    from serial import * 
    from threading import Thread 
    class MailboxMessage(): 
        def __init__(self, mailboxName, msg): 
            self.MailboxName = mailboxName
            self.msg = msg
            mailboxNameLength = len(mailboxName)+1
            msgLength = len(msg) +1
            totalLength = mailboxNameLength + msgLength + 7
   = bytearray(b"")
   type - system command without reply 0x81
   command type - WriteMailbox 0x9e
   = + bytearray(mailboxName)
   = bytearray(msg)
        def Mailbox(self):
            return self.MailboxName
        def Data(self):
        def Message(self):
            return self.msg
    class Receiver(Thread): 
        def __init__(self, serialPort): 
            self.serialPort = serialPort 
        def run(self): 
            message = []
            while True:
                for c in
                if len(message) == 2:
                    bytes = array('B', message)
                    messageSize = struct.unpack('<H',  bytes )[0]
                    #print messageSize
                    for c in
                    bytes = array('B', message[6:7])
                    mailboxNameLength = struct.unpack('<B',  bytes )[0]
                    #bytes = array('B', message[7+mailboxNameLength:7+mailboxNameLength+2])
                    #mailboxTextLength = struct.unpack('<H',  bytes )[0]
                    mailboxName = array('B', message[7:7+mailboxNameLength-1]).tostring()
                    mailboxText = array('B', message[7+mailboxNameLength+2 :messageSize+1]).tostring()
                    mailboxMessage = MailboxMessage(mailboxName, mailboxText)
                    print mailboxMessage.Mailbox()
                    print mailboxMessage.Message()
                    message[:] = []
    class Sender(Thread): 
        def __init__(self, serialPort): 
            self.serialPort = serialPort 
        def run(self): 
            text = ""
            print "Enter text to send"
            while(text != "quit\n"): 
                msg = raw_input("")
                mailboxName = "mailbox1"
                self.sendMessage(mailboxName, msg)
        def sendMessage(self, mailboxName,msg):
            mailboxMessage = MailboxMessage(mailboxName,msg)
    serialPort = Serial(port='/dev/tty.EV3-SerialPort', baudrate=9600)
    send = Sender(serialPort) 
    receive = Receiver(serialPort) 

    To start a program you need to change the command to be a start program command. You can find more info on this and other direct commands here.



    Yes I couldn’t really find an appropriate location for this question. Thanks anyway for the answer, I’ll try it out as soon as I can.


    Author Image
    Anders Søborg

    No problem


    Author Image
    Pascal SALLIOT

    I’m trying to exchange mailbox’s messages between python 2.7 and the EV3’s software “LabVIEW” via a Bluetooth communication.
    After several month searching, you’re the only one who was able to do this ; thank you for this work.

    When I run the program above, I’ve got this message :

    “SerialException(“could not open port %r: %r” % (self.portstr, ctypes.WinError()))
    SerialException: could not open port ‘/dev/tty.EV3-SerialPort’: WindowsError(3, ‘Le chemin d\x92acc\xe8s sp\xe9cifi\xe9 est introuvable.’)”
    It means that Windows is unable to find the path /dev/tty.EV3-SerialPort even if the brick is connected with the USB wire…

    Any suggestions ?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Posted in

Make a donation