| ApossC SDK V01.15

EthernetUDPSocket_OpenClient.mc
#include "..\..\..\SDK\SDK_ApossC.mc"
#include <SysDef.mh>
// Parameters for the example
#define SERVER_IP_ADDRESS 192.168.1.3 // IP address of the server socket.
#define SERVER_PORT_NUM 3000 // Port of the server socket.
#define CYCLE_TIME 200 // ms → 0 to disable
// Local functions
void EthernetSendData_sendCycle(void);
void FunctionEthernetHandler(void);
// global variables
long client_handle;
long status, status_old;
long main(void)
{
long ip = SERVER_IP_ADDRESS;
// All errors are cleared at the beginning of the program.
ErrorClear();
// opening an ethernet client in case of a failure the program will be terminated
print("EthernetOpenClient (UDP) to IP: ",((ip)&0xFF),".",((ip>>8)&0xFF),".",((ip>>16)&0xFF),".",((ip>>24)&0xFF)," and Port: ",SERVER_PORT_NUM);
client_handle = EthernetOpenClient(PROT_UDP, SERVER_IP_ADDRESS, SERVER_PORT_NUM); // ipAddress, portNumber);
ip = SYS_INFO(SYS_IP_ADDRESS_CONTROLLER);
print(" IP address of this client is: ",((ip)&0xFF),".",((ip>>8)&0xFF),".",((ip>>16)&0xFF),".",((ip>>24)&0xFF) );
print(" returned handler 'client_handle' : ",client_handle);
if(client_handle<0)
{
sdkEthernetPrintGeneralResult(client_handle);
print("Exit program");
Exit(0);
}
// Wating for connection
print("Wating for connection");
while(3 != status)
{
status = EthernetGetConnectionStatus(client_handle);
if(status != status_old)
{
sdkEthernetPrintConnectionStatus(status);
status_old=status;
Delay(1);
}
}
// Start interrupts
InterruptSetup(ETHERNET, FunctionEthernetHandler, client_handle);
InterruptSetup(PERIOD, EthernetSendData_sendCycle, CYCLE_TIME);
while (1) {
status = EthernetGetConnectionStatus(client_handle);
if(status != status_old)
{
sdkEthernetPrintConnectionStatus(status);
status_old=status;
Delay(1);
}
}
return(0);
}
void FunctionEthernetHandler(void)
{
long result = 0;
wchar receiveArray[50]="";
arrayset(receiveArray,0,50);
result = EthernetReceiveTelegram(client_handle, receiveArray);
print("we received a UDP Telegram at time: ", Time()," size: ",result," data: ",receiveArray);
return;
}
void EthernetSendData_sendCycle(void)
{
long result;
wchar sendArray[40] = "";
sprintf(sendArray, "UDP Client says Hello time: %d\n",Time());
result = EthernetSendTelegram(client_handle, sendArray, arraylen(sendArray));
print("SendCyclic time: ",Time());
if (result != 0) {
sdkEthernetPrintGeneralResult(result);
}
return;
}
sdkEthernetPrintConnectionStatus
long sdkEthernetPrintConnectionStatus(long status)
Definition: SDK_Communication_Ethernet.mc:50
sdkEthernetPrintGeneralResult
long sdkEthernetPrintGeneralResult(long result)
Definition: SDK_Communication_Ethernet.mc:89
SYS_IP_ADDRESS_CONTROLLER
#define SYS_IP_ADDRESS_CONTROLLER
IP address of controller.
Definition: SdoDictionary.mh:1283
SYS_INFO
#define SYS_INFO(parno)
System Information: Setter.
Definition: SdoDictionary.mh:1202

Data Sheets | Released Software | Software Manuals | Hardware Manuals | Maxon Shop

Maxon Support Center