我试图使我的Arduino微控制器和Mac一起聊天,并创建了功能正常的串行连接。我的计算机正在向Arduino发送数据,而当Arduino准备接收新数据时,它正在发送'1'

我创建了一个if-else语句(下面的Python脚本),该语句要么向Arduino发送新的数据行,要么等待Arduino准备好接收新的数据行。

问题在于,Python脚本第一部分中的ser.read()始终返回'1',这意味着该脚本发送单个数据线的速度快于Arduino连接的步进电机可能做出的反应。

在Arduino脚本中,您可以看到我正在serialEvent()函数的第一行中发送状态状态,在我的世界中,状态应让Arduino在新的“任务”出现之前完成其工作。但是,由于某种原因,它不起作用。有人可以帮我吗?

Python脚本

import os
import time
import serial

# Name of csv file with drawing coordinates
csvFile = "scaled_coordinates.csv"

# Create serial connection
ser = serial.Serial(port='/dev/tty.usbserial-A9005bDh', baudrate=9600)

wd = os.getcwd()
myFile = open(wd + "/coordinates/" + csvFile)

state = '1'

while True: # Exits when there is no more lines to read

    if state == '0': # Wait for Arduino to be ready
        state = ser.read()

    elif state == '1': # Send one more line to Arduino
        line = myFile.readline()
        if not line:
            break
        print line
        ser.write(line)
        #time.sleep(1)

        state = '0' # Wait for Arduino before reading next line

myFile.close


Arduino loop功能

void loop() {

  serialEvent(); // Call the serial function

  if (coord_complete) {

    // Steps to move from currrent to new point
    target1 = steps(x_current, y_current, x_new, y_new, 1);
    target2 = steps(x_current, y_current, x_new, y_new, 2);

    // Start moving
    stepper1.move(target1);
    stepper2.move(target2);

    // Update current position
    x_current = x_new;
    y_current = y_new;

    // Reset variables
    x_complete = false;
    y_complete = false;
    coord_complete = false;
  }

  // Stay in while loop until steppermotors is done
  while ((stepper1.distanceToGo() != 0) && (stepper2.distanceToGo() != 0)) {
    stepper1.run();
    stepper2.run();
  }
}


Arduino serialEvent功能

void serialEvent() {

  Serial.write('1'); // Tell Python that Arduino is ready for one more line

  while (Serial.available() && coord_complete == false) {
    char ch = Serial.read(); // Get new character
    Serial.print(ch);

    // If digit; add it to coord_string
    if (isDigit(ch)) {
      coord_string[index++] = ch;

    // Else if ch is ","; then rename to x_new
    } else if (ch == ',') {
      coord_string[index++] = NULL;                   // Finish coord_string
      x_new = atoi(coord_string);                     // Convert to integer
      x_complete = true;                              // Change x_complete to true
      index = 0;                                      // Reset index
      memset(coord_string, 0, sizeof(coord_string));  // Reset coord_string

    // Else if ch is a new line; then rename as y_new
    } else if (ch == ';') {
      //Serial.write('0');
      coord_string[index++] = NULL;
      y_new = atoi(coord_string);
      y_complete = true;
      index = 0;
      memset(coord_string, 0, sizeof(coord_string));
    }

    // Ends while-loop when true
    coord_complete = x_complete * y_complete;
  }
}


编辑

当前的Python代码如下所示:

import os
import time
import serial

# Name of csv file with drawing coordinates
csvGraphic = "Scaled_coordinates.csv"

# Create serial connection
ser = serial.Serial(port='/dev/tty.usbserial-A9005bDh', baudrate=9600)

wd = os.getcwd()
myFile = open(wd + "/graphics/" + csvGraphic)

state = '1'

while True: # Exits when there is no more lines to read

  print "state", state

  if state == '0': # Wait for Arduino to be ready
    state = str(ser.read())

  elif state == '1': # Send one more line to Arduino
    line = myFile.readline()
    if not line:
      ser.close()
      break
    print line
    ser.write(line)
    state = '0' # Wait for Arduino before reading next line

ser.close()
myFile.close


Python输出如下所示。只需等待一次即可执行代码,而无需等待Arduino。似乎行state = str(ser.read())读取某种串行缓冲区中的数据。我猜解决方案是清除缓冲区。我就是不知道

state 1
239,275;

state 0
state 1
1100,275;

state 0
state 1
300,400;

state 0
state 1
200,400;

state 0
state 1
200,300;

state 0
state 1
[Finished in 0.1s]

最佳答案

想想我找到了。 SerialEvent()loop的开头被调用。它做的第一件事是write('1'),这意味着每次执行loop时,它都会告诉您的python代码它已准备好接受新指令(即使没有给出指令!),并用'1'上的很多填充缓冲区,您可以通过读取一

尝试这个:

void SerialEvent(){
    if((stepper1.distanceToGo() == 0) && (stepper2.distanceToGo() == 0)){
        Serial.write('1');
    }
    //Rest of the function


我也想在循环结束时

while((stepper1.distanceToGo() != 0) || (stepper2.distanceToGo() != 0))


代替while((stepper1.distanceToGo() != 0) && (stepper2.distanceToGo() != 0))

关于python - 如何使Python发送的数据(通过串行连接)等待Arduino完成当前任务?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33805986/

10-14 03:40