Interacting with UDP Simulation Software Using Your Own Code

Environment

I used Windows 10 x64 for this example, although the operating system should not make much difference. The programs are written in Python. I will use the two simulated devices from the first stage to demonstrate the exchange.

Start two instances of the simulator. In the first, select Device 1 Interface 0; in the second, select Device 2 Interface 0. Click Reload parameters in each window to populate its settings:

DoIFm9P6JYu34Uf.png

On both instances, click Connect link and then Test connectivity. I tried sending a few pieces of data between them and confirmed that the link worked:

oSmERIK8CkX2qvd.png

Understanding the ports

kJjOmx2SZfbdgvw.png

How they relate

Before writing the programs, it helps to understand which ports talk to which. Here is the analogy I used.

Think of the upper-layer port as a boss and the physical layer as its messenger. The boss hands over a message; the messenger does not interpret it, but carries it to its destination. The simulator’s remote port setting tells that messenger where to deliver it.

For example, suppose software 1’s upper layer sends 11111111 to software 2. It hands the data to its own physical-layer local port 11100. Since the remote port is set to 12100, that layer forwards the data to port 12100 on software 2’s physical layer.

When software 2’s physical layer receives the data, it passes the message up to its boss: upper-layer port 12400.

That completes the path from the first upper-layer port to the second.

What tells software 1 that the packet arrived? Nothing in this exchange: the simulator sends it over UDP, which does not itself confirm delivery.

Python implementation

With that path in mind, we can write two programs. Each binds to one upper-layer port; the connected simulated physical ports carry the data between them.

Software 1

1
2
3
4
5
6
7
8
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)#指定UDP传输模式
ipPort1 = ('127.0.0.1',11400)#绑定软件1的上层端口
sk.bind(ipPort1)
localIpPort1 = ('127.0.0.1',11100)
content = b'1111111111111111'
print('start to send...')
sk.sendto(content,localIpPort1)#给本地端口11100发送content的字符串

Software 2

1
2
3
4
5
6
7
8
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)#指定UDP传输模式
ipPort2 = ('127.0.0.1',12400)
localIpPort2 = ('127.0.0.1',12100)
sk.bind(ipPort2)
print('start to recv...')
hexdata = eval('0x'+bytes.hex(sk.recv(1024)))#接收到的字符串转化为二进制并打印
print(bin(hexdata)[2:])

Save the programs as 1.py and 2.py and run them in powershell. That is simply my preferred way to run socket programs; cmd or another environment works too. Here is the result:

k6wOoxjmD3TJsve.png

The two windows separately:

pueVnBQi9KhJqgD.png

cFZexh8m1A4vHJo.png

The output suggests that software 2 received the message from software 1. The simulator’s status information shows the exchange as well:

uIG9Jgn1tVPfObw.png

We have now sent a UDP packet through the simulator from our own code.

Next, let’s reverse the direction: software 2 will send and software 1 will receive. Since the bit-string test worked, I changed the Python scripts to try a text message.

Software 1

1
2
3
4
5
6
7
8
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ipPort1 = ('127.0.0.1',11400)
sk.bind(ipPort1)
localIpPort1 = ('127.0.0.1',11100)
print('start to recv...')
recvdata = sk.recv(1024).decode('utf-8')
print(recvdata)

Software 2

1
2
3
4
5
6
7
8
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
ipPort2 = ('127.0.0.1',12400)
localIpPort2 = ('127.0.0.1',12100)
sk.bind(ipPort2)
content = 'Deep dark fantasy'
print('start to send...')
sk.sendto(content.encode('utf-8'),localIpPort2)

Start 1.py in one powershell window so the receiver is waiting, then run 2.py in a second window:

zl8vbgMpoIU9fSu.png

The simulator’s port display looks like this:

aorvOmGubTknPeS.png

The receiver shows garbled characters because frame synchronization has not yet been performed. I will leave that topic for later.

There are other things to experiment with. For example, I later set software 1’s remote address directly to software 2’s upper-layer address; I will leave out the implementation details here.

in3aoC5GsKytSfd.png

That worked too. In the earlier analogy, software 1’s messenger delivered the message straight to software 2’s boss.

That wraps up the experiment! (●ˇ∀ˇ●)