This script just opens a TCP server to listen for real-time position data from a FANUC robot to pass to another sensor software we are using (Luna Terahertz sensor).
So far everything is working great, I get an .xml file generated with the data from the FANUC with each datapoint getting an accurate timestamp.
However I don't know what I don't know and would just like some constructive feedback on the way I went about generating the xml file or any flaws you see in my overall formatting or commenting. One thing I'm also interested in is how to better format the incoming data using element
# import socket, datetime and time libraries
import socket
from datetime import datetime
import time
def main():
# Define TCP/IP server address and port
ip_address = '192.168.5.5'
port = 48200
# Initialize server socket and begin listening for data
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((ip_address, port))
server_socket.listen(1)
# Print message indicating server IP, port and listen status
print(f"Server listening on {ip_address}:{port}")
# Once a client attempts to send data, receive data, decode bytes into utf-8 string
#
# Lines 39-41 capture current timestamp values to be used in file generation
#
# Create xml file in append mode with title "<YearMonthDay>_TPCdata" in .xml format
#
# Write line with granular timestamp, down to microsecond in front of position data
#
# Write position data after timestamp and insert new line for formatting
#
# Once the data is received and written, close the client connection
# (CAUTION: not closing this leads to FANUC controller locking up)
while True:
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address[0]}:{client_address[1]}")
date_time = datetime.fromtimestamp(time.time())
timestr = date_time.strftime("%Y%m%d")
timestrgran = date_time.strftime("%H:%M:%S.%f")
data = client_socket.recv(1024)
if data:
xml_data = data.decode('utf-8')
with open(timestr+"_TPCdata.xml", mode='a', encoding ='utf-8') as xfile:
xfile.write(timestrgran)
xfile.write(" :: ")
xfile.write(xml_data)
xfile.write("\n")
xfile.close
print(f"Received data: {xml_data}")
client_socket.close()
if __name__ == "__main__":
main()
The results are in-line with my expectations, just want to know if I did this the most efficient way.
Here is an example of the data being stored in the xml file:
16:09:15.519923 :: <POS><usec>68360821</usec><G1><X>433.619476</X><Y>-801.910278</Y><Z>279.477539</Z><W>-177.122894</W><P>5.610122</P><R>-48.283722</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:09:21.093852 :: <POS><usec>73936913</usec><G1><X>433.619507</X><Y>-801.910278</Y><Z>279.477600</Z><W>-177.122879</W><P>5.610115</P><R>-48.283710</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:09:23.637498 :: <POS><usec>76480955</usec><G1><X>433.619476</X><Y>-801.910339</Y><Z>279.477600</Z><W>-177.122894</W><P>5.610116</P><R>-48.283707</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:09:28.926435 :: <POS><usec>81769041</usec><G1><X>433.619415</X><Y>-801.910217</Y><Z>279.477539</Z><W>-177.122879</W><P>5.610129</P><R>-48.283707</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:09:29.903188 :: <POS><usec>82745057</usec><G1><X>433.619476</X><Y>-801.910278</Y><Z>279.477539</Z><W>-177.122894</W><P>5.610122</P><R>-48.283722</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:10:22.648158 :: <POS><usec>6639596</usec><G1><X>406.542969</X><Y>-805.941467</Y><Z>240.309143</Z><W>-178.196320</W><P>3.798053</P><R>-48.379639</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>
16:10:51.550988 :: <POS><usec>35544047</usec><G1><X>395.019043</X><Y>-792.794189</Y><Z>225.621002</Z><W>-178.700150</W><P>1.136544</P><R>-44.721104</R></G1></POS><CYCLE>1</CYCLE><IO>1</IO>