首页 > 解决方案 > 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 结束

标签: python-3.xplccomm

解决方案


我有同样的问题:

TypeError:必须是 str,而不是字节。


错误是因为您使用的是 Python 3.x,而我也做了同样的事情:我使用的是 Python 3.6 而不是 Python 2.6 或 2.7。

将 Python 3.x 更改为 2.6 或 2.7(我使用的是 2.7)。


推荐阅读