| ApossC SDK V01.15

EthernetSocket_OpenServer.mc

Sends a string as ethernet telegram Simplified usage of the function EthernetSendTelegram() and can be used to send a string.

Parameters

  sendArray | Array which contains the data to be sent
  handle | The handle returned by the EthernetOpenClient() or EthernetOpenServer() command.

Returns
  value: See list in Ethernet Commands or use sdkEthernetPrintGeneralResult()
  value > 0 -
  value = 0 No error, everything OK
  value < 0 Error

#include "..\..\..\SDK\SDK_ApossC.mc"
#include <SysDef.mh>
// Parameters for the SDK function
#define MY_PORT_NUM 3000 // Define TCP Port number
#define CYCLE_TIME 1000 // ms → 0 to disable
// Local functions
void EthernetSendData_sendCycle(void);
void FunctionEthernetHandler(void);
// global variables
long server_handle;
long status,status_old;
long main(void)
{
// All errors are cleared at the beginning of the program.
ErrorClear();
print("EthernetOpenServer with port: ",MY_PORT_NUM);
// opening an ethernet server in case of a failure the program will be terminated
server_handle = EthernetOpenServer(PROT_TCP, MY_PORT_NUM);
print(" returned handler 'server_handle' : ",server_handle);
if(server_handle<0)
{
print("Exit program");
Exit(0);
}
// Wating for connection
print("Wating for connection");
while(3 != EthernetGetConnectionStatus(server_handle));
// Start ethernet interrupts
InterruptSetup(ETHERNET, FunctionEthernetHandler, server_handle);
InterruptSetup(PERIOD, EthernetSendData_sendCycle, CYCLE_TIME);
while (1)
{
status = EthernetGetConnectionStatus(server_handle);
if(status != status_old)
{
sdkEthernetPrintConnectionStatus(status);
status_old=status;
Delay(1);
}
}
return(0);
}
void FunctionEthernetHandler(void)
{
long result;
wchar receiveArray[50]="";
wchar sendResponse[]="sendResponse";
arrayset(receiveArray,0,result);
result = EthernetReceiveTelegram(server_handle, receiveArray);
print("\nwe received a Ethernet Telegram at time: ", Time());
print("num of received data: ",result);
print("receiveArray: ", receiveArray);
result = sdkEthernetSendString(sendResponse, server_handle);
sdkEthernetPrintGeneralResult(result);
return;
}
void EthernetSendData_sendCycle(void)
{
long result;
wchar sendArray[30] = "";
sprintf(sendArray, "EthernetSendData_sendCycle\n");
result = sdkEthernetSendString(sendArray, server_handle);
sdkEthernetPrintGeneralResult(result);
return;
}
sdkEthernetPrintConnectionStatus
long sdkEthernetPrintConnectionStatus(long status)
Definition: SDK_Communication_Ethernet.mc:50
sdkEthernetSendString
long sdkEthernetSendString(wchar sendArray[], long handle)
Definition: SDK_Communication_Ethernet.mc:25
sdkEthernetPrintGeneralResult
long sdkEthernetPrintGeneralResult(long result)
Definition: SDK_Communication_Ethernet.mc:89

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

Maxon Support Center