Python Forum
Receive Serial Data and store in different Variables in Python
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receive Serial Data and store in different Variables in Python
#1
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/
Reply
#2
What is your question?
Reply
#3
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.
Reply
#4
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).
Reply
#5
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
Reply
#6
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 278 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Receive Input on Same Line? johnywhy 8 609 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Create X Number of Variables and Assign Data RockBlok 8 872 Nov-14-2023, 08:46 AM
Last Post: perfringo
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,321 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  How to store columns of a .csv in variables (when the .csv has no headers) ? hobbyist 6 1,149 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 781 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,017 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  How can I add an end line character after every value that I receive? GiggsB 11 5,021 Mar-06-2022, 08:51 PM
Last Post: GiggsB
  How to receive webcam capture on spout? buzzdarkyear 2 2,593 Jan-12-2022, 02:26 PM
Last Post: buzzdarkyear
  python serial port barryjo 2 1,601 Dec-27-2021, 11:09 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020