menu

#include <winsock2.h>                                    //----- Has to be before <windows.h> to avoid re-definitions

#include <windows.h>

#include <stdio.h>                                            //----- Needed for output of debug values and measuring data

 

#define TOOLSCOPE_TCP_PORT 2100

#define TOOLSCOPE_UDP_PORT 2101

#define TOOLSCOPE_IP "192.168.1.250"       //----- IP of ToolScope

#define MAX_VALUES_TO_READ 100

 

//----- in some versions of WinSock there is a definition of INADDR_ANY  instead of ADDR_ANY

#ifndef ADDR_ANY

#define ADDR_ANY INADDR_ANY

#endif

 

int main(void) {

      WSADATA wsa;

      int result;

 

      //----- Start Winsock

      result = WSAStartup(0x0002, &wsa);   //----- 0x0002 for  Winsock 2.0, 0x0101 für WS 1.1

      if (result) {                                             //----- WinSock API error processing

             printf("Error: WinSock could not be started, ");

             switch(result) {

                   case WSASYSNOTREADY:    

                         printf("WinSock not ready. "); break;

                   case WSAVERNOTSUPPORTED:

                         printf("the requested WinSock version is not supported"); break;

                   case WSAEINPROGRESS:    

                         printf("there is a blocking WinSock 1.1 operation active"); break;

                   case WSAEPROCLIM:       

                         printf("maximum count of WinSock tasks is reached"); break;

                   case WSAEFAULT:         

                         printf("lpWSAData is a non-valid pointer "); break;

                   default:                

                         printf("unknown error "); break;

             }

            printf("Error code: %d\n", WSAGetLastError());       

             return result;

      }

      printf("DEBUG: started WinSock \n");

 

 

      //----- TCP-Socket erstellen

      SOCKET ControlSocket = socket(AF_INET, SOCK_STREAM, 0);

      if(ControlSocket==INVALID_SOCKET) {

             printf("Error: Could not create socket, error code: %d\n",

                   WSAGetLastError());

             return 1;

      }

      printf("DEBUG: created TCP socket\n");

     

      //----- Create SOCKADDR structure and fill with zeros

      SOCKADDR_IN TCPAddr;

      memset(&TCPAddr, 0, sizeof(TCPAddr));                                                 //----- fill with zeros

      TCPAddr.sin_family                = AF_INET;                                                                 //-----

      TCPAddr.sin_port                   = htons(TOOLSCOPE_TCP_PORT);      //----- Control port of ToolScope

      TCPAddr.sin_addr.s_addr     = inet_addr(TOOLSCOPE_IP);                //----- IP address of ToolScope

 

      //----- Connect TCP

      result = connect(ControlSocket, (sockaddr*)&TCPAddr, sizeof(SOCKADDR_IN));

      if (result == -1)  {

             printf("Error: could not initialize TCP connection, error code: %d\n",

                   WSAGetLastError());

             return 2;

      }

      printf("DEBUG: connected TCP\n");

     

      //----- Request and print data format

      char DataDescription[1000];

      send(ControlSocket, "SendDataDescription\n", 20, 0);

      printf("DEBUG: requested data format\n");

      result = recv(ControlSocket, DataDescription, 1000, 0);

      DataDescription[result]='\0';

      printf("\n\n%s", DataDescription);

      result = recv(ControlSocket, DataDescription, 1000, 0);

      DataDescription[result]='\0';

      printf("%s\n\n", DataDescription);

 

      //----- Create UDP socket

      SOCKET DataSocket = socket(AF_INET, SOCK_DGRAM, 0);

      if(DataSocket == INVALID_SOCKET) {

             printf("Error: could not create UDP socket, error code: %d\n",

                   WSAGetLastError());

             return 3;

      }

      printf("DEBUG: created UDP socket \n");

 

      //----- create SOCKADDR structure for UDP and fill with zeros

      SOCKADDR_IN UDPAddr;

      memset(&UDPAddr, 0, sizeof(UDPAddr));                                                 //----- fill with zeros

      UDPAddr.sin_family          = AF_INET;                                                        //-----

      UDPAddr.sin_port                   = htons(TOOLSCOPE_UDP_PORT);       //----- local UDP port

      UDPAddr.sin_addr.s_addr     = ADDR_ANY;

      int UDPAddrLen = sizeof(SOCKADDR_IN);

 

      //----- bind UDP socket to local UDP port

      result = bind(DataSocket,(SOCKADDR*)&UDPAddr,sizeof(SOCKADDR_IN));

      if(result == SOCKET_ERROR) {

             printf("Error: could not bind UDP socket to port %d , error code: %d\n",

                   TOOLSCOPE_UDP_PORT, WSAGetLastError());

             return 4;

      } else { printf("DEBUG: bound UDP socket to port %d \n", TOOLSCOPE_UDP_PORT); }

 

      //----- Start UDP data transfer

      send(ControlSocket, "StartUDPTransfer\n", 17, 0);

      char UDPPort[6];

      int UDPPortLen = sprintf(UDPPort, "%d\n", TOOLSCOPE_UDP_PORT);

      send(ControlSocket, UDPPort, UDPPortLen, 0);

      printf("DEBUG: started UDP transfer \n\n");

 

      //----- Buffer for received data (original values)

      byte DataBufByte1[MAX_VALUES_TO_READ*sizeof(double)];               

      union {

             //----- Buffer for received data (regrouped values)

             byte DataBufByte2[MAX_VALUES_TO_READ*sizeof(double)];

             //----- Buffer for received data (regrouped values)

             double DataBufDouble[MAX_VALUES_TO_READ];                                                                          

      };

      //----- Buffer for data which is formatted as string

      char Ausgabe[10*MAX_VALUES_TO_READ];

 

      for (int n=1; n<=10; n++) {       //----- for this example we read 10 UDP packets

             //----- Read measuring values by UDP

             result =       recvfrom(DataSocket, (char*)DataBufByte1, MAX_VALUES_TO_READ*sizeof(double), 0, (SOCKADDR*)&UDPAddr, &UDPAddrLen);

             if(result == SOCKET_ERROR) {

                   printf("Error: could not receive UDP data, error code: %d\n", WSAGetLastError());

                   return 5;

             } else { printf("DEBUG: received %d bytes!\n", result); }

            

            

//----- Rearrange the bytes (network byte order)

             for (int k=0; k<result; k+=sizeof(double)) {

                   for (int m=0; m<sizeof(double); m++) {

                         DataBufByte2[k+m] = DataBufByte1[k+sizeof(double)-m-1];

                   }

             }

 

             //----- print measuring values

             sprintf(Ausgabe, "%.2e", DataBufDouble[0]);

             for (int i=1; i<result/sizeof(double); i++) {

                   sprintf(Ausgabe, "%s, %.2e", Ausgabe, DataBufDouble[i]);

             }

             printf("Read data (%d. turn):\n%s\n\n", n, Ausgabe);

             Sleep(1000);                //----- wait 1 second

      }

 

      //----- clean up

      closesocket(ControlSocket);

      closesocket(DataSocket);

      WSACleanup();

      return 0;

}

 

Output of C++ program

DEBUG: started WinSock

DEBUG: created TCP socket

DEBUG: connected TCP

DEBUG: Requested data format

 

 

GetDataDescription

-;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge;Analoge Eingänge

-;-;-;-;-;-;-;-;-

Zeit;Klemme 16;Klemme 17;Klemme 18;Klemme 19;Klemme 36;Klemme 37;Klemme 38;Klemme 39

s;V;V;V;V;V;V;V;V

0,01;-;-;-;-;-;-;-;-

-;-;-;-;-;-;-;-;-

-;-;-;-;-;-;-;-;-

-;-;-;-;-;-;-;-;-

 

DEBUG: created UDP socket

DEBUG: DEBUG: bound UDP socket to port 2101

DEBUG: started UDP transfer

 

Read data (1. turn):

12106.3909, 0.682, -2.518, -8.882, -9.619, -9.357, -0.002, -9.294, -8.234

 

Read data (2. turn):

12106.3914, 0.603, -2.515, -8.889, -9.644, -9.361, -0.002, -9.300, -8.238

 

Read data (3. turn):

12106.3919, 0.527, -2.513, -8.892, -9.637, -9.362, -0.003, -9.302, -8.234

 

(and so on...)