您可以创建一个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)
你必须为此使用串行库
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)