In my recent project, I hava to transfer data from Arduino to python. And it is already done.
Here, it my Arduino code:
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);
}
And it is my Python code :
import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
while True:
data = arduino.readline()
if data:
print data
And output (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
Now, I want to store this value in list. I don't know how it done. Give some suggestions.
You probably want like this, but since you are running an infinite loop you will have continuous changing list with data appended.
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
来源:https://stackoverflow.com/questions/41979340/how-to-store-value-in-list-python-which-is-coming-from-arduino-serially