如何将数据写入Arduino上的文本文件

Mic*_*aie 6 string text file arduino creation

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

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

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

Ula*_*ach 5

您可以创建一个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);
Run Code Online (Sandbox Code Playgroud)


Ara*_*lai 0

你必须为此使用串行库

Serial.begin(9600);
Run Code Online (Sandbox Code Playgroud)

使用以下命令将传感器值写入串行接口

Serial.println(value);
Run Code Online (Sandbox Code Playgroud)

在你的循环方法中

在处理端使用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
}
Run Code Online (Sandbox Code Playgroud)