#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0

    If dict key with string is sent via serial port,dict value and string should display.


    Hello every one,

    my.dict={
    'ns':"The next station is"
    }
    I wrote a code for serial communication and i had the above dictionary in my code. From one pc If I send a key called 'ns' through serial port to other pc. The dict value "The next station is" will display on my screen.
    My dought is can i send a string with the key through serial port, and at the other end, the sent string should display with the dict value.

    example:The train receives the next station name through GPS and displays the station name. lets say the station name is 'ABCD'. So if i send 'ns' 'ABCD' through serial port, it has to print as " The next station is ABCD."

    If 'ns' "ABCD" was sent, "The next station is ABCD" should print.
    So for every new station it sends 'ns' and new station name through serial port,and on the other pc it has to print as "The next station is" "new station name". sorry for for poor english. Please help me. Thank you.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Make an object that you construct with a list of stations.
    The object maintains an internal counter.
    The object has a method that reads the serial port.
    When it gets the message 'ns' it increments (or decrements, depending on the direction of the train) its counter and displays that item on the list. You'll have to figure out how to connect your serial port.
    Code:
    '''
        At shell prompt, commanding `python -m doctest thisfile.py' runs this doctest.
        >>> s = Station(('Chappaqua', 'Pleasantville', 'Hawthorne'))
        >>> print(s())
        Pleasantville
        >>> for i in range(8):
        ...     print(s())
        ... 
        Hawthorne
        Pleasantville
        Chappaqua
        Pleasantville
        Hawthorne
        Pleasantville
        Chappaqua
        Pleasantville
    '''
    
    class Station(list):
    
        def __init__(self, stations = [], location = 0, direction = 1):
    	super().__init__(stations) # works in python 3.  list.__init__(self, stations) # would also work.
    	self.location = location
    	self.direction = direction
    
        def __call__(self):
    	self.location += self.direction
    	location = self.location
    	if location == 0:
    	    self.direction =  1
    	elif location == len(self)-1:
    	    self.direction = -1
    	return self[location]
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Make an object that you construct with a list of stations.
    The object maintains an internal counter.
    The object has a method that reads the serial port.
    When it gets the message 'ns' it increments (or decrements, depending on the direction of the train) its counter and displays that item on the list. You'll have to figure out how to connect your serial port.
    Code:
    '''
        At shell prompt, commanding `python -m doctest thisfile.py' runs this doctest.
        >>> s = Station(('Chappaqua', 'Pleasantville', 'Hawthorne'))
        >>> print(s())
        Pleasantville
        >>> for i in range(8):
        ...     print(s())
        ... 
        Hawthorne
        Pleasantville
        Chappaqua
        Pleasantville
        Hawthorne
        Pleasantville
        Chappaqua
        Pleasantville
    '''
    
    class Station(list):
    
        def __init__(self, stations = [], location = 0, direction = 1):
    	super().__init__(stations) # works in python 3.  list.__init__(self, stations) # would also work.
    	self.location = location
    	self.direction = direction
    
        def __call__(self):
    	self.location += self.direction
    	location = self.location
    	if location == 0:
    	    self.direction =  1
    	elif location == len(self)-1:
    	    self.direction = -1
    	return self[location]
    I don't know what is the next station is, because when the train is moving, through Gps it receives the next station name, when the next station name receive. the pc sends 'ns' "next station name" to many other devices through serial port.
    So i want to know, when the dict key and the string(next station name) is sent, how the dict value is displayed with the received string(next station name).
    thank you
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Do you mean something like this ...
    Code:
    mydict = {
    'ns' : "The next station is "
    }
    
    # input received ...
    key = 'ns'
    next_station = 'abcd'
    
    # process it and print output ...
    print(mydict[key] + next_station)
    
    ''' result -->
    The next station is abcd
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo