Trigger brick program from Python

HomeForumsMonoBrick Communication LibraryTrigger brick program from Python

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by Author Image Pascal SALLIOT 3 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #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,
    Fergus

    #3781
    Author Image
    Anders Søborg
    Key Master

    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
            self.data = bytearray(b"")
            self.data.append(totalLength)#length
            self.data.append(0)
            self.data.append(100)#sequence number
            self.data.append(0)
            self.data.append(129)#Command type - system command without reply 0x81
            self.data.append(158)#system command type - WriteMailbox 0x9e
            self.data.append(mailboxNameLength)
            self.data = self.data + bytearray(mailboxName)
            self.data.append(0)
            self.data.append(msgLength)
            self.data.append(0)
            self.data = self.data+ bytearray(msg)
            self.data.append(0)
        def Mailbox(self):
            return self.MailboxName
        def Data(self):
            return self.data        
        def Message(self):
            return self.msg
    
    class Receiver(Thread): 
        def __init__(self, serialPort): 
            Thread.__init__(self) 
            self.serialPort = serialPort 
        def run(self): 
            message = []
            messageSize=0
            while True:
                for c in self.serialPort.read(size=1):
                    message.append(ord(c))
                if len(message) == 2:
                    bytes = array('B', message)
                    messageSize = struct.unpack('<H',  bytes )[0]
                    #print messageSize
                    for c in self.serialPort.read(size=messageSize):
                        message.append(ord(c))
                          
                    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[:] = []
            self.serialPort.close() 
    
    class Sender(Thread): 
        def __init__(self, serialPort): 
            Thread.__init__(self) 
            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)
            self.serialPort.close()
        def sendMessage(self, mailboxName,msg):
            mailboxMessage = MailboxMessage(mailboxName,msg)
            self.serialPort.write(mailboxMessage.Data())    
    
    serialPort = Serial(port='/dev/tty.EV3-SerialPort', baudrate=9600)
    
    send = Sender(serialPort) 
    receive = Receiver(serialPort) 
    send.start() 
    receive.start()
        
    
            

    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.

    Anders

    #3784

    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.

    Fergus

    #3787
    Author Image
    Anders Søborg
    Key Master

    No problem

    Anders

    #4490
    Author Image
    Pascal SALLIOT
    Participant

    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