问题描述
我想从python do arduino发送数据,以便通过继电器控制电机.想法是发送一个数字,以识别电动机和一个值,最后移动它.不幸的是,我遇到了一些问题.数据正在丢失.
I would like to send data from python do arduino in order to control motors via relays. Idea is to send a number, in order to identify a motor and a value, to finally move it. Unfortunately im struggling with some problems. Data is getting lost.
因此,在这个最小的示例中,有一个标识符"n"表示接收到的数据是变量"number",还有一个标识符"c"表示接收到的数据是一个计数器.为了找出问题所在,我将数据发送回Python并尝试读取它.此设置仅适用于第一个字符.所有其他数据都将丢失.
So, in this minimal example there is an identifier "n", to indicate that the received data is the variable "number" and an identifier "c" to identify that the received data is a counter. To find out what's wrong, I send the data back to Python and try to read it. This is setup is only working for the first to characters. All other data is getting lost.
有人可以给我提示吗?这些标识符是否必要?我是否必须添加一些delay()或其他波特率?
Can someone give me a hint? Are those identifiers necessary? Do I have to put some delay(), or maybe another baut-rate?
我想这与第二个标识符有关.例如,如果我仅使用计数器",则设置工作正常.
I guess it's something about the second identifier. The setup is working fine, if I only work with the "counter" for example.
非常感谢您!
Pyhton:
import serial
import time
Port = "/dev/cu.usbserial-A601FZBL"
ser = serial.Serial(Port,9600,timeout=1)
time.sleep(1)
counter = 0
def Test(counter):
ser.write(b"n")
ser.write(str(1).encode())
print("number:", 1)
get = ser.readline().decode()
print("get:", get)
ser.write(b"c")
ser.write(str(counter).encode())
print("counter:", counter)
get = ser.readline().decode()
print("get:", get)
print()
while True:
Test(counter)
counter += 1
Arduino:
void setup() {
Serial.begin(9600);
}
int number;
int counter;
void loop() {
if (Serial.available() > 0) {
if (Serial.peek() == 'n') {
Serial.read();
number = Serial.parseInt();
Serial.println(number);
if (Serial.peek() == 'c') {
Serial.read();
counter = Serial.parseInt();
Serial.println(counter);
}
while (Serial.available() > 0) {
Serial.read();
}
}
}
}
对不起!这是我的示例运行:(我很困惑,为什么在输出中的print()旁边还有其他空白行)
Sorry! Here is my sample-run:(I'm confused, why there are additional blank-lines, beside the print() in the output)
number: 1
get: 1
counter: 0
get:
number: 1
get:
counter: 1
get:
number: 1
get:
counter: 2
get:
number: 1
get:
counter: 3
get:
number: 1
get:
counter: 4
get:
number: 1
get:
counter: 5
推荐答案
感谢您的支持.因此,最终没有数据丢失;)问题是:Arduino在将数据发送回时正在创建空白行.因此"ser.readline()[:-2] .decode()"解决了此问题.当发送大于9的整数时,我遇到了另一个问题.为此,必须将整数拆分为单独的char并对其进行编码.这是我的解决方法:
Thanks for the support. So, finally no data did get lost ;) The problem was: Arduino is creating a blank-line, when sending data back. so "ser.readline()[:-2].decode()" solved this issue. I run into another problem, when sending integers bigger than 9. In order to do so, one has to split the integer into separated chars and encode them. Here is my workaround:
Python:
import serial
import time
Port = "/dev/cu.usbserial-A601FZBL"
ser = serial.Serial(Port,9600,timeout=1)
time.sleep(1)
counter = 0
def Test(counter):
data = [b"n", b"0", b"c"]
value = list(str(counter))
for char in value:
data.append(char.encode())
ser.write(data)
print("write:", "0", counter)
get_1 = ser.readline()[:-2].decode()
get_2 = ser.readline()[:-2].decode()
print("get: ", get_1, get_2)
print()
while True:
Test(counter)
counter += 1
Arduino:
void setup() {
Serial.begin(9600);
}
int number;
int counter;
void loop() {
while (Serial.available() > 0) {
if (Serial.peek() == 'n') {
Serial.read();
number = Serial.parseInt();
Serial.println(number);
if (Serial.peek() == 'c') {
Serial.read();
counter = Serial.parseInt();
Serial.println(counter);
}
while (Serial.available() > 0) {
Serial.read();
}
}
}
}
最后的输出:
write: 0 0
get: 0 0
write: 0 1
get: 0 1
write: 0 2
get: 0 2
write: 0 3
get: 0 3
write: 0 4
get: 0 4
write: 0 5
get: 0 5
write: 0 6
get: 0 6
write: 0 7
get: 0 7
write: 0 8
get: 0 8
write: 0 9
get: 0 9
write: 0 10
get: 0 10
write: 0 11
get: 0 11
这篇关于与arduino的串行通讯(用于电机控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!