Serial Communication and Array Division with Virgula

0

Hello, Good night, today I came up with another problem, reading data generated by arduino sensors, I was able to print them and save them in an array, but to really be 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 not create a .CSV file, to save it and then get it again just to convert to two array, I wanted to do it 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? 
    
asked by anonymous 24.03.2018 / 03:03

1 answer

1

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

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

Of course they will come as string , so you can create a function to take care of separating and converting values:

>>> def separa(v):
...     (v1, v2) = v.split(",")
...     return float(v1),float(v2)
... 
>>> (v1,v2) = separa(valor)
>>> v1
292.0
>>> v2
2436.0
    
24.03.2018 / 15:41