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


Messages In This Thread
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 310 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 601 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 973 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 902 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Badly defined python code? Ken76 2 631 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,019 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  Getting NameError for a function that is defined JonWayn 2 1,121 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,912 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,087 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,351 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