Skip to content

Receive command from host computer via SerialPort #5103

@TanguyBoulard

Description

@TanguyBoulard

Hi all,

I am using {Adafruit Metro M0 Express + Serial communication + Adafruit Shileld Motor Shield V2 + Stepper motor}, and I'm programming it with Circuit Python.
I want to receive command transmitted from the computer to which the Metro is connected via USB. Then I can execute the right command and activate motors.

CircuitPython Code:

import time
import board
from adafruit_motor import stepper
from adafruit_motorkit import MotorKit
import supervisor

kit = MotorKit(i2c=board.I2C())

def FORWARD():
    for i in range(100):
        kit.stepper2.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)
        time.sleep(0.01)

def BACKWARD():
    for i in range(100):
        kit.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)
        time.sleep(0.01)

def EXECUTE():
    if value == 'forward':
            FORWARD()

    elif value == 'backward':
            BACKWARD()

while True:
    if supervisor.runtime.serial_bytes_available:
        value = input().strip()
        command = value.decode('utf-8')
        EXECUTE(command)

PC code:

import serial

port = serial.Serial('COM16', 115200)
if not port.isOpen():
    port.open()
port.flush()

command = b'forward\n\r'
port.write(command)

port.close()

But nothing happen!

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportissues that involve helping a user accomplish a task

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions