python-3.x - Python 3.X pycomm.ab_comm.clx CommError: must be str, not bytes
问题描述
我试图让“PYCOMM”连接到我的 CLX 5000 处理器。每次我运行我的代码时,我都会得到:CommError: must be str, not bytes。 我查看了所有代码,但找不到问题所在。应该是字符串格式的所有内容都是。我正在使用 python3.6 这是代码:
import sys
from pycomm.ab_comm.clx import Driver as ClxDriver
c = ClxDriver()
if c.open('172.16.2.161'):
print(c.read_tag('Start'))
# Prints (1,'BOOL') if true; (0,'BOOL') if false
c.close()
这是错误:
C:\Users\shirley\Miniconda3\python.exe C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py Traceback(最近一次调用最后):文件“C:\Users\shirley \Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 617, in build_header h += pack_uint(length) # Length UINT TypeError: must be str, not bytes
The header is 24 bytes fixed length, and includes the command and the length of the optional data portion.
:return: the headre
"""
try:
h = command # Command UINT
**h += pack_uint(length) # Length UINT**
h += pack_dint(self._session) # Session Handle UDINT
h += pack_dint(0) # Status UDINT
h += self.attribs['context'] # Sender Context 8 bytes
h += pack_dint(self.attribs['option']) # Option UDINT
return h
except Exception as e:
raise CommError(e)
在处理上述异常的过程中,又出现了一个异常:
回溯(最近一次通话最后):文件“C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py”,第 786 行,如果 self.register_session() 为无,则打开:文件“ C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py",第 635 行,在 register_session self._message = self.build_header(ENCAPSULATION_COMMAND['register_session'], 4) 文件“C: \Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py",第 624 行,在 build_header 中引发 CommError(e) pycomm.cip.cip_base.CommError: must be str, not bytes
在处理上述异常的过程中,又出现了一个异常:
回溯(最近一次通话最后一次):文件“C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py”,第 5 行,在 if c.open('172.16.2.161') 中:文件“C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py”,第 793 行,在 open raise CommError(e) pycomm.cip.cip_base.CommError: must be str, not bytes
进程以退出代码 1 结束
解决方案
我有同样的问题:
TypeError:必须是 str,而不是字节。
错误是因为您使用的是 Python 3.x,而我也做了同样的事情:我使用的是 Python 3.6 而不是 Python 2.6 或 2.7。
将 Python 3.x 更改为 2.6 或 2.7(我使用的是 2.7)。
推荐阅读
- python - Python Pandas 从一列和另一列中提取唯一值
- python - 如何使用 Selenium、Xpath 和预期条件按属性获取元素
- python - 替换 .data 文件中的字符串
- svelte - 在通过 prop 传递的 svelte 中管理切换状态的推荐方法是什么?
- sql-server - SQL Server中带有度数的VB.Net存储列
- machine-learning - 是否有用 Julia 编写的用于机器学习的本地库?
- wix - WiX:使用 .msp 补丁文件使用的更改更改 .msi 目录和 CustomAction 表
- javascript - 在javascript中,如何消除文档中的任何焦点?
- python - 如何使用 scipy 的 savemat 在单元格数组中创建 matlab 向量
- r - 是否可以在 R 中将方法名称作为包装函数参数传递?