Python Forum

Full Version: Receive Serial Data and store in different Variables in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am using a wireless modem which is connected with FTDI32 chip. The modem is receiving the values from Wireless Temperature sensor.
I am working on a project in which I want to receive the values of the wireless sensor which is transmitted to the wireless modem in my laptop and with the help of python code, I want to convert the data and display in my message box.

As in mentioned below manual, there are different registers which contain hexadecimal values and I need to store the register values in different variables using python

Any suggestion on this will be a great help.

https://ncd.io/long-range-iot-wireless-t...ct-manual/
What is your question?
Hi, Actually, I have created this code in which I am receiving the data from Arduino using serial communication in string format.

import serial
ard = serial.Serial('COM4', 9600);
while True:
  k = ard.readline().decode('ascii');
  print(k)


I want to store the information in 3 different python variables. Do I need to use a buffer to store the received data? how will the code be?

According to my search on the internet, the buffer is used to slice the string.
As beginner python world I need some suggestion which helps me to create the temperature monitoring system.
Will be looking forward to your advice on this.
It depends on what the contents of k are here. For example, you could do something like this
k = "1,2,3"
a, b, c = k.split()
Make sure to provide enough detail for us to help (e.g. what k looks like).
Thanks for the help, Actually I am receiving the data as mentioned below

hello\r\n
hello1\r\n
hello2\r\n

I just want to store the data in such a way that
>hello will be stored in variable a
>hello1 will be stored in variable b
>hello2 will be stored in variable c

and the \n\r will be minimized from the serial data
jenkins43 Wrote:>hello will be stored in variable a
Store it in a dictionary,that's what Python dos Think
If look closer at it when do a = 'hello',so do Python store this is in globals() dictionary.
>>> a = 'hello'

# how it look in globals 
'a': 'hello',

# Use this dictionary
>>> globals()['a']
'hello'
So this make a visible dictionary record.
import io

# Simulate serial data
ard = io.StringIO('''\
hello\r\n
hello1\r\n
hello2\r\n''')

record = {}
lst =  ['a', 'b', 'c']
while True:
    k = ard.readline()
    if k == '':
        break
    try:
        if k == '\n':
            pass
        else:
            record[lst.pop(0)] = k.strip('\r\n')
    except IndexError:
        pass 
Test:
>>> record
{'a': 'hello', 'b': 'hello1', 'c': 'hello2'}
>>> record['b']
'hello1'
>>> record['c']
'hello2'