Pyserial Error with __init__
I'm trying to create a module that initializes a serial port connection using python:
import serial
class myserial:
def __init__(self, port, baudrate)
self = serial.Serial(port, b开发者_StackOverflowaudrate)
When I run this in Python I get an AttributeError message stating that self does not have an attribute open. Does anyone have any idea what's wrong with this code above? Any help would be greatly appreciated.
thanks
What are you trying to do, assigning to self
? See Why is `self` in Python objects immutable? for an answer that discusses this.
Perhaps you want to elaborate more on what you are actually trying to do.
You should be setting the Serial object to a member of self, rather than directly to self
Something like this:
class myserial:
def __init__(self, port, baudrate):
self.ser = serial.Serial(port, baudrate)
HTH
精彩评论