多变量Arduino的Python接口 [英] arduino python interface with multiple variables
问题描述
我想要把所有的时间改到我的Python接口3传感器变量。
I want to bring 3 sensor variables that change all the time to my python interface.
我与这个测试code尝试,它不工作,我究竟做错了什么?
I am trying with this test code, it does not work, what am I doing wrong?
Arduino的:
void setup() {
Serial.begin (9600);
}
void loop() {
Serial.print(random(1,3));
Serial.print(random(3,5));
Serial.print(random(5,7));
}
的Python:
Python:
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
来存储所有这些,拆分$ C $分隔变量C>将它们分开:
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
Arduino的:
void loop() {
Serial.print(random(1,3));
Serial.print(" ");
Serial.print(random(3,5));
Serial.print(" ");
Serial.println(random(5,7));
}
的Python:
Python:
allitems=ser.readline()
separateditems=allitems.split();
canvas.create_text(190, 150, " - ".join(separateditems), fill="gray", font="Helvetica 45 bold",tag="T1")
在这个例子中,我把项目中的 separateditems
列表(让 separateditems [0]
等于随机的(1,3)
, separateditems [1]
等于随机(3,5- )
和 separateditems [2]
等于随机的(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.
那么我强烈建议你把一个延迟循环
里面,以避免发送太多的数据。我建议把一个延迟(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屋!