Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Library scope
#1
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
Reply
#2
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)
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,755 Nov-07-2023, 09:49 AM
Last Post: buran
  Scope of variable confusion Mark17 10 2,867 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,548 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,067 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,825 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 3,442 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,531 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Solving a scope issue profconn1 4 2,608 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Namespace and scope difference Uchikago 9 4,626 Jul-03-2019, 03:36 PM
Last Post: Uchikago
  what is scope for python? samexpert 2 2,251 Jun-24-2019, 01:03 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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