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
#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


Messages In This Thread
RE: Receive Serial Data and store in different Variables in Python - by snippsat - Dec-28-2018, 01:33 PM

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 389 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Receive Input on Same Line? johnywhy 8 815 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Create X Number of Variables and Assign Data RockBlok 8 1,032 Nov-14-2023, 08:46 AM
Last Post: perfringo
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,627 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,359 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 860 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,226 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,312 Mar-06-2022, 08:51 PM
Last Post: GiggsB
  How to receive webcam capture on spout? buzzdarkyear 2 2,698 Jan-12-2022, 02:26 PM
Last Post: buzzdarkyear
  python serial port barryjo 2 1,696 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