我有一个伺服电机通过以太网连接到我的计算机。我正试图用Python编写一个TCP客户机来连接它,并通过字符串发送命令。例如,发送字符串“?90.1”应提示设备返回参数数组。我目前的问题是,我不确定我是否正在建立连接,或者设备是否正在接收任何消息。我已经验证了正确的IP地址和端口
这是我目前的代码:
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('192.168.12.61', 10001)
print("Connecting to {}".format(server_address))
sock.connect(server_address)
try:
# Initial test to see if connection has been made. Will send ?90.1
message = str.encode("?90.1")
print("Sending message {}".format(message))
sock.sendall(message)
# Look for response. Expected response is K-parameters
data = sock.recv(32)
print("Received {}".format(data.decode()))
finally:
print("Closing socket")
sock.close()
以下是输出:
(CMController) C:\Users\Andy\PycharmProjects\CMController>python CMController.py
Connecting to ('192.168.12.61', 10001)
Sending message b'?90.1'
Received ?90.1
Closing socket
目前没有回答
相关问题 更多 >
编程相关推荐