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


Messages In This Thread
code not working, NameError: name 's' is not defined - by ridgerunnersjw - Oct-05-2020, 06:39 PM
RE: code not working..... - by Larz60+ - Oct-05-2020, 06:45 PM
RE: code not working..... - by Yoriz - Oct-05-2020, 06:45 PM
RE: code not working..... - by ridgerunnersjw - Oct-05-2020, 06:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 345 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 622 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 984 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 920 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Badly defined python code? Ken76 2 639 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 1,035 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Getting NameError for a function that is defined JonWayn 2 1,136 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,945 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,103 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,387 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