在 Python 3 中通过 TCP 将 Str 发送到 Keyence 相机控制器

问题描述 投票:0回答:1

我需要将 ascii 字符串命令发送到 Keyence 相机。我编写了一个 python 3 代码来做出决定,但发送的命令并没有做出决定。

我需要发送“PW,001[CR]”,将 1 替换为 2、3 或 4 等,以选择 Keyence IV2 相机正在运行的程序。 使用 putty,我可以通过原始 TCP PW,001 发送并且它可以工作。
在 python 中,socket.send 希望我将其编码为字节类型对象。 Keyence 相机控制器对此不执行任何操作,如果我打印消息,它将包含 b'PW,001',我相信 Keyence 控制器不知道如何处理此信息。 Keyence 的 fieldnet pdf 显示它正在寻找 ascii 字符串。 Keyence 支持人员希望我用 C++ 重写代码。我不想仅仅因为相机无法配合而完全重写我的程序。我的程序执行配置文件解析,监视该配置文件的更新,并根据机器更新的配置文件决定将 keyence 相机设置为运行的程序。除了 tcp 套接字通信之外,所有文件均有效。 如果有一种方法可以通过 TCP 发送字符串而不将其编码为类似字节的对象,请告诉我。谢谢你

编辑我明白了

解决方案:以字节方式 ascii 发送命令。

这是为 Python 3.6 编写的,在 Windows 7 计算机上运行。大部分代码不包括在内,但 TCP 套接字和发送的消息包括在内。
在 python3 中,您不能发送原始字符串对象,它必须是字节对象。使用在线转换器将字符转换为 ascii,但对于 PR 之类的东西,您将得到 50 52。您不能只是以这种方式发送它以便控制器理解它。此外,[CR] 或结束字符不是您可以直接转换的内容。在现场手册中,您可以找到一个表,您可以使用该表直接查找ascii字符,包括现场手册中列出的[CR]。这个[CR]恰好是0D。要通过 TCP 发送此信息,您需要将其作为字节对象发送,其格式如下。

b”\x50\x52\x0D”

现场手册中程序切换命令的示例为 PW,001 [CR]。这是命令 Keyence 相机切换到程序 1。要在 python 中发送此命令,在创建并打开套接字后,您将使用如下所示的命令: 我使用 puppet 作为我的套接字的名称

Puppet.send(b”\x50\x57\x2C\x30\x30\x31\x0D”)

我在下面的代码示例中使用了一个变量来存储我的消息,但您可以直接发送字符串。前缀 b 用于 python 将字符串转换为字节对象。 \x 是 ascii 编码,紧随其后的十六进制两位数字是您要发送的特定字符的 ascii 代码。如果您通过原始 TCP 连接以 putty 之类的方式发送该字符串或原始命令,则不需要空格。 下面是我的代码的 TCP 发送部分。如果您想尝试使用它,只需添加一行来调用相机功能并向其发送一个数字,就像这样

camera(1)
这将调用相机功能并向其发送 1,这将向 Keyence 相机发送一条消息以进行更改编程 1. 如果您打算测试该功能,还可以更改 IP 地址以匹配您的 Keyence 相机控制器。
对于此类 TCP 命令,您需要正确设置 Keyence 控制器。要首次设置 IP 地址,请直接连接,进入高级设置 -> 更改网络设置,然后保存并关闭控制器电源,然后重新打开并通过网络连接进行连接。然后您可以设置正确的 TCP 设置。您需要将高级设置->实用程序->FieldNet/Comm 设置更改为非过程命令。在I/O设置->程序切换方式为Panel/PC/Network。

import socket  #import used for communications via TCP/IP for keyence camera
TCP_IP = '192.168.1.51' #IP address of Keyence controller
TCP_PORT = 8500  #port for communications to Keyence controller
# BUFFER_SIZE = 32  #Normally 1024, the response from the camera is not used in this example

def camera(i):  #camera function to send commands to Keyence camera controller
   
    if(i=='1'):  #if camera function was called with a value of 1 then send Keyence controller commands to choose program 1
        MESSAGE = b"\x50\x57\x2C\x30\x30\x31\x0D" #byte version or PW,001[CR]
    elif(i=='2'): #if camera function was called with a value of 2 then send Keyence controller commands to choose program 2
        MESSAGE = b"\x50\x57\x2C\x30\x30\x32\x0D" #byte version or PW,002[CR]
    elif(i=='3'): #if camera function was called with a value of 3 then send Keyence controller commands to choose program 3  
        MESSAGE = b"\x50\x57\x2C\x30\x30\x33\x0D" #byte version or PW,003[CR]
    elif(i=='4'): #if camera function was called with a value of 4 then send Keyence controller commands to choose program 4  
        MESSAGE = b"\x50\x57\x2C\x30\x30\x34\x0D" #byte version or PW,004[CR]
    elif(i=='5'): #if camera function was called with a value of 5 then send Keyence controller commands to choose program 5  
        MESSAGE = b"\x50\x57\x2C\x30\x30\x35\x0D" #byte version or PW,005[CR]
    elif(i=='6'): #if camera function was called with a value of 6 then send Keyence controller commands to choose program 6
        MESSAGE = b"\x50\x57\x2C\x30\x30\x36\x0D" #byte version or PW,006[CR]  
    Keyence(MESSAGE)

def Keyence(MES):  #TCP function to send to Keyence IV series camera controller.  Socket must be created each time after closing it
    puppet = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #create the socket and call it puppet
    puppet.connect((TCP_IP, TCP_PORT))  #connect to Keyence controller
    puppet.send(MES)  #send command
    #data = puppet.recv(BUFFER_SIZE)  #receive controller's return message, we are not doing anything with it right now
    puppet.close()  #close communication to Keyence camera controller
python-3.x sockets tcp
1个回答
0
投票

您需要将 CR 字节 b'0x0D' 替换为 b' '.

© www.soinside.com 2019 - 2024. All rights reserved.