如何覆盖 PySerial 的“__del__” Python 中的方法?

发布于 2024-11-03 09:36:37 字数 677 浏览 1 评论 0原文

我设置了一个简单的 Python 脚本来将字符发送到 COM3 上的设备。 (这是在Windows XP上运行的) 设备接收字符很好,但是当脚本结束时,或者我调用

ser.close()      #ser is an instance of the `serial` method `Serial`

它将串行端口上的 DTR 线设置为高,重置我的设备。

对此感到困惑,我查阅了 PySerial 文档 (http://pyserial.sourceforge.net/) 并发现Module有一个__del__方法,该方法在删除Serial实例时调用ser.close()函数。

有没有办法重写这个 __del__ 方法?

(我不想将设备的重置线与 DTR 线断开)

下面是我正在使用的基本代码:

import serial
ser = serial.Serial()
ser.port = 2      #actually COM3
ser.baudrate = 9600
ser.open()
ser.write("Hello")
ser.close()

I have a simple Python script set up to send Characters to a device on COM3.
(This is running on Windows XP)
The device receives the Characters fine, but when the script ends, or I call

ser.close()      #ser is an instance of the `serial` method `Serial`

It sets the DTR Line on the serial port High, Resetting my Device.

Perplexed by this, I poked around in the PySerial Documentation (http://pyserial.sourceforge.net/) and found that the Module has a __del__ method, which calls the ser.close() function when the Serial instance is deleted.

Is there anyway to Override this __del__ method?

(I would prefer to not have to disconnect the Reset Line of my device from the DTR line)

Below is the basic code I am using:

import serial
ser = serial.Serial()
ser.port = 2      #actually COM3
ser.baudrate = 9600
ser.open()
ser.write("Hello")
ser.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-11-10 09:36:37

当然,继续并覆盖它。您可以创建 Serial 的子类并指定一个不执行任何操作的 __del__() 方法并使用该子类而不是 Serial,或者删除该子类使用 del 语句从实际的 Serial 类(猴子修补)中获取方法。前一种方法是首选,但如果您无法控制使用串行端口的代码,则可能需要第二种方法。当然,请确保 __del__() 方法在覆盖它之前不会执行任何其他重要操作。

子类化:

import serial

class mySerial(serial.Serial):
    def __del__(self):
        pass

ser = mySerial()
# rest of script the same

Monkey-patching:

import serial

del serial.Serial.__del__

如果您有此模块的 Python 源代码(即它不是用 C 编写的),第三种方法是创建您自己的副本,然后对其进行编辑而不这样做。 (您也可以对 C 模块执行此操作,但如果您没有 C 专业知识,则运行它会有点困难。)

当然,更改 __del__() 方法以不调用 ser.close() 对您给出的代码没有多大帮助,因为您自己调用 ser.close() 。 :-)

Sure, go ahead and override it. You can either create a subclass of Serial and specify a do-nothing __del__() method and use the subclass instead of Serial, or delete that method from the actual Serial class (monkey-patching) using the del statement. The former method is preferred but the second may be necessary if code you don't have any control over is using the serial port. Be sure the __del__() method doesn't do anything else important before overriding it, of course.

Subclassing:

import serial

class mySerial(serial.Serial):
    def __del__(self):
        pass

ser = mySerial()
# rest of script the same

Monkey-patching:

import serial

del serial.Serial.__del__

If you have Python source for this module (i.e. it is not written in C), a third approach would be to create your own copy of it and just edit it not to do that. (You can also do this for C modules, but getting it running is a bit more difficult if you don't have C expertise.)

Of course, changing the __del__() method to not call ser.close() isn't going to help much in the code you give, since you're calling ser.close() yourself. :-)

玩物 2024-11-10 09:36:37

你尝试过这样的事情吗?

MySerial = serial.Serial

def dummy_del(obj):
    pass

MySerial.__del__ = dummy_del

ser = MySerial()

Have you tried something like this?

MySerial = serial.Serial

def dummy_del(obj):
    pass

MySerial.__del__ = dummy_del

ser = MySerial()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文