【问题标题】:Need help running text file in Arduino需要帮助在 Arduino 中运行文本文件
【发布时间】:2018-05-25 11:40:56
【问题描述】:

我目前正在根据我发送到文本变量的数字在某些位置进行 Arduino 移动。我正在使用 Processing IDE 和 Arduino IDE。

我目前遇到的问题是 Arduino 根本不读取文本文件。相反,我必须自己手动输入数字。

总结一下,我正在尝试获取一个仅包含一个数字的文本文件。让处理应用程序读取它,然后让 Arduino 在某些位置移动。

这是我迄今为止尝试过的:

  1. 用switch删除if语句

  2. 将 myport ('0') 更改为 myport ('9'),因为那是我的电线连接的地方

  3. 尝试在 Arduino 上使用 ByteRead,但什么也没做

  4. 试图将 ByteRead 转换为 int 而不是字节

我在谷歌上查过,但运气不好,我能找到的唯一链接是这个http://arduinobasics.blogspot.com/2012/05/reading-from-text-file-and-sending-to.html

import processing.serial.*;
import java.io.*;

int counter=0;
String [] subtext;
Serial myPort;


void setup() {
  //Create a switch that will control the frequency of text file reads.
  //When mySwitch=1, the program is setup to read the text file.
  //This is turned off when mySwitch = 0

  //Open the serial port for communication with the Arduino
  //Make sure the COM port is correct
  myPort = new Serial(this, "COM3", 9600);
  myPort.bufferUntil('\n');
}

void draw() {
    /*The readData function can be found later in the code.
     This is the call to read a CSV file on the computer hard-drive. */
    readData("C://Users//InGodWeTrush//Desktop//maxColorIndex.txt");

    /*The following switch prevents continuous reading of the text file, until
     we are ready to read the file again. */

  /*Only send new data. This IF statement will allow new data to be sent to
   the arduino. */
  if (counter<subtext.length) {
    /* Write the next number to the Serial port and send it to the Arduino 
     There will be a delay of half a second before the command is
     sent to turn the LED off : myPort.write('0'); */
    myPort.write(subtext[counter]);
    delay(20000);
    myPort.write('0');
    delay(11000);
    //Increment the counter so that the next number is sent to the arduino.
    counter++;
  } else {
    //If the text file has run out of numbers, then read the text file again in 5 seconds.
    delay(20000);
  }
} 

/* The following function will read from a CSV or TXT file */
void readData(String myFileName) {
  File file=new File(myFileName);
  BufferedReader br=null;

  try {
    br=new BufferedReader(new FileReader(file));
    String text=null;

    /* keep reading each line until you get to the end of the file */
    while ((text=br.readLine())!=null) {
      /* Spilt each line up into bits and pieces using a comma as a separator */
      subtext = splitTokens(text, ",");
    }
  }
  catch(FileNotFoundException e) {
    e.printStackTrace();
  }
  catch(IOException e) {
    e.printStackTrace();
  }
  finally {
    try {
      if (br != null) {
        br.close();
      }
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

我的 Arduino 代码:

#include <Servo.h>
Servo servol;

void setup() {
  // initialize the digital pins as an output.
  servol.attach(9);
  // Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
  byte byteRead;
  /* check if data has been sent from the computer: */
  if (Serial.available()) {
    /* read the most recent byte */
    byteRead = Serial.read();
    //You have to subtract '0' from the read Byte to convert from text to a number.
    byteRead = byteRead - '0';

    //Turn off all LEDs if the byte Read = 0
    if (byteRead == 0) {
      servol.write(18);
      Serial.println("The color is Red");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 1) {
      servol.write(36);
      Serial.println("The color is Orange");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 2) {
      servol.write(54);
      Serial.println("The color is Green");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 3) {
      servol.write(72);
      Serial.println("The color is Blue");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 4) {
      servol.write(90);
      Serial.println("The color is Purple");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 5) {
      servol.write(108);
      Serial.println("The color is Pink");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 6) {
      servol.write(126);
      Serial.println("The color is Red");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 7) {
      servol.write(144);
      Serial.println("The color is Black");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 8) {
      servol.write(162);
      Serial.println("The color is Grey");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 9) {
      servol.write(180);
      Serial.println("The color is White");
      delay(10000);
      servol.write(1);
    } else {
      Serial.println("Error");
      delay(10000);
      servol.write(0);
    }
  }
}

【问题讨论】:

  • 您每 0.1/0.5 秒发送一次字节,但每 10 秒读取一次。
  • 感谢您的回复。我增加了时间,但它仍然不会读取我的文本文件。我注意到的是我按下运行的第一个(处理 IDE 或 Arduino)将起作用。如果我先运行我的 Arduino 代码,那么它会说 COM3 在处理 IDE 上很忙。如果我先在处理 IDE 上运行代码,那么我会在 Arduino 上收到一个错误,说上传到开发板时出现问题。

标签: arduino processing


【解决方案1】:

因此,您将 Arduino 连接到串行端口(或通过 USB-UART 转换器,没有问题),比如说 COM1,然后从一个程序(处理 IDE)向该端口发送一些数据,然后尝试从其他程序(Arduino IDE)?

不可能同时将多个程序连接到同一个 COM 端口。您从串口读取字节(我猜是从处理 IDE),然后通过串口发回文本(试图将其发送到 Arduino IDE?)。

你应该在PC端的同一个应用程序中做所有事情(写串口和读串口)(应该先连接到COM端口并保持它,防止其他程序访问它)

-或-

您应该使用连接到不同 COM 端口的两个串行端口(可能带有两个 USB-UART 转换器)。

注意 Arduino UNO、Nano 等带有 ATmega328 芯片的硬件只有一个串口。在这种情况下,您应该使用一些软件 UART 仿真来启动第二个串行端口。

最简单的选择是从 Arduino 代码中删除 Serial.println 并通过硬件(LED 等)在 Arduino 上进行指示,并且在工作期间不要将 Arduino IDE 连接到电路板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多