2

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.

1737973
  • 159
  • 18
  • 42

1 Answers1

1

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
gerard Garvey
  • 255
  • 3
  • 8
  • if you want to learn more i would highly recomend sentdex's socket tutorial – gerard Garvey Sep 07 '19 at 09:31
  • Thanks for the quick reply, but after entering the loop 2nd time it's stuck, i think it's not able to connect to the machine, bcz the server seems to have closed the connect and probably the remaining data is sent and lost. I don't how this might help, but i did the same thing with PUTTY software where i simply type ?Q500 and get the full message back in the terminal. i tried seeing the source code of putty , since i'm new to programming it's kinda mess for me. And i've entered the full program here. just connect, send and receive. That's it – Praveen Stein Sep 07 '19 at 09:38