本文介绍了如何将数据写入 Arduino 上的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some position data continually coming in and I am currently printing it to the serial.

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

Say I have the string "5" and want to print that to a text file, "myTextFile", what would I need to do to achieve this? To be clear, the text file would be saved on my computer not on an SD card on the Arduino.

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

Also, is their a way to create a text file within the program before I start saving to it?

推荐答案

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

You can create a python script to read the serial port and write the results into a text file:

##############
## 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);

这篇关于如何将数据写入 Arduino 上的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:09