如何将值存储在来自 arduino 的列表(python)中? [英] How to store value in list (python) which is coming from arduino serially?

查看:25
本文介绍了如何将值存储在来自 arduino 的列表(python)中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最近的项目中,我不得不将数据从 Arduino 传输到 python.并且已经完成了.

In my recent project, I hava to transfer data from Arduino to python. And it is already done.

这是我的 Arduino 代码:

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);
}

这是我的 Python 代码:

And it is my Python code :

import serial
arduino = serial.Serial('COM12', 9600, timeout = .1)
while True:
    data = arduino.readline()
    if data:
        print data

和输出(python shell):

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

这篇关于如何将值存储在来自 arduino 的列表(python)中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆