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
  I'm getting a NameError: ...not defined. vonArre 2 152 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 907 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 834 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Badly defined python code? Ken76 2 569 May-25-2023, 11:05 PM
Last Post: DigiGod
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 970 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,772 Nov-11-2022, 09:03 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,025 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,198 Sep-01-2022, 07:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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