在我最近的项目中,我将数据从Arduino传输到python。它已经完成了。
这是我的Arduino代码:
float TPS_MIN = 0.00;
float TPS_MAX = 5.00;
float MAP_MIN = 0.85;
float MAP_MAX = 1.90;
float LOAD_MIN_TPS = 2.00;
float LOAD_MAX_TPS = 10.00;
float LOAD_MIN_MAP = 9.69;
float LOAD_MAX_MAP = 82.18;
float m1, m2;
float y1, y2;
float TPS[] = {0, 0.4, 0.8, 1.2, 1.6, 2, 2.4, 2.8, 3.2, 3.6, 4, 4.4, 4.8, 5};
float MAP[] = {0.85, 0.95, 1.05, 1.15, 1.25, 1.35, 1.45, 1.55, 1.65, 1.75, 1.85, 1.9};
int i;
int j;
void setup()
{
Serial.begin(9600);
}
void loop()
{
m1 = (LOAD_MAX_TPS - LOAD_MIN_TPS) / (TPS_MAX - TPS_MIN);
for(i = 0; i < 14; i++)
{
y1 = m1 * (TPS[i] - TPS_MIN) + LOAD_MIN_TPS;
Serial.println(y1);
}
m2 = (LOAD_MAX_MAP - LOAD_MIN_MAP) / (MAP_MAX - MAP_MIN);
for(j = 0; j < 12; j++)
{
y2 = m2 * (MAP[j] - MAP_MIN) + LOAD_MIN_MAP;
Serial.println(y2);
}
delay(10000000);
}
这是我的Python代码:
import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
while True:
data = arduino.readline()
if data:
print data
输出(python shell):
2.00
2.64
3.28
3.92
4.56
5.20
5.84
6.48
7.12
7.76
8.40
9.04
9.68
10.00
9.69
16.59
23.50
30.40
37.31
44.21
51.11
58.02
64.92
71.82
78.73
82.18
现在,我想将此值存储在列表中。我不知道它是怎么做的。提出一些建议。
答案 0 :(得分:3)
您可能想要这样,但由于您运行的是无限循环,因此您将连续更改列表并附加数据。
import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
arduino_data = [] # declare a list
while True:
data = arduino.readline()
if data:
arduino_data.append(data) # Append a data to your declared list
print arduino_data