如何将Pyserial读取数据导出到文本文件或Matlab中
我是Pyserial的新手,我目前正在使用它从微控制器读取大量数据,数据格式类似于(1,2,3,4,…),我想将这些数据保存到文本文件或Matlab中,我应该如何实现?谢谢 这里有一个链接,指向我创建的一个存储库,总结了实现这一点的各种方法: 描述如下如何将Pyserial读取数据导出到文本文件或Matlab中,matlab,pyserial,Matlab,Pyserial,我是Pyserial的新手,我目前正在使用它从微控制器读取大量数据,数据格式类似于(1,2,3,4,…),我想将这些数据保存到文本文件或Matlab中,我应该如何实现?谢谢 这里有一个链接,指向我创建的一个存储库,总结了实现这一点的各种方法: 描述如下 PySerial Pyserial是一种从Arduino获取数据的极好方法,它既健壮又易于实现 下面的示例展示了一种非常简单的方法,可以帮助您开始 但是,请注意arduino不会不断地向您的程序发送数据——您可能会遇到缓冲区填充过多的错误。
PySerial Pyserial是一种从Arduino获取数据的极好方法,它既健壮又易于实现 下面的示例展示了一种非常简单的方法,可以帮助您开始 但是,请注意arduino不会不断地向您的程序发送数据——您可能会遇到缓冲区填充过多的错误。 素描 #先试试这个,感受一下pyserial naive.py
import serial
ser = serial.Serial('/dev/ttyACM0',115200)
f = open('dataFile.txt','a')
while 1 :
f.write(ser.readline())
f.close()
f = open('dataFile.txt','a')
#!/usr/bin/python
import serial, time
ser = serial.Serial('/dev/ttyACM0', 115200, timeout = 0.1)
#if you only want to send data to arduino (i.e. a signal to move a servo)
def send( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
break
except:
pass
time.sleep(0.1)
#if you would like to tell the arduino that you would like to receive data from the arduino
def send_and_receive( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
state = ser.readline()
print state
return state
except:
pass
time.sleep(0.1)
f = open('dataFile.txt','a')
while 1 :
arduino_sensor = send_and_receive('1')
f.write(arduino_sensor)
f.close()
f = open('dataFile.txt','a')
天真无邪。ino
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
void setup () { pinMode(13, OUTPUT); Serial.begin(115200); }
void loop() {
if (Serial.available()) {
ch = Serial.read();
if ( ch == '1' ) {
Serial.println(analogRead(A0)); // if '1' is received, then send back analog read A0
}
else if (ch == '2') {
digitalWrite(13,HIGH); // if '2' is received, turn on the led attached to 13
}
else if (ch == '3') {
digitalWrite(13,LOW); // if '3' is received then turn off the led attached 13
} else {
delay(10);
}
}
}
健壮的Pyserial和Sketch
当您准备好时,考虑在您的Python程序提示时,ARDUIO只发送数据:
8robust.pyimport serial
ser = serial.Serial('/dev/ttyACM0',115200)
f = open('dataFile.txt','a')
while 1 :
f.write(ser.readline())
f.close()
f = open('dataFile.txt','a')
#!/usr/bin/python
import serial, time
ser = serial.Serial('/dev/ttyACM0', 115200, timeout = 0.1)
#if you only want to send data to arduino (i.e. a signal to move a servo)
def send( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
break
except:
pass
time.sleep(0.1)
#if you would like to tell the arduino that you would like to receive data from the arduino
def send_and_receive( theinput ):
ser.write( theinput )
while True:
try:
time.sleep(0.01)
state = ser.readline()
print state
return state
except:
pass
time.sleep(0.1)
f = open('dataFile.txt','a')
while 1 :
arduino_sensor = send_and_receive('1')
f.write(arduino_sensor)
f.close()
f = open('dataFile.txt','a')
robust.ino
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(analogRead(A0));
delay(1000);
}
void setup () { pinMode(13, OUTPUT); Serial.begin(115200); }
void loop() {
if (Serial.available()) {
ch = Serial.read();
if ( ch == '1' ) {
Serial.println(analogRead(A0)); // if '1' is received, then send back analog read A0
}
else if (ch == '2') {
digitalWrite(13,HIGH); // if '2' is received, turn on the led attached to 13
}
else if (ch == '3') {
digitalWrite(13,LOW); // if '3' is received then turn off the led attached 13
} else {
delay(10);
}
}
}
你应该发布你写的代码。