Programming - First steps
P.
71 of 349
TCP/IP server and client
Same syntax, only „tcp“ instead of
„udp“
readtcp fills its arguments with data
Event occurred?
More details
If your Enertex® EibPC is equipped with software option NP, it basically works as a TCP/IP server at
port 4809. Likewise, the Enertex® EibPC also provides a TCP/IP client.
The syntax for the implementation of a TCP/IP communication is exactly that of a UDP
communication. Thus we can at this point simply refer to the preceding pages. The advantage for the
user is that a TCP connection can be established by the same method. Therefore, code lines are
simply to be re-used.
The overview in short form:
sendtcp(
port {data type u16},IP address {data type u32},
arbitrary number and data types of arguments
)
sendtcp
expects a variable or an expression of data type u16 as the first argument. This argument
indicates the port number a TCP/IP telegram shall be sent to. The IP address (second argument) has
to be entered in the usual notation and is internally compatible with data type u32. The actual data
start with the 3rd argument. They are arbitrary in number and data type.
The Enertex
®
EibPC always sends from port 4809. This port cannot be altered. For transmission, one specifies the
receiver's port.
The counterpart of sending a TCP telegram is its reception. For this, the function
readtcp
is provided.
readtcp(
port {data type u16},IP address {data type u32}, arbitrary arguments
)
Here also applies: The first argument is a variable or an expression of data type u16 and provides
the port of the transmission a TCP telegram has been received by. The IP address (second
argument) is of data type u32. The actual “user data” start with the 3rd argument. They are arbitrary
in number and data type. Again holds: If your LAN device can be addressed via DNS and a name,
you can use the function
resolve
also here.
readtcp
looks into the input buffer of the Enertex
®
EibPC for a received TCP telegram. If a TCP
telegram is available, the arguments of the function, but in any case
port
and
IP address
, are “filled”
with data until the amount of received data (incoming TCP telegram) conforms to the data length of
the arguments of the function
readtcp
.
To check if a message has been received, you can, like with the UDP telegrams, resort to
event
(
readtcp
()).
if event(readtcp(Port,IP,StringPlayer)) and (Port==808u16) and (IP==192.168.22.21) then \\
Client and server
The Enertex® EibPC establishes connections via
sendtcp
and
readtcp
both as a client and as a
server.
If you operate the Enertex® EibPC as a server, a client has to register with the Enertex® EibPC. This
is performed automatically by the operating system without your interaction, because the Enertex®
EibPC is always in reception mode.
Whenever the client sends a message to the Enertex® EibPC,
event
(
readtcp
()) becomes active. In
the program, one has only to “look” if the IP is that of a desired peer, and then process the data
accordingly. After 30 seconds of inactivity of an existing connection, the Enertex® EibPC
disconnects automatically.
To operate the Enertex® EibPC as a client, it has to register with the server by means of
connecttcp(
port {data type u16}, address {data type u32}
)
.
In order to disconnect duly, one needs:
closetcp(
port {data type u16}, address {data type u32}
)
.
www.enertex.de/downloads/d-eibpc/DokuCF-1.pdf
you will find a detailed example of the
implementation of a more complex command processing as realized for Command Fusion.
HandbuchEibPC_USA-30.odt, 2017-05-11
Enertex
®
Bayern GmbH - Erlachstraße 13 - 91301 Forchheim - [email protected]