Prompt Detail:
我想要使用以下程式來驅動我的XY滑台:
import serial
import time
class StageCommander:
def __init__(self, port='COM3', baudrate=38400):
self.ser = serial.Serial(port, baudrate)
self.ser.timeout = 1 # Set a timeout for reading
def send_command(self, command):
self.ser.write(command.encode())
time.sleep(0.5) # Add a delay to ensure the command is processed
return self.ser.readline().decode().strip() # Read the response from the device
def drive_to_absolute_position(self, axis, position):
command = f"{axis}:GOABSOLUTE {position}" # 注意這裡的空格
response = self.send_command(command)
print(response) # Print the device's response
def close(self):
self.ser.close()
# 使用範例:
commander = StageCommander('COM3', 38400)
# Drive X-axis to position 10:
commander.drive_to_absolute_position(axis='X', position=10)
# Drive Y-axis to position 10:
commander.drive_to_absolute_position(axis='Y', position=10)
# Drive X-axis to position 50:
commander.drive_to_absolute_position(axis='X', position=50)
# Drive Y-axis to position 20:
commander.drive_to_absolute_position(axis='Y', position=20)
# 結束通信
commander.close()
但發生了錯誤:
E21
E21
E21
E21
根據說明書的意思,E21代表的未發送的分隔符錯誤,無分隔符 (CR) 或不正確。
請修正程式碼。
Add a comment