Jump to content

Is it possible to force UDPOpen() do use a certain source port to send data from?


rudi
 Share

Recommended Posts

Hello,

to communicate with a Siemens S7 PLC to Switch on / off some actors I'm doing my first steps with the UDP functions in Autoit.

The Task to accomplish:

  1. My Windows box Needs to send 10 Byte of data, currently only Byte 1 will be used:
    The packets have to be send using the UDP Source (PC) 10.27.10.3:2051, Destination (S7) 10.27.20.202:2050
    Byte 1 = 0x00000001 will Switch on an actor connected to the PLC
    Byte 1 = 0x00000002 will Switch off that actor again.
  2. These 10 Bytes will be send back right away by the PLC, once received and evaluated to be valid data
    (The Switch on / off Action inside the PLC can be assumed as successfull, there are other control mechanisms inside the PLC)

 

Siemens Simatic S7 PLC:

The PLC has  two built in "function blocks" (UDPSEND / UDPRCV) to communicate with *OTHER* S7 PLC Units.

UDPSEND:

  1. Destination IP Address = 10.27.10.3 (My Windows box)
  2. Destination Port = 2051 (My Windows box, no Problem with UDPBIND())
  3. Source Port = 2050 (The S7 PLC box)
  4. General Setting of the PLC: Own IP Address = 10.27.20.202/16

UPDRCV:

  1. Sender IP address, as above
  2. PLC listen port = 2051
  3. Senders source port = 2050 (My PC. No way to allow a dynamically choosen, free "high port")
  4. as above

 

Example with PLCs only:

PLC1 = 10.27.20.202
PLC2 = 10.27.20.203

PLC1

  • function "UDPSEND" will send UDP data from 10.27.20.202:2050 to 10.27.20.203:2051
  • function "UDPRCV" will listen for UDP data coming from 10.27.20.203:2050 to 10.27.20.202:2051 

PLC2

  • function "UDPSEND" will send UDP data from 10.27.20.203:2050 to 10.27.20.202:2051
  • function "UDPRCV" will listen for UDP data coming from 10.27.20.202:2050 to 10.27.20.203:2051 

 

My Problem is the UDP send direction:

It Looks like, that the function UDPopen($DestIP,$DestPort) is always using a random, free "high port" as it's source port?

For the UDP listener I can open the required listen port with UDPBind($MyIp,$ListenPort), and the PLC will correctly connect coming "from" UDP:2050.

But I also Need to send data to the PLC, and they *MUST* be send coming from UDP:2050
 

Any suggestions appreciated, TIA, Rudi.

 

S7-PLC-UDPSEND_UDPRCV.png

S7-UDP-Send-Receive-2-PC.png

Edited by rudi
typos

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

4 hours ago, rudi said:

It Looks like, that the function UDPopen($DestIP,$DestPort) is always using a random, free "high port" as it's source port?

What does $aArray[3] contain? Is it a random port or is it the one you set on UDPOpen?

https://www.autoitscript.com/autoit3/docs/functions/UDPOpen.htm

Here's a simple script to view the array...

#include 'array.au3'

UDPStartup()
Local $iPort = 2065
Local $sIPAddress = '192.168.1.20'
Local $aUDPInfo = UDPOpen($sIPAddress, $iPort)

_ArrayDisplay($aUDPInfo)
UDPShutdown()

 

BTW, there's an error in the example script on the MyUDP_Client() function.

Local $iSocket = UDPOpen($sIPAddress, $iPort)
Local $iError = 0 ;<----

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Hello,

 

thanks for your reply.

I'm using Wireshark to do LAN traces. There I can see, that using UDPOpen() will correctly connect to the destination port specified, but will use an always randomly different SourcePort, it's going to send its data from.

The socket Array does *NOT* hold the sending socket, that's going to be used when UDPopen() connects to some UDP Server.

 

One correction:

When I use in a Loop These three,  UDPopen(), send one packet, UDPCloseSocket(), I can see, that one SourcePort Number after another is used up to the Limit of 64k (65534). So it's *NOT* a random high port, but the next in a continuously row from 48k to 64k.

So it Looks like UDPOpen() respects a MinSocketNumber and a MaxSocketNumber.

If I could adjust both values to 2051, I would end up exactly with what I need.

(I know I will run into an error in case socket 2051 is "occupied" already)

 


Then it jumps back to 48k (49152) and continues to go up by one again:

 

cu, Rudi.

 

wirshark-screenshot.png.c280211f8ad3d44bab66e9c2d6d13b7e.png

 

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Hmmm. Well, I guess I'm not getting the full picture here.

I suppose my first question would be...

Does the PLC have the ability to manually change or stay static with port numbers?

It looks like Wireshark is showing a local system port number, from it's communications stack.

I don't think AutoIt UDP is the source of it. Your OS handles that locally.

As long as your AutoIt script opens on the port of your choosing, it should be right.

--edit--

Is there some way you can replicate your problem in a small reproducer script?

--edit2--

If what you're trying to do is force change a socket number, that's not going to happen.

The OS handles that automatically in the communications stack.

As far as I know, there's no way to change it manually.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

in your case u should change UDPopen() to UDPbind() , here is a working example:-
 

$DST_IP="10.27.20.202" ; PLC IP
$DST_Port=2051         ; PLC RX port
$SRC_IP="127.0.0.1"    ; WIN IP
$SRC_Port=2050         ; WIN port which u want it to be the src port 

$Receive_Socket=UDPBind($SRC_IP,$SRC_Port)
$Transmit_socket=$Receive_Socket
$Transmit_socket[2]=$DST_IP
$Transmit_socket[3]=$DST_Port

; For simple data transmit to PLC u just use this code :-
$mydata="hello !"
UDPSend($Transmit_socket,$mydata)

by the way u can use one port to send & receive at same time , that will reduce design complexity .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...