简体   繁体   English

让Arduino读取一个字符串(通过Python发送),相应地移动Motor进行输入,然后接收下一个字符串

[英]Have an Arduino read a string (sent via Python), move a Motor accordingly to input, then receive the next string

I am working on a Rubiks Cube Solver, which uses an Arduino and motors to move each face around. 我正在研究Rubiks多维数据集求解器,该求解器使用Arduino和电机来移动每个面。 The Python script I use writes a move per line, then is meant to the first line, then the second line, and so on 我使用的Python脚本每行写一个移动,然后是第一行,然后是第二行,依此类推

x = 0
    with open(FILEPATH, "r") as f:
        lines = f.readlines()
        FileEnd = False
        while FileEnd == False:
            try:
                ser.write(lines[x])
                print lines[x]
                x += 1

            except serial.SerialException:
                time.sleep(1)

             except IndexError:
                FileEnd = True

The Arduino is supposed to receive this String, stop reading from the Serial, act accordingly as to what is in the String, then accept then next String. Arduino应该接收此字符串,停止从串行读取,对字符串中的内容采取相应措施,然后接受下一个字符串。

void setup()
{
  Serial.begin(9600);
  Serial.flush();
  Serial.println("Power On");

}

void loop()
{
  Serial.begin(9600);
  // Read from Serial Port here
  Serial.end();
  // Act accordingly to String here
  Serial.flush();
  //Start all over again
}

I am faced with two Problems: 我面临两个问题:

1) The Python script keeps sending each move, uncaring that Serial.end() should have occured 1)Python脚本不断发送每个动作,不关心应该发生Serial.end()

2) When I read the Serial Monitor, I see the same bunch of seemingly random characters that flood the entire moniter upon receiving input from the Python Script 2)当我阅读串行监视器时,我看到一堆看似随机的字符,它们从Python脚本接收到输入后便淹没了整个监控器

Help Please? 请帮助? :3 :3

I have a tiny bit of experience with doing stuff like this. 我在做这样的事情上有一点经验。 I think pyserial may help in this case http://pyserial.sourceforge.net/ In terms of the serial monitor, I believe there is a way to flush the data before you sent with with pyserial. 我认为pyserial在这种情况下可能会有所帮助http://pyserial.sourceforge.net/就串行监视器而言,我相信有一种方法可以在您发送pyserial之前刷新数据。 There are a lot of stuff out there with arduino and pyserial. arduino和pyserial有很多东西。

I would recommend sending the commands in G-Code Format. 我建议以G代码格式发送命令。

There are several Arduino firmwares that already accept G-Code and drive multiple motors. 有几种Arduino固件已经接受G代码并驱动多个电机。 This way you don't need to re-invent both the Serial Receiver, Multiple Motor Command Queue and Multiple Motor software drivers. 这样,您无需重新发明串行接收器,多电机命令队列和多电机软件驱动程序。

I would recommend Grbl firmware. 我会推荐Grbl固件。 It will run on an UNO and drive step motors from a variety of shields. 它将在UNO上运行,并通过各种防护罩驱动步进电机。

So that your Python can simply send and forget the G code commands, as needed. 这样您的Python可以根据需要简单地发送和忘记G代码命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM