具有多个变量的arduino python接口 [英] arduino python interface with multiple variables
问题描述
我想将 3 个不断变化的传感器变量带到我的 Python 界面.
I want to bring 3 sensor variables that change all the time to my python interface.
我正在尝试使用此测试代码,它不起作用,我做错了什么?
I am trying with this test code, it does not work, what am I doing wrong?
阿杜诺:
void setup() {
Serial.begin (9600);
}
void loop() {
Serial.print(random(1,3));
Serial.print(random(3,5));
Serial.print(random(5,7));
}
蟒蛇:
canvas.create_text(190, 150, text=ser.readline(1), fill="gray", font="Helvetica 45 bold",tag="T1")
如何让多个变量一直更新?现在我只是得到第一个,它没有更新
How can I get multiple variables updating all the time? right now I am just getting the first one, and it is not updating
推荐答案
为什么使用 readline
函数只读取一个字节?由于您需要多个值,请用(例如)空格分隔变量,然后使用 readline
将它们全部存储并使用 split
将它们分开:
Why are you using the readline
function to read just one byte? Since you want multiple values, separate the variables with (for instance) a space, then use readline
to store them all and split
to separate them:
PS:注意最后一个其实是一个println
PS: note that the last one is actually a println
阿杜诺:
void loop() {
Serial.print(random(1,3));
Serial.print(" ");
Serial.print(random(3,5));
Serial.print(" ");
Serial.println(random(5,7));
}
蟒蛇:
allitems=ser.readline()
separateditems=allitems.split();
canvas.create_text(190, 150, " - ".join(separateditems), fill="gray", font="Helvetica 45 bold",tag="T1")
在这个例子中,我把项目放在 separateditems
列表中(所以 separateditems[0]
等于 random(1,3)
, separateditems[1]
等于 random(3,5)
并且 separateditems[2]
等于 random(5,7)
).然后我加入他们以显示随机(1,3)-随机(3,5)-随机(5,7)".无论如何,您可以对收集到的数据做任何想做的事情.
In this example I put the items in the separateditems
list (so separateditems[0]
is equal to random(1,3)
, separateditems[1]
is equal to random(3,5)
and separateditems[2]
is equal to random(5,7)
). Then I joined them to display "random(1,3) - random(3,5) - random(5,7)". Anyway you can do whatever you want with the collected data.
那么我强烈建议您在 loop
内设置延迟,以避免发送太多数据.我建议将 delay(100);
放在最后,或者,如果您在等待时需要做其他事情,请参阅无延迟弹跳"示例.
Then I HIGHLY suggest you to put a delay inside the loop
, to avoid sending too many data. I suggest putting a delay(100);
at the end or, if you need to do other things while waiting, see the "Bounce without delay" example.
这篇关于具有多个变量的arduino python接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!