Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:19:46+00:00 2026-06-07T21:19:46+00:00

When i assign this outside the class ser , how can i use the

  • 0

When i assign this outside the class ser, how can i use the ser from the class methods? Like following its not working in Window.

import socket, 
         datetime, 
           threading, 
             os,
               serial

# declare once 
ser = serial.Serial('COM3', 19200)

class AServer(threading.Thread):
  def __init__(self, port):
    threading.Thread.__init__(self)
    self.port = port

  def now(self):
    d = datetime.datetime.now()
    return d.strftime("%d/%m/%y %H:%M:%S")

  def run(self):
    host = ''
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, self.port))
    print 'successfully, waiting...'
    s.listen(1)
    conn, addr = s.accept()
    print 'contact', addr, 'on', self.now()

    while 1:
      try:
        data = conn.recv(1024)
      except socket.error:
        print 'lost', addr, 'waiting..'
        s.listen(1)
        conn, addr = s.accept()
        print 'contact', addr, 'on', self.now()
        continue

      if not data:
        print 'lost', addr, 'waiting..'
        s.listen(1)
        conn, addr = s.accept()
        print 'contact', addr, 'on', self.now()
      else:    
        print "received msg:", data
        conn.send('roger')
        if data.startswith('blablabla'):
          ser.write('\x02\x01\x80\x00\x03\x80')
        else:
          print "not ok"

t = AServer(80)
t.start()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:19:48+00:00Added an answer on June 7, 2026 at 9:19 pm

    This is to do with Python’s namespaces. You need to say that you want to access the global ser variable

    # declare once 
    ser = serial.Serial('COM3', 19200)
    
    class AServer(threading.Thread):
      def __init__(self, port):
        threading.Thread.__init__(self)
        self.port = port
    
        global se
        self._ser = ser # local reference to global variable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, you can assign an arbitrary attribute from outside the defining class :
Is this even possible? For example, let's say I have the following: class Window
I need to get UserCarId(int) from this query and assign to int type variable.
The usual way to assign color box functionality on a link is like this:
Given this XAML: <StackPanel Orientation=Horizontal Grid.Row=1 Grid.Column=0 Margin=5> How can I assign the Grid.Row
I writing an application where i use an array within a class. This array
I am using the directory class to get this information but unable to assign
Can you pass in a $this variable to use in a function in the
I would like to use a mix-in class to add properties to a model.
Is it possible to use widgets outside of the sidebar? When you assign them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.