与 arduino 的 pyserial 通信(用于电机控制) [英] pyserial communication with arduino (for motor-control)

查看:23
本文介绍了与 arduino 的 pyserial 通信(用于电机控制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 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.

谁能给我一个提示?这些标识符是必需的吗?我是否必须放置一些延迟(),或者另一个 baut-rate?

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.

在此先非常感谢您!

派顿:

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

阿杜诺:

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 的整数时,我遇到了另一个问题.为此,必须将整数拆分为单独的字符并对其进行编码.这是我的解决方法:

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:

蟒蛇:

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

阿杜诺:

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 的 pyserial 通信(用于电机控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆