Python Forum

Full Version: Library scope
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've written a program that uses the SERIAL library. The program seems to work just fine. I have a question about the scope of the methods, such as serial.read. In the main routine I can use serial. read, serial.write, serial.inWaiting etal with no problems. BUT if I try to use a subroutine like this
def CRLF ()
SERIAL.write(LineFeed.encode())

This does not work, yet in the main routine it does. This leads me to believe the scope is not there. How can I make this work? Thanks, Mike
What is the SERIAL library? Do you mean pySerial (imported as serial)?

I think you are mistaken. This is not a module/library scope issue. It is a class method/instance method issue.

pySerial.write() is an instance method, not a class method. Information saved in the instance object is needed to do the write(). You need to provide a pyserial instance object as the first argument when calling the method.
serial.write(serial_object, data_bytes)
Normally this is done using object.method(args) notation like this:
serial_object.write(data_bytes)
Yes, I mean pySerial. I'm new to Python and am ignorant of instance vs class. I've been reading as much as I can, but not what I need. Can you point me to more information that will help. I believe that I'm past the basics, loops, decisions, etal, but need more on items like this. Thanks, Mike