如何在Arduino上将数据写入文本文件 [英] How to write data to a text file on Arduino

查看:1490
本文介绍了如何在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上将数据写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆