python - 如何使用 Pyserial 在 python 中创建一个类以及如何初始化它
问题描述
我想为具有 9PIN RS232 的温度室 VTM7004 创建一个类。该腔室有一个公引出线,只能访问 GND、TXD 和 RXD。
我从亚马逊买了一根电缆,并在所有 3 个引脚之间进行了 1:1 连接: https ://www.amazon.de/-/en/gp/product/B0758B874Y/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
这是我的代码:
import serial
import time
import string
class VTM():
def __init__(self):
if (self.ChamberVTM7004.isOpen() != True):
self.ChamberVTM7004 = serial.Serial(port='COM5',
baudrate=9600,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
parity=serial.PARITY_NONE,
timeout=5,
xonxoff=False,
rtscts=False,
dsrdtr=False)
else:
print(self.ChamberVTM7004.portstr + " is open ")
if __name__ == "__main__":
Test = VTM()
这是我得到的错误:
C:\Users\zbgmqg\Desktop\OnSemi20191125\venv\Scripts\python.exe C:/Users/zbgmqg/Desktop/OnSemi20191125/Automation/TemperatureChamber.py
Traceback (most recent call last):
File "C:/Users/zbgmqg/Desktop/OnSemi20191125/Automation/TemperatureChamber.py", line 106, in <module>
Test = VTM()
File "C:/Users/zbgmqg/Desktop/OnSemi20191125/Automation/TemperatureChamber.py", line 17, in __init__
if (self.ChamberVTM7004.isOpen() != True):
AttributeError: 'VTM' object has no attribute 'ChamberVTM7004'
Process finished with exit code 1
非常感谢任何帮助:)
解决方案
推荐阅读
- r - 嵌套不完美时在空间上连接嵌套的多边形
- swift - iOS RealityKit。更改实体的翻译会导致意外行为
- sql-server - 如何将表变量名映射到 tempdb 表名?
- devspace - 用于在容器更新后在容器中运行测试的 DevSpace 挂钩
- mysql - 无法远程连接到容器化 MySQL 数据库容器
- java - 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:索引超出范围
- wordpress - 网站被 Virgin Media 的“Web Safe”安全功能标记
- amazon-web-services - AWS IAM 拒绝除一个之外的所有存储桶的策略
- javascript - 从 SQL Server 数据库呈现的 HTML 未触发 ng-click 事件的问题
- scipy - 如何访问 scipy 频谱图的设置?