Python Forum
code not working, NameError: name 's' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code not working, NameError: name 's' is not defined
#1
Good afternoon....
I have a serial device that I am able to communicate with. I decided to put the code into a class module and build it out. I am running into an issue with respect to the serial port....

Error:
>>> from radar import RD >>> m = RD('S06') >>> m.KLD2Setup() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/my_Python/Radar/radar.py", line 23, in KLD2Setup s.write(self.cmd.encode()) NameError: name 's' is not defined


My code:
#! /usr/bin/python

class RD():
	'''RFB K-LD2 module'''		
	def __init__(self, command):
		'''initialize'''
		self.cmd = command
		from serial import Serial
		s = Serial('/dev/ttyS0', 38400)

	def KLD2Setup(self):
		'''use this method for sending commands and receiving data'''
		self.cmd = '$' + self.cmd + '\r'
		if (self.cmd[1] in {"S","D","R","W","T"}):
			k = 8
		elif (self.cmd[1] in {"F","A"}):
			k = 10
		elif (self.cmd[1] == "C"):	
			k = input ("Enter the number of bytes? ")	
		else: 
			k = 0
		if (k):
			s.write(self.cmd.encode())
			print((s.read(int(k)).rstrip()).lstrip(b'@'))
This code block works if I invoke python create a serial port, etc. in a file without a class
Reply
#2
this is a scope issue,
change:
  • line 9: self.s = Serial('/dev/ttyS0', 38400)
  • line 23: self.s.write(self.cmd.encode())
Reply
#3
In the __init__ you create a local s and try to use a local s in the method KLD2Setup
As you are now using a class you need to use self.s
#! /usr/bin/python
from serial import Serial

class RD():
    '''RFB K-LD2 module'''      
    def __init__(self, command):
        '''initialize'''
        self.cmd = command
        self.s = Serial('/dev/ttyS0', 38400)
 
    def KLD2Setup(self):
        '''use this method for sending commands and receiving data'''
        self.cmd = '$' + self.cmd + '\r'
        if (self.cmd[1] in {"S","D","R","W","T"}):
            k = 8
        elif (self.cmd[1] in {"F","A"}):
            k = 10
        elif (self.cmd[1] == "C"):  
            k = input ("Enter the number of bytes? ")   
        else: 
            k = 0
        if (k):
            self.s.write(self.cmd.encode())
            print((self.s.read(int(k)).rstrip()).lstrip(b'@'))
Reply
#4
Thank you both very much...That worked great!
Reply
#5
just to mention that brackets in your if/elif/else conditions are redundant

if self.cmd[1] in {"S","D","R","W","T"}:
    k = 8
elif self.cmd[1] in {"F","A"}:
    k = 10
elif self.cmd[1] == "C":  
    k = input("Enter the number of bytes? ")   
else: 
    k = 0
if k:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Illegal instruction? working code for months? korenron 4 113 5 hours ago
Last Post: korenron
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 150 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I don't undestand why my code isn't working. RuyCab 2 247 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 396 Jun-14-2021, 03:41 PM
Last Post: deanhystad
  [split] NameError: name 'start_with_first_selected' is not defined knoxvilles_joker 4 600 Apr-18-2021, 07:59 PM
Last Post: knoxvilles_joker
  code is not working , can anybody help? RandomPerson69 4 574 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 761 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 2,029 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 585 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  NameError: name 'L2' is not defined soshelby 5 995 Nov-05-2020, 08:50 PM
Last Post: soshelby

Forum Jump:

User Panel Messages

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