【问题标题】:How to write data to a text file on Arduino如何在 Arduino 上将数据写入文本文件
【发布时间】:2017-03-21 17:09:27
【问题描述】:

我有一些位置数据不断进入,我目前正在将其打印到串行。

假设我有字符串“5”并想将其打印到文本文件“myTextFile”,我需要做什么来实现这一点?需要明确的是,文本文件将保存在我的计算机上,而不是 Arduino 上的 SD 卡上。

另外,他们是一种在我开始保存之前在程序中创建文本文件的方法吗?

【问题讨论】:

  • 所以你有一些连接到 Arduino 的 SD 卡,你想在上面创建和写入文件?
  • 我想将文件写入到 arduino 连接的计算机上。现在要更清楚地说明问题,谢谢。
  • 您可以使用 Putty,连接 Arduino 并将其配置为写入输出。否则需要您自己的计算机端软件。
  • Windows 还是 Mac/Unix?

标签: string file text arduino creation


【解决方案1】:

你必须为此使用 serial-lib

Serial.begin(9600);

使用

将您的传感器值写入串行接口
Serial.println(value);

在你的循环方法中

在处理端使用PrintWriter将从串口读取的数据写入文件

import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
   mySerial = new Serial( this, Serial.list()[0], 9600 );
   output = createWriter( "data.txt" );
}
void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readString();
         if ( value != null ) {
              output.println( value );
         }
    }
}

void keyPressed() {
    output.flush();  // Writes the remaining data to the file
    output.close();  // Finishes the file
    exit();  // Stops the program
}

【讨论】:

  • 这是我想要的答案,但目前还不清楚。谁能阐明如何设置并解释这些功能中发生了什么?
  • 你如何在 Arduino 上使用 import
【解决方案2】:

您可以创建一个python脚本来读取串口并将结果写入文本文件:

##############
## Script listens to serial port and writes contents into a file
##############
## requires pySerial to be installed 
import serial  # sudo pip install pyserial should work

serial_port = '/dev/ttyACM0';
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
write_to_file_path = "output.txt";

output_file = open(write_to_file_path, "w+");
ser = serial.Serial(serial_port, baud_rate)
while True:
    line = ser.readline();
    line = line.decode("utf-8") #ser.readline returns a binary, convert to string
    print(line);
    output_file.write(line);

【讨论】:

  • 完美答案。只需要在 mac 上将 serial_port 更改为我的 /dev/cu.usb____
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多