Este bloco de função de executar na tarefa Voltar, gerencia a comunicação com o protocolo UDP no modo cliente. É preciso definir em PeerAdd o endereço IP e em PeerPort a porta UDP do sistema do servidor ao qual você deseja se conectar.
Ativando o comando Connect a conexão com o sistema do servidor está aberta, na conexão UDP não é possível verificar se o servidor está presente e aceita a conexão, portanto, ele está sempre ativado Connected e na saída File o fluxo a ser usado para troca de dados com o sistema do servidor é retornado.
In LocalAdd e LocalPort você pode definir o endereço IP e a porta da interface de rede a partir da qual se conectar. Se a conexão não for possível, ela é gerada Fault.
Bloqueio de função
CoDeSys: Indisponível
Laboratório de lógica: eLLabXUnified12Lib
descrição
Connect (BOOL) Comando de habilitação de conexão.
PeerAdd (@STRING) Endereço IP do sistema do servidor ao qual se conectar.
PeerPort (UINT) Número da porta UDP à qual se conectar.
LocalAdd (@STRING) Endereço IP da interface de rede a partir da qual se conectar. Padrão '0.0.0.0': a interface é escolhida automaticamente com base no IP ao qual se conectar.
LocalPort UINT) Número da porta UDP a partir da qual a conexão inicia (0 escolhido automaticamente).
FlushTm (UINT)) Tempo de descarga de dados (mS). Se nenhum dado for carregado no stream após o tempo definido, os dados presentes são enviados automaticamente.
LifeTm (UINT) Tempo de vida do soquete (S), se nenhum dado for recebido ou enviado após o tempo definido, o soquete é fechado automaticamente. Se for definido o tempo "0" o soquete nunca é fechado.
RxSize (UINT) Tamanho do buffer de recepção de dados.
TxSize (UINT) Tamanho do buffer de transmissão de dados.
Connected (BOOL) Ativo se a conexão for estabelecida com o servidor.
Fault (BOOL) Ativo se houver erro de gerenciamento.
File (FILEP) Fluxo de E/S valorizado na conexão estabelecida com o sistema do servidor. Se a conexão não estiver ativa, NULL é retornado
Exemplos
Como usar os exemplos.
FBD_SysUDPClient, ST_SysUDPClient: No exemplo, é ativada uma conexão com um servidor UCP escutando na porta 1000. Uma vez estabelecida a conexão, os caracteres recebidos do servidor são enviados de volta em eco. A porta local é escolhida automaticamente pelo sistema.
SyslogClient: Implementa um cliente Syslog com envio de mensagens para servidores.
LogicLab (Ptp116, FBD_SysUDPClient)
PROGRAM FBD_SysUDPClient
VAR
Fp : eFILEP; (* File pointer *)
UDPClient : SysUDPClient; (* UDP client *)
END_VAR
LogicLab (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]