Serial Communication and Array Division with Comma

Asked

Viewed 146 times

0

Hello, Good Night, today I came up with another problem, reading data generated by sensors of the Arduino, I was able to print them and save them in an array, but to really get good, I need to break the two separate columns, as in the example

292.00,2436.00

292.00,2467.00

293.00,2498.00

But I can’t create a file. CSV, to save and then take it again just to convert into two array, wanted to do this in the python code itself

import serial 
import time
import numpy as np

entrada = serial.Serial('COM4', 4800)

tempo_leitura = time.time() + 5 # Contador de 5 segundos
geral = 0

def leituraSerial():
    while(time.time() < tempo_leitura):
        np.geral = entrada.readline().decode()
        print(np.geral)
        time.sleep(0.01)


def conversorSerial():
    #único jeito que sei fazer é a partir de loadtxt(), que não creio ser o caso, tentei diversas maneiras, porém não nenhuma obteve sucesso. Outra dúvida é o retorno do .decode() é uma string correto? 
  • How so break the two separate columns? The values come together by serial and you need to separate them?

1 answer

1

Use the method split() to break the string that you receive by serial and use the return to feed the variables:

>>> valor="292.00,2436.00"
>>> (v1, v2) = valor.split(",")
>>> v1
>>> '292.00'

Of course they’ll come as string, then you can create a function to take care to separate and convert the values:

>>> def separa(v):
...     (v1, v2) = v.split(",")
...     return float(v1),float(v2)
... 
>>> (v1,v2) = separa(valor)
>>> v1
292.0
>>> v2
2436.0
  • Your solution seems to solve the problem, but I noticed in tests I did, that np.general does not receive all the readings, only the last one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.