In the
As you are now using a class you need to use self.s
__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'@'))