我试图使用pySerial控制连接到Arduino和CNC屏蔽的步进电机。
我通过向串行接口发送字符来成功运行电机。现在我想选择旋转的数量,但我还没有。
我使用Tkinter作为我的GUI。
这是我的Python代码:
import serial # Import Serial Library
from Tkinter import *
import time
usbport = 'COM3'
serial_begin_is = 9600
arduinoSerialData = serial.Serial(usbport, serial_begin_is) # Create Serial port object called arduinoSerialData
class GUI():
def __init__(self, root):
# type: (object) -> object
self.root = root
self.root.geometry("750x600")
self.root.title("motor")
self.CreateWidgets()
self.spin = 0
self.num = 1
def CreateWidgets(self):
self.number_of_spins = 1.0
self.spin_num = 0.0
self.speed_val = StringVar()
self.spin_num_entry = Entry(self.root, textvariable=self.spin_num)
self.spin_num_entry.place(x=200, y=20)
self.speed_num_chose = Label(self.root, text="enter spin numbers:")
self.speed_num_chose.place(x=50, y=20)
self.enter_spin_num = Button(self.root, text="enter", command=lambda: self.set())
self.enter_spin_num.place(x=200, y=45)
# self.speed_num_label = Label(self.root, text="spin num will be" +str(self.number_of_spins))
# self.speed_num_label.place(x=255, y=45)
self.speed_num_entry = Entry(self.root, textvariable=self.speed_val)
self.speed_num_label = Label(self.root, text="spin num will be: " + str(self.number_of_spins))
self.speed_num_label.place(x=255, y=45)
self.run_b = Button(self.root, text="run", command=lambda: self.run(self.number_of_spins))
self.run_b.place(x=255, y=85)
# self.run_b.config(state=DISABLED)
self.spin_num_B = Button(self.root, text="stop", command=lambda: self.send_stop())
self.spin_num_B.place(x=255, y=150)
self.quit_b = Button(self.root, text="quit", command=lambda: root.destroy())
self.quit_b.place(x=50, y=500)
def set(self):
self.number_of_spins = int(self.spin_num_entry.get())
self.speed_num_label.config(text="spin num will be: " + str(self.number_of_spins))
self.run_b.config(state=NORMAL)
def run(self, x):
x = int(x)
print x
# for i in xrange(x):
arduinoSerialData.write(x)
arduinoSerialData.write(b'r')
print "run"
def send_stop(self):
arduinoSerialData.write(b's')
print "stop"
root = Tk()
GUI(root)
root.mainloop()
这是我的Arduino代码:
int cnt = 0 ;
long spin=0;
float num = 1.0 ;
float speed1 = 100;
char start ='s';
bool prt = false;
int flag =0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
#define stepPin 4
#define dirPin 7
#define enable 8
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()>0){
start = char(Serial.read());
if (start == 'r')
prt = true;
if (prt){
start = char(Serial.read());
if (start!='r'&&start!='s')
num = int(start);
while(spin <200*32*num){
start = char(Serial.read());
if (start =='s'){
digitalWrite(enable, HIGH);
prt = false;
spin = 200*32*num;
}
move();
spin++;
}
prt = false;
spin = 0;
}
if (start == 's'){
pinMode(enable, OUTPUT);
digitalWrite(enable, HIGH);
}
}
}
void move () {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enable, OUTPUT);
digitalWrite(enable, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed1);
}
答案 0 :(得分:1)
python代码:
import serial # Import Serial Library
from Tkinter import *
import time
usbport = 'COM3'
serial_begin_is = 38400
arduinoSerialData = serial.Serial(usbport, serial_begin_is,timeout=0.5) # Create Serial port object called arduinoSerialData
class GUI():
def __init__(self, root,arduinoSerialData):
# type: (object) -> object
self.root = root
self.root.geometry("750x600")
self.root.title("motor")
self.CreateWidgets()
self.spin = 0
self.num = 1
self.arduinoSerialData=arduinoSerialData
self.flag = True;
def CreateWidgets(self):
self.number_of_spins = 0
self.speed_value = 150
self.spin_num = StringVar()
self.speed_val = StringVar()
self.spin_num_entry = Entry(self.root, textvariable=self.spin_num)
self.spin_num_entry.place(x=200, y=20)
self.spin_num_chose = Label(self.root, text="enter spin numbers:")
self.spin_num_chose.place(x=50, y=20)
self.enter_spin_num = Button(self.root, text="enter", command=lambda: self.set())
self.enter_spin_num.place(x=200, y=45)
# self.spin_num_label = Label(self.root, text="spin num will be" +str(self.number_of_spins))
# self.spin_num_label.place(x=255, y=45)
self.speed_label = Label(text="speed val will be:"+str(self.speed_value))
self.speed_label.place(x=550,y=75)
self.speed_b = Button(self.root, text="enter", command=lambda: self.speed_set())
self.speed_b.place(x=550, y=45)
self.speed_num_entry = Entry(self.root, textvariable=self.speed_val)
self.speed_num_entry.place(x=550, y=20)
self.speed_num_chose = Label(self.root, text="enter speed value:")
self.speed_num_chose.place(x=420, y=20)
self.spin_num_label = Label(self.root, text="spin num will be: " + str(self.number_of_spins))
self.spin_num_label.place(x=255, y=45)
self.run_b = Button(self.root, text="run", command=lambda: self.run())
self.run_b.place(x=255, y=85)
self.run_b.config(state=DISABLED)
self.spin_num_B = Button(self.root, text="stop&reset", command=lambda: self.send_stop())
self.spin_num_B.place(x=255, y=150)
self.quit_b = Button(self.root, text="quit", command=lambda: root.destroy())
self.quit_b.place(x=50, y=500)
self.dir_b = Button(self.root, text="change direction", command=lambda: self.dirf())
self.dir_b.place(x=295, y=85)
def set(self):
self.number_of_spins = int(self.spin_num_entry.get())
self.data = [b"s"]
#self.arduinoSerialData.write(self.data)
value = list(str(self.number_of_spins))
for char in value:
self.data.append(char.encode())
print self.data
self.spin_num_label.config(text="spin num will be: " + str(self.number_of_spins))
self.run_b.config(state=NORMAL)
def run(self):
run_char= "r"
self.data[0] = run_char.encode()
print self.data
self.arduinoSerialData.write(self.data)
print "run"
self.spin_num_entry.delete(0, END)
def send_stop(self):
stop = "s"
self.data[0] =stop.encode()
self.arduinoSerialData.write(self.data)
print self.data
self.arduinoSerialData.close()
self.arduinoSerialData = serial.Serial(usbport, serial_begin_is)
self.speed_label.place_forget()
self.speed_label.config(text="speed val will be:" +str(self.speed_value))
self.speed_label.place(x=550, y=75)
time.sleep(0.05)
def dirf(self):
print "direction had been chaged "
self.arduinoSerialData.write(b"d")
def speed_set(self):
self.speed_label.place_forget()
self.speed_label.config(text="speed val will be:"+self.speed_num_entry.get())
self.speed_label.place(x=550, y=75)
self.speed_in_int = int(self.speed_num_entry.get())
self.data_speed = [b"v"]
value = list(str(self.speed_in_int))
for char in value:
self.data_speed.append(char.encode())
print self.data_speed
self.arduinoSerialData.write(self.data_speed)
root = Tk()
GUI(root,arduinoSerialData)
root.mainloop()
Arduino代码:
int cnt = 0 ;
long spin=0;
float num = 1.0 ;
float speed1 = 150;
char start ;
bool prt = false;
bool dirf = true;
int flag =0;
String data[2];
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
#define stepPin 4
#define dirPin 7
#define enable 8
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enable, OUTPUT);
digitalWrite(enable, HIGH);
digitalWrite(dirPin, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()>0){
if (Serial.peek() == 'r') {
prt = true;
Serial.read();
num = Serial.parseInt();
//Serial.println(num);
}
if (prt){
while(spin <200*32*num){
move();
spin++;
if (Serial.peek() == 's') {
Serial.read();
digitalWrite(enable, HIGH);
prt = false;
spin = 200*32*num;
}
if (Serial.peek() == 'd') {
Serial.read();
dirf = !dirf;
dir();
Serial.read();
}
if (Serial.peek() == 'v') {
Serial.read();
speed1 = Serial.parseInt();
Serial.read();
}
}
prt = false;
spin = 0;
//num = 1;
}
if (Serial.peek() == 's') {
Serial.read();
digitalWrite(enable, HIGH);
prt = false;
spin = 0;
num = 1;
}
if (Serial.peek() == 'd') {
dirf = !dirf;
dir();
}
if (Serial.peek() == 'v') {
Serial.read();
speed1 = Serial.parseInt();
Serial.read();
}
}
/*
while (Serial.available() > 0) {
Serial.read();
}
*/
}
void move () {
digitalWrite(enable, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed1);
}
void dir (){
if (dirf)
digitalWrite(dirPin, HIGH);
else
digitalWrite(dirPin, LOW);
}