Initialize the I2C address and frequency settings for the stepper motor, set the direction for motors X and Y, read the device's firmware version and I2C address, and then in the loop, execute the reset for motors X and Y, enable/disable the motors, and switch the status at certain time intervals.
from m5stack import *
from m5ui import *
from uiflow import *
import module
import time
setScreenColor(0x000000)
stepmotor = module.get(module.STEPMOTORDRIVER)
stepmotor.initDevice(0x27)
stepmotor.setStepPulse(500, 0)
stepmotor.setStepPulse(500, 1)
stepmotor.setStepPulse(500, 2)
stepmotor.setStepDir(0, 1)
stepmotor.setStepDir(1, 1)
stepmotor.enableMotor(1)
print(stepmotor.readStatus(0XFE))
print(stepmotor.readStatus(0XFF))
while True:
stepmotor.resetMotor(0, 1)
wait(2)
stepmotor.resetMotor(1, 1)
wait(2)
stepmotor.enableMotor(0)
wait(2)
stepmotor.enableMotor(1)
wait(2)
stepmotor.resetMotor(0, 0)
wait(2)
stepmotor.resetMotor(1, 0)
wait(2)
wait_ms(2)
stepmotor.enableMotor(0)
stepmotor.initDevice(0x27)
stepmotor.modbus_init(26, 34, 115200, 1, 1)
stepmotor.readStatus(0XFE)
stepmotor.readFaultPinStatus(0)
stepmotor.readIOstatus()
stepmotor.readPinStatus(0)
stepmotor.resetMotor(0, 1)
stepmotor.setI2cAddress(0x27)
stepmotor.setMicroStepSelect(0)
stepmotor.singleMotorCtrl(0, 0)
stepmotor.setStepDir(0, 0)
stepmotor.setStepPulse(500, 0)
modbus.read_coils(1, 1, 0)
modbus.read_discrete_inputs(1, 1, 0)
, reading 0 inputs. This is a commonly used operation in the Modbus protocol, typically for reading the status of switches, buttons, and other digital inputs.
modbus.read_holding_registers(1, 1, 0, True)
modbus.read_input_registers(1, 1, 0, True)
modbus.write_multiple_coils(1, 1, 0)
modbus.write_multiple_registers(1, 1, 0, True)
modbus.write_single_coil(1, 1, 0)
modbus.write_single_register(1, 1, 0, True)
1~6,15,16
READ_COILS_STATUS
, which means reading coil status. The function codes commonly used are:modbus.find_address
modbus.find_function
modbus.find_quantity
modbus.function_init(1, 0, 0)
modbus.receive_req_create_pdu()
modbus.create_slave_response(0)
modbus.update_process(1, 0, 0, [0, 0, 0])
READ_COILS_STATUS
function code of the Modbus slave device. It sets the starting address to 0, reads 0 coils, and uses a list to store the data returned by the slave device. This command is usually used to update the coil status after the device receives a data request.modbus._mdbus_uart.any()
modbus._mdbus_uart.read()
modbus._mdbus_uart
.readline()
modbus._mdbus_uart.read(10)
modbus._mdbus_uart.write('')
modbus._mdbus_uart.write(''+"\r\n")
modbus._mdbus_uart.write(bytes([0, 0, 0]))