Reputation: 21
I'm trying to get the machine data from a CNC HAAS controller. It has a built-in software called MDC, which acts as a server. I need to write a client program to send a request to the MDC's IP and port number. when I send a request and receive it seems like the server is sending it one byte at a time and so I could capture only one byte at a time, others are lost. How to get the entire data. I'm using Python's socket module.
I've used a while loop, based on a previous question on Stack Overflow, but it seems like the server is sending the data and closing the connection and by the time my client program loops again, the other data is lost and the connection is closed.
# Import socket module
import socket
# Create a socket object
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Define the port on which you want to connect
port = 5051
# connect to the server on local computer
s.connect(('192.168.100.3', port))
#sending this message will give me the status of the CNC machine
s.send(("?Q500").encode())
d= (s.recv(1024)).decode()
print(d)
s.close()
The expected output is:
>PROGRAM, MDI, IDLE, PARTS, 380
The output I'm getting is >
, which is just the first character (byte) of the actual output.
Upvotes: 2
Views: 314
Reputation: 263
A bit more code would be helpful but i will try to hlp with what you gave us
you could try this
s.send(("?Q500").encode("utf-8")) # just add an encoding
fullData = ""
while True:
d = (s.recv(1024)).decode("utf-8")
fullData += d
if not d:
print(fullData)
s.close()
break
Upvotes: 1