MX Foundation 4
ar429_UDPCommTX.c
/*******************************************************************************
//
// File:
// ar429_UDPCommTX.c
//
// Copyright (c) MAX Technologies Inc. 1988-2017, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// The embedded program "ar429_UDPCommRxEmbedded.c" is required by this program
// and compiled executable (ar429_UDPCommRxEmbedded.mxf) must have been transferred
// (by tftp or any mean) on the FM in the maxfiles folder to be used.
// The ar429_UDPCommRxEmbedded.mxf must be started first (usually ./ar429_UDPCommRxEmbedded.mxf).
// It sends a UDP datagram to the FM that is going to be used to start an aperiodic transmission,
// and waits for the answer (another UDP datagram sent by the FM).
// The UDP datagram sent by the FM must be the same than the one sent to the FM.
//
// Hardware Requirements:
// - MAXT FlexMulti with AR429 ports on it
//
*******************************************************************************/
#include "example.h"
#define BUFFER_SIZE 100
void udpInit(void);
void udpClose(int socket);
int main(void)
{
struct sockaddr_in txPort;
int txPortSocket=0;
char transmitStrg[BUFFER_SIZE] ="L 101 102 103 104 D 1111 2222 3333 4444",
receivedStrg[BUFFER_SIZE] ="";
uint64 i=0;
int bytes_sent=0, bytes_received=0;
socklen_t server_length=0;
char* pEnd;
int j=0;
int labelArray[10], datarecArray[10];
udpInit();
//Creates the socket
txPortSocket = (int)socket(AF_INET, SOCK_DGRAM, 0);
if (txPortSocket == -1)
{
printf("Could not create socket.\n");
#ifdef _linux
#else
WSACleanup();
#endif
}
/* Clear out server struct */
memset((void *)&txPort, '\0', sizeof(struct sockaddr_in));
server_length = sizeof(struct sockaddr_in);
txPort.sin_family = AF_INET;
txPort.sin_addr.s_addr = inet_addr("192.168.0.1"); //Destination address
txPort.sin_port = htons(8888); //port number to choose
//Sends the datagram to the FM
printf("Sending datagram...\n");
bytes_sent = sendto(txPortSocket, transmitStrg, (int)strlen(transmitStrg) + 1, 0, (struct sockaddr *)&txPort, (socklen_t)server_length);
if(bytes_sent == -1)
{
printf("Error sending datagram.\n");
udpClose(txPortSocket);
}
else
printf("Datagram sent successfully.\n");
//Receives the datagram from the FM
printf("Waiting for datagram...\n");
bytes_received = recvfrom(txPortSocket, receivedStrg, BUFFER_SIZE, 0, (struct sockaddr *)&(txPort), (socklen_t *)&server_length);
if(bytes_received != -1)
{
printf("Datagram received from the FM : %s.\n", receivedStrg);
i=0;
do
{
//Treats the labels
if(receivedStrg[i]=='L')
{
printf("Label(s) : ");
j=0;
pEnd = &receivedStrg[i+1];
while(pEnd[1] != 'D')
{
labelArray[j] = (int)(strtol(pEnd,&pEnd,10));
printf("%d ", labelArray[j]);
j++;
}
printf("\n");
}
//Treats the data
if(receivedStrg[i]=='D')
{
printf("Data : ");
j=0;
pEnd = &receivedStrg[i+1];
while(pEnd[1] != '\0')
{
datarecArray[j] = (int)(strtol(pEnd,&pEnd,10));
printf("%d ", datarecArray[j]);
j++;
}
printf("\n");
}
i++;
} while(receivedStrg[i] != '\0');
}
udpClose(txPortSocket);
printf("\n\nPress a key to terminate\n");
getchar();
return 0;
}
void udpInit(void)
{
#ifdef _linux
#else
WSADATA wsaData = { 0 };
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
wprintf(L"WSAStartup failed: %d\n", iResult);
return;
}
#endif
}
void udpClose(int socket)
{
#ifdef _linux
shutdown(socket, SHUT_RDWR);
close(socket);
#else
closesocket(socket);
WSACleanup();
#endif
}
Updated 10/23/2023