1

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()
Daniel Li
  • 14,976
  • 6
  • 43
  • 60
  • you can always pass it in to the constructor, having global stuff hanging around is never a great idea ... python is nice for scripting, but the second you want to use it for more, you have to go back and clean that stuff up – pyInTheSky Jul 16 '12 at 19:22
  • What does "not working" mean? Are you getting an error message or some other indication that the global nature of the variable is the problem? – kindall Jul 16 '12 at 19:38
  • @kindall: No error, simply the trigger or com write fails. My robot left hand is not moving when i globally call it, but when i assign in the while 1 which is risky. there it works. My robot left and right arm moves. –  Jul 16 '12 at 19:45
  • OK. So why do you think the global variable is the problem? – kindall Jul 16 '12 at 19:46
  • @kindall: because its outside class scope and a thread do not access easyly in my java experience. –  Jul 16 '12 at 19:48
  • 1
    Well, if multiple threads were using the same serial port, that could get messy, but I don't see that. Experience with Java isn't really relevant here. – kindall Jul 16 '12 at 19:52

1 Answers1

1

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
Matti Lyra
  • 12,828
  • 8
  • 49
  • 67
  • 1
    -1. Accessing a global variable inside a method or function does not require declaring it global in that function. – kindall Jul 16 '12 at 19:38
  • @kindall: do you know why then they recommend `global`? http://stackoverflow.com/a/423596/285594 –  Jul 17 '12 at 06:34
  • `global` is needed to *assign* names in the global scope. – kindall Jul 17 '12 at 13:26