Reply To: Trigger brick program from Python

HomeForumsMonoBrick Communication LibraryTrigger brick program from PythonReply To: Trigger brick program from Python

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 = bytearray(b"") number 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.


Posted in

Make a donation