Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python arduino移动伺服_Python_Tkinter_Arduino_Pyserial - Fatal编程技术网

使用python arduino移动伺服

使用python arduino移动伺服,python,tkinter,arduino,pyserial,Python,Tkinter,Arduino,Pyserial,在这里,我试图用Tkinter制作的滑块GUI控制我的sg90伺服,并通过串行方式将滑块的当前值发送给Arduino。现在的问题是,无论何时说,我将滑块从72快速移动到77,它在串行监视器中显示的值是7374757677 以下是python代码: from tkinter import * screen = Tk() screen.geometry("400x400") #some commands for arduino import serial uno = serial.Serial(

在这里,我试图用Tkinter制作的滑块GUI控制我的sg90伺服,并通过串行方式将滑块的当前值发送给Arduino。现在的问题是,无论何时说,我将滑块从72快速移动到77,它在串行监视器中显示的值是7374757677

以下是python代码:

from tkinter import *

screen = Tk()
screen.geometry("400x400")

#some commands for arduino
import serial
uno = serial.Serial('/dev/ttyACM0', 9600)

def servocontrol(var):
    uno.write(str(servo.get()).encode())

#defining the widget
servo = Scale(screen, from_=0, to=180, orient=HORIZONTAL, command=servocontrol)

#packing the widget
servo.pack()

#running the loop
screen.mainloop()
这里是Arduino代码:

#include <Servo.h>

Servo myservo;
String pypos;

int pos = 0;

void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0) {
    pypos = Serial.readString();
    Serial.println("Angle: " + pypos);
    Serial.println('\n');
    int pyposint = pypos.toInt();
    myservo.write(pyposint);
    Serial.flush();
    delay(15);
  }  
}
#包括
伺服myservo;
字符串pypos;
int pos=0;
无效设置()
{
附件(9);
Serial.begin(9600);
}
void循环()
{
如果(Serial.available()>0){
pypos=Serial.readString();
Serial.println(“角度:+pypos”);
Serial.println('\n');
int-pyposint=pypos.toInt();
myservo.write(pyposint);
Serial.flush();
延误(15);
}  
}
现在看看输出


我不知道出了什么问题。如果有人能帮助我,那将是一个很大的帮助。

如果有人也在寻找这个问题的答案,我找到了一个解决办法,这里是我所做的

我没有通过编码将数据作为字符串发送,而是以字节格式将数据作为整数发送

从那以后一切都很好

uno.write(bytes([var]))