Exemplo n.º 1
0
    def __init__(self):
        # make sure this baudrate matches the baudrate True the Arduino
        self.running = False
        self.led_state = False
        self.baud = 115200
        self.commands = [
            'set_led',
            'status',
        ]

        try:
            # gets the first available USB port
            self.port_name = self.list_usb_ports()[0][0]
            self.serial_port = serial.Serial(self.port_name,
                                             self.baud,
                                             timeout=0)
        except (serial.SerialException, IndexError):
            raise SystemExit('Could not open serial port.')
        else:
            print 'Serial port sucessfully opened.'
            self.messenger = CmdMessenger(self.serial_port)
            # attach callbacks
            self.messenger.attach(func=self.on_unknown_command)
            self.messenger.attach(func=self.on_status,
                                  msgid=self.commands.index('status'))
Exemplo n.º 2
0
    def __init__(self):
        # make sure this baudrate matches the baudrate on the Arduino
        self.running = False
        self.baud = 115200
        self.commands = [
            'acknowledge',
            'error',
            'float_addition',
            'float_addition_result',
        ]

        try:
            # try to open the first available usb port
            self.port_name = self.list_usb_ports()[0][0]
            self.serial_port = serial.Serial(self.port_name,
                                             self.baud,
                                             timeout=0)
        except (serial.SerialException, IndexError):
            raise SystemExit('Could not open serial port.')
        else:
            print 'Serial port sucessfully opened.'
            self.messenger = CmdMessenger(self.serial_port)
            # attach callbacks
            self.messenger.attach(func=self.on_error,
                                  msgid=self.commands.index('error'))
            self.messenger.attach(
                func=self.on_float_addition_result,
                msgid=self.commands.index('float_addition_result'))

            # send a command that the arduino will acknowledge
            self.messenger.send_cmd(self.commands.index('acknowledge'))
            # Wait until the arduino sends and acknowledgement back
            self.messenger.wait_for_ack(
                ackid=self.commands.index('acknowledge'))
            print 'Arduino Ready'
Exemplo n.º 3
0
    def __init__(self, port, baudrate):
        # Initialize the serial / command messenger connection
        self.ser = serial.Serial(port=port, baudrate=baudrate, timeout=0)
        self.cm = CmdMessenger(self.ser)

        # attach callbacks
        self.cm.attach(self._onUnknownCommand)

        # Start monitoring
        self.cmm = CmdMessengerMonitor(self.cm)
        self.cmm.start()
Exemplo n.º 4
0
    def __init__(self):
        self.running = False
        self.led_state = False
        # make sure this baudrate matches the baudrate on the Arduino
        self.baud = 115200
        self.commands = ['set_led']

        try:
            # gets the first available USB port
            self.port_name = self.list_usb_ports()[0][0]
            self.serial_port = serial.Serial(self.port_name, self.baud, timeout=0)
        except (serial.SerialException, IndexError):
            raise SystemExit('Could not open serial port.')
        else:
            print 'Serial port sucessfully opened.'
            self.messenger = CmdMessenger(self.serial_port)
Exemplo n.º 5
0
    def __init__(self):
        # make sure this baudrate matches the baudrate on the Arduino
        self.running = False
        self.baud = 57600
        self.commands = [
            'acknowledge',
            'error',
            'drive_robot',
            'robot_status',
        ]

        try:
            # try to open the first available usb port
            self.port_name = self.list_usb_ports()[0][0]
            # Initialize an ArduinoBoard instance.  This is where you specify baud rate and
            # serial timeout.  If you are using a non ATmega328 board, you might also need
            # to set the data sizes (bytes for integers, longs, floats, and doubles).
            #arduino = PyCmdMessenger.ArduinoBoard("/dev/ttyACM0",baud_rate=9600)
            self.serial_port = serial.Serial(self.port_name,
                                             self.baud,
                                             timeout=0)
        except (serial.SerialException, IndexError):
            raise SystemExit('Could not open serial port.')
        else:
            print('Serial port sucessfully opened.')
            # Initialize the messenger
            self.messenger = CmdMessenger(self.serial_port)

            # attach callbacks
            self.messenger.attach(func=self.on_error,
                                  msgid=self.commands.index('error'))
            self.messenger.attach(func=self.on_robot_status,
                                  msgid=self.commands.index('robot_status'))

            # send a command that the arduino will acknowledge
            self.messenger.send_cmd(self.commands.index('acknowledge'))
            # Wait until the arduino sends and acknowledgement back
            self.messenger.wait_for_ack(
                ackid=self.commands.index('acknowledge'))
            print('Arduino Ready')
Exemplo n.º 6
0
import time

from cmdmessenger import CmdMessenger
from serial.tools import list_ports

DEV = '/dev/tty.wchusbserialfd12410'

CMD_ACK = 0
CMD_INFO = 1
CMD_ERROR = 2
CMD_SET_SPEED = 3

port = serial.Serial(DEV, 9600)
print('connected to', port.name)

msgr = CmdMessenger(port)

print('waiting for arduino...')
#msgr.send_cmd(CMD_ACK)
#msgr.wait_for_ack(CMD_ACK)
print("arduino ready")


def on_error(received_command, *args, **kwargs):
    print('Error: ', args[0][0])

def on_info(received_command, *args, **kwargs):
    print('Info: ', args[0][0])

def on_unknown_cmd(received_command, *args, **kwargs):
    print('unknown cmd')