0

I'm finding it really hard to phrase this question, mainly because I've found the problem confusing myself.

So I have an IMU stream running from my Arduino and I'm streaming it through pySerial. Basically it gives me an output like this:

** #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11**

which is a continuous stream until I choose to stop it. What I'm trying to do is be able to pick the values individually and compare it a set value that I have already identified

For example: Compare value #2 to a pre-defined integer b. I have not been able to figure out the code for that. Help would be appreciated. So far I have this simple code. It has to be in real-time.

import serial

ser = serial.Serial('COM11', baudrate = 115200, timeout = 1)

def getValues():

    arduinoData = ser.readline().decode('ascii')
    return arduinoData


while(1):
        print(getValues())

This is the output I get from getValues(). It keeps going until I stop it. a

Shayan Riyaz
  • 82
  • 11
  • each call to `getValues` outputs a single value? Add a sample and the desired output, because it's very ambiguous. – marcos Feb 26 '20 at 01:38
  • Hey Marcos, just updated it. – Shayan Riyaz Feb 26 '20 at 01:57
  • you should post code not images so people can reproduce it. – marcos Feb 26 '20 at 01:59
  • The code requires 6 IMU's to be connected to an Arduino. It'll be difficult for people to reproduce that. So I don't think it's applicable. I simply want to be able to store each of the string values in real-time and compare it to a different value. – Shayan Riyaz Feb 26 '20 at 02:03

2 Answers2

1

If getValues returns a string, you can store each value in a list for later analysis, for example:

getValues = lambda: '0, 1, 2, 3, 4' 

values = []
i = 2
while i:
    str_values = getValues()
    values += map(int, str_values.split(','))
    i -= 1

print(values)
>>> [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]

Now you can do comparisons with each values:

b = 2
if values[2] == b:
    print('values[2] equals to b')

>>> values[2] equals to b
marcos
  • 4,473
  • 1
  • 10
  • 24
0

I was able to figure out how to solve this issue, what I simply did was hold every value in my list into a variable. Since I wasn't planning on storing this data I simply overwrote on my declared variables and compared them with my predefined values in the loop.

Thank you for providing me with help.

Here's my code

import serial

while True:
    while(arduinoData.inWaiting()==0):
        pass # do nothing

    arduinoString = arduinoData.readline().decode('ascii')
    dataArray = arduinoString.split(',')
    length = len(dataArray)
    if length == 13:
        TP = float(dataArray[1])
        TR = float(dataArray[2])
        IP = float(dataArray[3])
        IR =  float(dataArray[4])
    else:
        time.sleep(n)

Shayan Riyaz
  • 82
  • 11