使用Python与LabVIEW Datasocket服务器中的不同对象进行通信

huangapple go评论88阅读模式
英文:

Communicating with different objects in LabVIEW Datasocket server with python

问题

I am trying to communicate with a LabVIEW datasocket server, which seems to be very different in structure to the python socket server than I have seen created. I want to be able to read/write certain objects, but not all. It seems that with my client code, it does connect to the server, but I am unable to read or write to it. It either is forced closed by the server if I am trying to read all data, or if I am trying to write to a specific object, python throws the error: struct.error: bad char in struct format or struct.error: required argument is not an integer if I use N or I in struct.unpack(). Any pointers would be helpful.

  1. import socket
  2. import json #converts data to byte streams
  3. import select
  4. import errno
  5. import numpy
  6. import struct
  7. def byteswap(i):
  8. return struct.unpack("!I", struct.pack("!I", i))[0]
  9. HEADER_LENGTH = 200
  10. IP = "localhost"
  11. PORT = 3015 #6341 #8089 #3015
  12. Engine_name = "ArrayTest"
  13. # Create a socket
  14. # socket.AF_INET - address family, IPv4, some other possible are AF_INET6, AF_BLUETOOTH, AF_UNIX
  15. # socket.SOCK_STREAM - TCP, connection-based, socket.SOCK_DGRAM - UDP, connectionless, datagrams, socket.SOCK_RAW - raw IP packets
  16. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. # Connect to a given ip and port
  18. client_socket.connect((IP, PORT))
  19. # Set connection to non-blocking state, so .recv() call won't block, just return some exception we'll handle
  20. client_socket.setblocking(True)
  21. while True:
  22. arr = [1,2,3,4,5,6]
  23. send_data = byteswap(json.dumps({"test_data":arr}))
  24. print(send_data)
  25. client_socket.send(send_data.encode())
英文:

I am trying to communicate with a LabVIEW datasocket server, which seems to be very different in structure to the python socket server than I have seen created. I want to be able to read/write certain objects, but not all. It seems that with my client code, it does connect to the server, but I am unable to read or write to it. It either is forced closed by the server if I am trying to read all data, or if I am trying to write to a specific object, python throws the error: struct.error: bad char in struct format or struct.error: required argument is not an integer if I use N or I in struct.unpack(). Any pointers would be helpful.

  1. import socket
  2. import json #converts data to byte streams
  3. import select
  4. import errno
  5. import numpy
  6. import struct
  7. def byteswap(i):
  8. return struct.unpack("!I", struct.pack("!I", i))[0]
  9. HEADER_LENGTH = 200
  10. IP = "localhost"
  11. PORT = 3015 #6341 #8089 #3015
  12. Engine_name = "ArrayTest"
  13. # Create a socket
  14. # socket.AF_INET - address family, IPv4, some otehr possible are AF_INET6, AF_BLUETOOTH, AF_UNIX
  15. # socket.SOCK_STREAM - TCP, conection-based, socket.SOCK_DGRAM - UDP, connectionless, datagrams, socket.SOCK_RAW - raw IP packets
  16. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. # Connect to a given ip and port
  18. client_socket.connect((IP, PORT))
  19. # Set connection to non-blocking state, so .recv() call won;t block, just return some exception we'll handle
  20. client_socket.setblocking(True)
  21. while True:
  22. arr = [1,2,3,4,5,6]
  23. send_data = byteswap(json.dumps({"test_data":arr}))
  24. print(send_data)
  25. client_socket.send(send_data.encode())

答案1

得分: 1

因为Labview套接字服务器具有一些未知的协议,所以我决定通过Labview利用TCP创建自己的服务器并使用JSON来提取数据。需要注意的是,直到Labview 2013才开始使用JSON,Python直到Labview 2018才被引入。大致构建的服务器代码如下。

  1. import socket
  2. import threading
  3. import json
  4. File_length = 4098 # 可以传递的最大字符数,需要与Labview保持一致
  5. PORT = 3015
  6. SERVER = socket.gethostbyname(socket.gethostname())
  7. ADDR = (SERVER, PORT)
  8. FORMAT = 'utf-8'
  9. DISCONNECT_MESSAGE = "!DISCONNECT"
  10. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. server.bind(ADDR)
  12. def handle_client(conn, addr):
  13. print(f"[NEW CONNECTION] {addr} connected.")
  14. Message_previous = ""
  15. connected = True
  16. while connected:
  17. Message = conn.recv(File_length).decode(FORMAT)
  18. Decoded_message = json.loads(Message)
  19. if Decoded_message != Message_previous:
  20. print(Decoded_message)
  21. Message_previous = Decoded_message
  22. String_arr = Decoded_message["Test Value Arr"]
  23. print(String_arr)
  24. print(String_arr[0])
  25. def start():
  26. server.listen()
  27. print(f"[LISTENING] Server is listening on {SERVER}")
  28. while True:
  29. conn, addr = server.accept()
  30. thread = threading.Thread(target=handle_client, args=(conn, addr))
  31. thread.start()
  32. return threading.active_count()
  33. # print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
  34. print("[STARTING] server is starting...")
  35. start()

希望其他人会发现这个初步设置有所帮助。

英文:

Because Labview socket server has some unknown protocols, I ended up deciding to create my own server utilizing TCP through Labview and stripping the data using JSON. It is important to note that JSON is not used until Labview 2013 and Python is not introduced until Labview 2018. The server code that is roughly built is as below.

  1. import socket
  2. import threading
  3. import json
  4. File_length = 4098 #maximum number of characters that can be passed back and forth. needs to be kept the same as labview
  5. PORT = 3015
  6. SERVER = socket.gethostbyname(socket.gethostname())
  7. ADDR = (SERVER, PORT)
  8. FORMAT = 'utf-8'
  9. DISCONNECT_MESSAGE = "!DISCONNECT"
  10. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. server.bind(ADDR)
  12. def handle_client(conn, addr):
  13. print(f"[NEW CONNECTION] {addr} connected.")
  14. Message_previous = ""
  15. connected = True
  16. while connected:
  17. Message = conn.recv(File_length).decode(FORMAT)
  18. Decoded_message = json.loads(Message)
  19. if Decoded_message != Message_previous:
  20. print(Decoded_message)
  21. Message_previous = Decoded_message
  22. String_arr = Decoded_message["Test Value Arr"]
  23. print(String_arr)
  24. print(String_arr[0])
  25. def start():
  26. server.listen()
  27. print(f"[LISTENING] Server is listening on {SERVER}")
  28. while True:
  29. conn, addr = server.accept()
  30. thread = threading.Thread(target=handle_client, args=(conn, addr))
  31. thread.start()
  32. return threading.active_count()
  33. #print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
  34. print("[STARTING] server is starting...")
  35. start()

I hope others find the rough setup helpful.

huangapple
  • 本文由 发表于 2023年2月18日 02:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定