#Define the serial port
uart1 = UART(1, baudrate = 115200, tx = Pin(4), rx = Pin(5))
#Define five-wire four-phase motor pins
motor1a = Pin(9,Pin.OUT)
motor1b = Pin(10,Pin.OUT)
motor1c = Pin(11,Pin.OUT)
motor1d = Pin(12,Pin.OUT)
def forward(delay, maxspeed): #Stepper motor forward function, define stepper motor as double four-shot drive mode
for i in range(0, maxspeed):
motor1a.high()
motor1b.high()
motor1c.low()
motor1d.low()
time.sleep(delay)
motor1a.low()
motor1b.high()
motor1c.high()
motor1d.low()
time.sleep(delay)
motor1a.low()
motor1b.low()
motor1c.high()
motor1d.high()
time.sleep(delay)
motor1a.high()
motor1b.low()
motor1c.low()
motor1d.high()
time.sleep(delay)
def backward(delay, maxspeed): #Stepper motor reversal function
for i in range(0, maxspeed):
motor1a.low()
motor1b.low()
motor1c.high()
motor1d.high()
time.sleep(delay)
motor1a.low()
motor1b.high()
motor1c.high()
motor1d.low()
time.sleep(delay)
motor1a.high()
motor1b.high()
motor1c.low()
motor1d.low()
time.sleep(delay)
motor1a.high()
motor1b.low()
motor1c.low()
motor1d.high()
time.sleep(delay)
def stop(): #Motor stop function
motor1a.low()
motor1b.low()
motor1c.low()
motor1d.low()
def dianji():
forward(0.005, 512)
stop()
time.sleep(3)
backward(0.005, 512)
stop()
time.sleep(3)
def zrun():
forward(0.005, 512)
def frun():
backward(0.005, 512)
#counti = 0
#list2 = []
motor_off = [‘S’, ‘T’, ‘<‘, ‘\x10’, ‘\x10’, ‘\x00’, ‘\x07’, ‘s’, ‘w’, ‘i’, ‘t’, ‘c’, ‘h’, ‘\x00’, ‘>’, ‘E’, ‘T’]#Serial screen command, you can modify it by yourself
motor_on = [‘S’, ‘T’, ‘<‘, ‘\x10’, ‘\x10’, ‘\x00’, ‘\x07’, ‘s’, ‘w’, ‘i’, ‘t’, ‘c’, ‘h’, ‘\x01’, ‘>’, ‘E’, ‘T’]
time.sleep(1)
while True:
counti = 0
list2 = []
try:
rxdata = uart1.read(40)
list1 = list(rxdata)
except BaseException as e:
print(‘串口没有接收到数据’)
time.sleep(3)
else:
#list1 = list(jieshou)
for item in list1:
list2.append(chr(item))
counti += 1
if item == 62: #The serial port returns the decimal value of the ASCII code of the end sign’>’ in the command data
for item2 in range(2): #When the’>’ is read, two characters will be read later to display the complete command, but there are actually two check digit#characters behind
counts = list1[counti]
list2.append(chr(counts))
counti += 1
break
print(list2)
if list2 == motor_on:
zrun()
elif list2 == motor_off:
stop()
uart1.sendbreak()