Este bloque de funciones de ejecutar en tarea Atrás, gestiona la comunicación con el protocolo UDP en modo cliente. Es necesario definir en PeerAdd la dirección IP y en PeerPort el puerto UDP del sistema del servidor al que desea conectarse.
Activando el comando Connect la conexión con el sistema del servidor está abierta, en la conexión UDP no es posible verificar si el servidor está presente y acepta la conexión, por lo tanto, siempre está activado Connected y en la salida File se devuelve la secuencia que se utilizará para el intercambio de datos con el sistema del servidor.
In LocalAdd e LocalPort puede definir la dirección IP y el puerto de la interfaz de red desde la que conectarse. Si la conexión no es posible se genera Fault.
Función de bloqueo
CÓDIGOS: indisponible
Laboratorio lógico: eLLabXUnified12Lib
Descripción
Connect (BOOL) Comando de habilitación de la conexión.
PeerAdd (@STRING) Dirección IP del sistema del servidor al que conectarse.
PeerPort (UINT) Número de puerto UDP al que conectarse.
LocalAdd (@STRING) Dirección IP de la interfaz de red desde la que conectarse. Predeterminado '0.0.0.0': la interfaz se elige automáticamente en función de la IP a la que conectarse.
LocalPort UINT) Número de puerto UDP desde el que se inicia la conexión (0 elegido automáticamente).
FlushTm (UINT)) Tiempo de vaciado de datos (mS). Si no se cargan datos en la secuencia después del tiempo definido, los datos presentes se envían automáticamente.
LifeTm (UINT) Tiempo de vida del socket (S), si no se reciben ni envían datos después del tiempo definido, el socket se cierra automáticamente. Si se define el tiempo "0" el socket nunca se cierra.
RxSize (UINT) Tamaño del búfer de recepción de datos.
TxSize (UINT) Tamaño del búfer de transmisión de datos.
Connected (BOOL) Activo si se establece conexión con el servidor.
Fault (BOOL) Activo si error de gestión.
File (FILEP) Flujo de E/S valorado en la conexión establecida con el sistema servidor. Si la conexión no está activa, se devuelve NULL
Ejemplos
Cómo utilizar los ejemplos..
FBD_SysUDPClient, ST_SysUDPClient: En el ejemplo, se activa una conexión a un servidor UCP que escucha en el puerto 1000. Una vez realizada la conexión, los caracteres recibidos del servidor se devuelven en eco. El puerto local es elegido automáticamente por el sistema.
SyslogClient: Implementa un cliente Syslog con envío de mensajes a servidores.
Laboratorio lógico (Ptp116, FBD_SysUDPClient)
PROGRAM FBD_SysUDPClient
VAR
Fp : eFILEP; (* File pointer *)
UDPClient : SysUDPClient; (* UDP client *)
END_VAR
Laboratorio lógico (Ptp116, ST_SysUDPClient)
PROGRAM ST_SysUDPClient
VAR
i : UDINT; (* Auxiliary variable *)
Fp : eFILEP; (* File pointer *)
UDPClient : SysUDPClient; (* UDP client *)
END_VAR
// *****************************************************************************
// PROGRAM "FBD_SysUDPClient"
// *****************************************************************************
// A UDP client is instantiated, it connects to the UDP server IP and port
// defined. Data received are echoed.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// First program execution loop initializations.
IF (SysFirstLoop) THEN
UDPClient.PeerAdd:=ADR('192.168.0.77'); //Peer address
UDPClient.PeerPort:=1000; //Peer port
UDPClient.LocalAdd:=ADR('0.0.0.0'); //Local address
UDPClient.LocalPort:=0; //Local port
UDPClient.FlushTm:=50; //Flush time (mS)
UDPClient.LifeTm:=30; //Life time (S)
UDPClient.RxSize:=128; //Rx buffer size
UDPClient.TxSize:=128; //Tx buffer size
END_IF;
// Manage the UDP client.
UDPClient(Connect:=TRUE); //TCPClient management
Fp:=UDPClient.File; //File pointer
// -------------------------------------------------------------------------
// ECHO LOOP
// -------------------------------------------------------------------------
// Execute the echo loop.
IF (SysFIsOpen(Fp)) THEN
IF (TO_BOOL(SysFGetIChars(Fp)) AND TO_BOOL(SysFGetOSpace(Fp))) THEN
i:=Sysfputc(Sysfgetc(Fp), Fp); //Character echo
END_IF;
END_IF;
// [End of file]
LogicLab (SyslogClient
)
PROGRAM SyslogClient
VAR
Cmd : BOOL; (* Send command *)
i : UDINT; (* Auxiliary variable *)
Facility : USINT; (* Facility number *)
Severity : USINT; (* Severity number *)
CaseNr : USINT; (* Program case *)
Months : @STRING; (* Month definitions *)
UDPClient : SysUDPClient; (* UDP client *)
DateTime : SysETimeToDate; (* Date time conversion *)
END_VAR
// *****************************************************************************
// PROGRAM "SyslogClient"
// *****************************************************************************
// This program forward information as syslog messages to a syslog server.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// First program execution loop initializations.
IF (SysFirstLoop) THEN
UDPClient.PeerAdd:=ADR('192.168.0.184'); //Peer address
UDPClient.PeerPort:=514; //Peer port
UDPClient.LocalAdd:=ADR('0.0.0.0'); //Local address
UDPClient.LocalPort:=0; //Local port
UDPClient.FlushTm:=50; //Flush time (mS)
UDPClient.LifeTm:=30; //Life time (S)
UDPClient.RxSize:=128; //Rx buffer size
UDPClient.TxSize:=512; //Tx buffer size
END_IF;
// -------------------------------------------------------------------------
// SENDING SEQUENCES
// -------------------------------------------------------------------------
// Manages the UDP Client and sending sequences.
UDPClient(); //UDPClient management
CASE (CaseNr) OF
// ---------------------------------------------------------------------
// Wait command.
0:
UDPClient.Connect:=FALSE; //TCPClient management
IF NOT(Cmd) THEN RETURN; END_IF;
Cmd:=FALSE; //Send command
CaseNr:=CaseNr+1; //Program case
// ---------------------------------------------------------------------
// Connect to Syslog server.
1:
UDPClient.Connect:=TRUE; //TCPClient management
IF NOT(SysFIsOpen(UDPClient.File)) THEN RETURN; END_IF;
// ---------------------------------------------------------------------
// SEND THE MESSAGE TO SERVER
// ---------------------------------------------------------------------
// Send the message to server, here an message example.
// <19>Feb 26 17:36:42 SlimLine Prova messaggio
// ---------------------------------------------------------[PRI Part]--
// The Priority value is calculated by first multiplying the Facility
// number by 8 and then adding the numerical value of the Severity.
i:=(Facility*8)+Severity; //Priority value
i:=SysVfprintf(UDPClient.File, ADR('<%u>'), UDINT_TYPE, ADR(i));
// ------------------------------------------------------[HEADER Part]--
// Contains two fields called the TIMESTAMP and HOSTNAME.
Months:=ADR('JanFebMarAprMayJunJulAugSepOctNovDec');
DateTime(EpochTime:=SysDateTime); //Date time conversion
i:=SysVfprintf(UDPClient.File, ADR('%.3s '), STRING_TYPE, Months+(DateTime.Month*3));
i:=SysVfprintf(UDPClient.File, ADR('%2u '), USINT_TYPE, ADR(DateTime.Day));
i:=SysVfprintf(UDPClient.File, ADR('%02u:'), USINT_TYPE, ADR(DateTime.Hour));
i:=SysVfprintf(UDPClient.File, ADR('%02u:'), USINT_TYPE, ADR(DateTime.Minute));
i:=SysVfprintf(UDPClient.File, ADR('%02u '), USINT_TYPE, ADR(DateTime.Second));
i:=SysVfprintf(UDPClient.File, ADR('%s '), STRING_TYPE, ADR('SlimLine'));
// ---------------------------------------------------------[MSG Part]--
// The CONTENT contains the details of the message.
i:=SysVfprintf(UDPClient.File, ADR('%s'), STRING_TYPE, ADR('This is a message'));
// Eseguo invio messaggio di Log.
i:=SysFOBfFlush(UDPClient.File); //Flush output characters
CaseNr:=CaseNr+1; //Program case
// ---------------------------------------------------------------------
// Waits until the message is sent.
2:
IF (TO_INT(SysFGetOBfSize(UDPClient.File)) <> SysFGetOSpace(UDPClient.File)) THEN RETURN; END_IF;
CaseNr:=0; //Program case
END_CASE;
// [End of file]