If "Send UDP Response back to Sender" is set to true, the values of "UDP response IP and Port" will be ignored. |
We've written a tool to help testing the UDP side of the protocol implementation. It can generate all packets for this protocol.
It's written in python for version 2.7, the name is prototester.py. Calling this script without arguments will display its usage.
python.exe prototester.py 127.0.0.1 15090 II =HBBBH 0xD3 0x00
Argument | Description |
---|---|
python.exe | The python executable. |
prototester.py | Our script |
127.0.0.1 | The host to send data to |
15090 | The port to send data to |
II | The format string used to format the outgoing data. Its 0xD3 and 0x00 both interpreted Int32, so the outgoing data will be 0xD3 00 00 00 ' 00 00 00 00. |
=HBBBH |
Format string used for the received data, in this example 5, 0, 1, 0, 0. The = is a prefix, that will set alignment to 1 byte, so uint8/uint16 don't eat more data than than their size. See https://docs.python.org/2/library/struct.html#struct-alignment The format string will cut the received bytes into the following output: 1x UInt16 (H) 3x Byte (BBB) 1x UInt16 (H) |
0xD3 0x00 | The data to be used with the send format string. It can be written as hex with "0x" prefix, or normal as decimal number, if a float is required it can be written with a dot (eg. 1.0) and it will be parsed as such. |
sending 8 bytes... d3 00 00 00 00 00 00 00 received 7 bytes. 05 00 00 01 00 00 00 data: (5, 0, 4, 0, 0)
Data | Description |
---|---|
d3 00 00 00 00 00 00 00 | The sent raw data. |
05 00 00 01 00 00 00 | The received raw data. |
(5, 0, 1, 0, 0) | The received data, formatted by the receive format string. |
Data | Description |
---|---|
05 00 | after this length field, 5 more bytes will follow |
00 | 0x00 Response status OK (Success) |
01 | Selected simulation: 0x01 Microsoft Flight Simulator X (Can be 0 to 4 depending on which simulation you have configured in connection settings) |
00 | Connection state: 0x00 Disconnected |
00 00 | Simulation update rate: 0 Updates per second. (Can be anything, undefined if not connected) |
#!/usr/bin/env python2 usagemsg = """ usage: prototester.py [-r] host port sendformat recvformat data0,... dataN -r - if this flag is set, send and receive will loop forever. host - the host to send the packet to. port - the port to send the packet to. sendformat - a string defining the format of the data to send. each char defines the type of 1 senddata argument. see python documentation for struct.pack for usable chars and their types. recvformat - same as sendformat, but parses the received data. data0-N - the data to be parsed and sent to the host example: prototester.py 127.0.0.1 15090 iiffff iiBBhBBhBBh 0xCC 0x11 1.0 0 0 0 explanation: send to 127.0.0.1:15090 2x a 4byte signed Integer and 4x a 4byte float. 0xCC and 0x11 are the two integers. decimal numbers can also be used as input. the 1.0 and the 3 zeroes are the floats. if no dot is used (eg 1.0) the input will be parsed as integer. the "BBhBBhBBh" part is used by struct.unpack to parse the incoming raw bytes. in this case 2 unsigned chars are followed by a signed short. this is repeated 3 times """ import socket import struct import sys import os def toNiceHex(binaryString): hexstr = str.encode(binaryString, "hex") niceHex = ' '.join([hexstr[i:i+2] for i in range(0, len(hexstr), 2)]) return niceHex def parseData(data): nr = data isHex = False val = None if data[:2] == "0x": isHex = True nr = nr[2:] if isHex: nr = int(nr, 16) if '.' in str(data): val = float(nr) else: val = int(nr) return val doLoop = False # parsing arguments args = sys.argv[1:] if len(args) < 5: print(usagemsg) exit() if args[0][0:2] == "-r": args = sys.argv[2:] doLoop = True host = args[0] port = int(args[1]) sendformat = args[2] recvformat = args[3] data = args[4:] inputs = [parseData(val) for val in data] if len(sendformat) != len(inputs): print("number of sendformat chars is different from the number of data arguments") exit() # preparing data for sending bytesToSend = struct.pack(sendformat, *inputs) niceHex = toNiceHex(bytesToSend) # prepare socket size = 8192 timeout = 8 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(timeout) sock.bind(('', 0)) while True: os.system("cls") print "sending {} bytes...".format(len(bytesToSend)) print niceHex #send and get response sock.sendto(bytesToSend, (host, port)) response, address = sock.recvfrom(8192) print "received {} bytes.".format(len(response)) print toNiceHex(response) # parse result result = struct.unpack(recvformat, response) print ("data: ") print(result) if not doLoop: break sock.close()
©2024 Brunner Elektronik AG
CH-8335 Hittnau 
http://www.brunner-innovation.swiss