开发者

Pyserial Error with __init__

开发者 https://www.devze.com 2023-03-08 05:20 出处:网络
I\'m trying to create a module that initializes a serial port connection using python: import serial class myserial:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消