Python没有作为pyserial类的主要运行

时间:2015-07-27 17:06:25

标签: python oop python-3.x pyserial

我已经创建了一个小类来在python上打开一个串口(ttyUSB0),但是当我尝试将它作为 main 运行时,它给了我很多东西我真的没有知道。

我想让类在运行它时创建一个串口实例,并让propper函数(或方法)让我回复我对classe实例的要求。相反,当我跑步时,我得到下面的输出:

$ python3  entrada.py

如何让它按照我的意图运行?

这是entrada.py的代码

import serial #for port opening
import sys #for exceptions

from collections import __main__
#

#configure the serial connections (the parameters differs on the device you are connecting to)

class Serializer: 
    def __init__(self, port, baudrate=9600, timeout=5): 
        self.port = serial.Serial(port = port, baudrate=baudrate, 
        timeout=timeout, writeTimeout=timeout)

    def open(self): 
        ''' Open the serial port.'''
        self.port.open()

    def close(self): 
        ''' Close the serial port.'''
        self.port.close() 

    def send(self, msg):
        self.prot.write(msg)

    def recv(self):
        return self.port.readline()

PORT = '/dev/ttyUSB0' #Esto puede necesitar cambiarse

def main():
    test_port = Serializer(port = PORT)
    try:
        test_port().open()
    except:
        print ("Could not open serial port: ", sys.exc_info()[0])
        sys.exit(2)

    while True:
        print(test_port.recv())

if __name__ == __main__:
    main()

这是输出:

from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 2:
            raise TypeError('Expected 2 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        'Return a new Point object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('x', 'y'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(x=%r, y=%r)' % self

    @property
    def __dict__(self):
        'A new OrderedDict mapping field names to their values'
        return OrderedDict(zip(self._fields, self))

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return self.__dict__

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    def __getstate__(self):
        'Exclude the OrderedDict from pickling'
        return None

    x = _property(_itemgetter(0), doc='Alias for field number 0')

    y = _property(_itemgetter(1), doc='Alias for field number 1')


Point: x= 3.000  y= 4.000  hypot= 5.000
Point: x=14.000  y= 0.714  hypot=14.018
Point(x=100, y=22)
Point3D(x, y, z)
TestResults(failed=0, attempted=66)

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

if __name__ == "__main__":
   do_it()

我很惊讶if __name__ == __main__:没有给你一个关于未定义__main__的错误

答案 1 :(得分:1)

仅供参考,您在$em->createQuery('...')->setMaxResult(5); self.prot.write(msg)

中拼错了端口

什么没有运行?它给出了什么错误?那输出怎么样?

你为什么要做self.port.write(msg)?一个简单的sys.exit(2)就足够了。

另外,将sys.exit()变量设为PORT可能会与main()函数中的PORT = '/dev/ttyUSB0'混淆。