通过串行通信将值从Raspberry(Python)发送到Arduino的快速方法?

通过串行通信将值从Raspberry(Python)发送到Arduino的快速方法?,python,c++,arduino,serial-port,raspberry-pi,Python,C++,Arduino,Serial Port,Raspberry Pi,我想通过RX/TX串行端口(非USB)从Raspberry向Arduino发送一个int值(0-640) 目前,我通过UDP接收Raspberry上的值,因此接收的类型是“class‘bytes’”。我将字节转换为int,将其编码为字符串并发送给Arduino 我的问题是在Arduino端从ASCII转换为int非常慢。当然,最好的情况是有一些简单的东西,比如: 覆盆子(梦境场景): Arduino(梦想场景): 有什么建议吗?有问题吗 到目前为止,我的代码是: 树莓码: import stru

我想通过RX/TX串行端口(非USB)从Raspberry向Arduino发送一个int值(0-640)

目前,我通过UDP接收Raspberry上的值,因此接收的类型是“class‘bytes’”。我将字节转换为int,将其编码为字符串并发送给Arduino


我的问题是在Arduino端从ASCII转换为int非常慢。当然,最好的情况是有一些简单的东西,比如:

覆盆子(梦境场景):

Arduino(梦想场景):

有什么建议吗?有问题吗

到目前为止,我的代码是:

树莓码:

import struct
import serial

serial = serial.Serial('/dev/ttyAMA0', 115200)

while True:
    # (This is here to show you where it comes from):
    data, addr = sock.recvfrom(4)

    if len(data)!=4:
        continue
    else:
        # Convert data to int
        msg = struct.unpack('i',data)[0]
        # Send to Arduino
        serial.write(str(msg).encode() + str('\n').encode())
Arduino代码:

void setup()
{
  // Opens serial ports, sets data rate
  Serial1.begin(115200); // Serial1 = reads from Pi
}

void loop()
{
  if (Serial1.available())
  {
    // Resets RPiMsg
    RPiMsg = 0;

    // Read serial one byte at a time and convert ASCII to int
    while(true)
    {
      incomingByte = Serial1.read();

      // Exit while-loop, end of message
      if (incomingByte == '\n') break;

      // If nothing is in the buffer, Serial1.read() = -1
      if (incomingByte == -1) continue;

      // Shift 1 decimal place left
      RPiMsg *= 10;

      // Convert ASCII to int
      RPiMsg = ((incomingByte - 48) + RPiMsg);
    }

    Serial.println(RPiMsg);
  }
}

我觉得你的代码看起来不错!实际问题是什么?我的问题是在Arduino端从ASCII转换为int非常慢。它以“块”的形式出现,一次可能有10-20条消息。然后是一点延迟,然后是另一块@汤马斯帕伯也许你需要
冲洗
覆盆子面。。。在调用writeAre后,您确定转换速度慢吗?不应该这样。无论如何,我将以每个数字2字节的形式发送数据,并按原样使用它们,无转换
RPiMsg=(data1aaaa,好吧,我猜:串行端口很慢,所以每次只发送一个数字。Pi接收消息,发送一个数字,等待arduino能够接收更多,然后发送另一个。
import struct
import serial

serial = serial.Serial('/dev/ttyAMA0', 115200)

while True:
    # (This is here to show you where it comes from):
    data, addr = sock.recvfrom(4)

    if len(data)!=4:
        continue
    else:
        # Convert data to int
        msg = struct.unpack('i',data)[0]
        # Send to Arduino
        serial.write(str(msg).encode() + str('\n').encode())
void setup()
{
  // Opens serial ports, sets data rate
  Serial1.begin(115200); // Serial1 = reads from Pi
}

void loop()
{
  if (Serial1.available())
  {
    // Resets RPiMsg
    RPiMsg = 0;

    // Read serial one byte at a time and convert ASCII to int
    while(true)
    {
      incomingByte = Serial1.read();

      // Exit while-loop, end of message
      if (incomingByte == '\n') break;

      // If nothing is in the buffer, Serial1.read() = -1
      if (incomingByte == -1) continue;

      // Shift 1 decimal place left
      RPiMsg *= 10;

      // Convert ASCII to int
      RPiMsg = ((incomingByte - 48) + RPiMsg);
    }

    Serial.println(RPiMsg);
  }
}