2

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.

Hasan
  • 157
  • 8
  • 1
    You should provide an **example** of your expected behaviour, since we can't read your mind. The propositions *<>* and *<< How can I differentiate all this data in different arrays>>* are arguably ambiguous. One could start from this `TPS = [] LOAD = [] ... TPS.append(data.split()[2]) LOAD.append(data.split()[5])` – Patrick Trentin Feb 01 '17 at 12:08
  • @PatrickTrentin Ok let me explain with another example. – Hasan Feb 01 '17 at 12:24
  • you changed a couple of times your output, as it is now you have just a stream of values.. you want them all in the same array? – Patrick Trentin Feb 01 '17 at 16:02
  • @PatrickTrentin I changed whole example which bit easy to understand. Yes, I want them all in same array or list in python language. So, after that, it is easy to me to separate data from that array. I hope you understand. – Hasan Feb 02 '17 at 06:22
  • fine then, the current answer should do it – Patrick Trentin Feb 02 '17 at 08:31
  • @PatrickTrentin Yes, it is. – Hasan Feb 02 '17 at 08:53

1 Answers1

3

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
Aniket Pawar
  • 2,641
  • 19
  • 32