menu

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.Socket;

import java.net.SocketTimeoutException;

import java.net.UnknownHostException;

import java.nio.ByteBuffer;

 

public class SimpleReader {

 

   public final static int    TOOLSCOPE_TCP_PORT = 2100;           //----- Port to connect on the ToolScope

   public final static String TOOLSCOPE_IP = "192.168.1.250";       //----- IP of ToolScope

  

   public static void main(String[] args) {

     

      // Define generic variables

      Socket TCPSocket = null;             //----- TCP Socket

      BufferedReader TCPInput = null;                 //----- Reader for data via TCP/IP

      BufferedWriter TCPOutput = null;                 //----- Writer for data via TCP/IP

      int dataChannels = 0;                    //----- Count of data channels

     

      //----- Connect via TCP/IP

      try {

         InetAddress address = InetAddress.getByName(TOOLSCOPE_IP);

         TCPSocket = new Socket(address, TOOLSCOPE_TCP_PORT);

      } catch (Exception e) {

         System.out.println("Konnte nicht mit dem Messgerät verbinden:");

         e.printStackTrace();

         System.exit(-1);

      }

 

      //----- Get streams for the TCP/IP connection, read data describtion

      try {

         TCPInput = new BufferedReader(new InputStreamReader(TCPSocket.getInputStream()));

         TCPOutput = new BufferedWriter(new OutputStreamWriter(TCPSocket.getOutputStream()));

 

         System.out.println("Sende über TCP/IP: SendDataDescription <ENTER>");

         TCPOutput.write("SendDataDescription\n");

         TCPOutput.flush();

 

         TCPSocket.setSoTimeout(500);

 

         Thread.sleep(1000);

        

         String input = null;

        

         try {

                while (true) {

                   input = TCPInput.readLine();

                   System.out.println("Empfange über TCP/IP: "+input+ " <ENTER>");

                }

         } catch (SocketTimeoutException e) {

                //----- got all data

         }

         //----- the incoming columns are seperated by ';'

         //----- count the columns with that assumption

         for (int idx=0;idx<input.length();idx++)

                if (input.charAt(idx)==';')

                   dataChannels++;

 

         //----- we have exactly one columns more

         //----- as ';', because ';' divides the columns

         dataChannels = dataChannels+1;

        

      } catch (Exception e) {

         System.out.println("Konnte keine Informationen über Messdaten anfordern:");

         e.printStackTrace();

         try {

                TCPSocket.close();

                TCPInput.close();

                TCPOutput.close();

         } catch (Exception e2) {;} //----- ignore following errors

         System.exit(-1);

      }

     

      //----- define a buffer for receiving UDP packets

      byte[] buffer = new byte[dataChannels*Double.SIZE/8];

      DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);

      ByteBuffer dataBufferWrapper = ByteBuffer.wrap(buffer);

     

      //----- read 10 data packets

      DatagramSocket datagramSocket = null;

      try {

         datagramSocket = new DatagramSocket();

         datagramSocket.setReceiveBufferSize(buffer.length * 100); //----- increases the connection savety

         datagramSocket.setSoTimeout(2000);                                                    //----- increases the connection savety

 

         System.out.println("Sende über TCP/IP: StartUDPTransfer <ENTER>");

         System.out.println("Sende über TCP/IP: "+datagramSocket.getLocalPort()+" <ENTER>");

         TCPOutput.write("StartUDPTransfer\n"+datagramSocket.getLocalPort()+"\n");

         TCPOutput.flush();

         for (int packetCount=0;packetCount<10;packetCount++) {

                datagramSocket.receive(datagramPacket);

                System.out.print("Empfange über UDP: ");

                for (int channelIdx=0;channelIdx<dataChannels;channelIdx++)

                   System.out.printf("%8.2f ", dataBufferWrapper.getDouble(channelIdx*Double.SIZE/8));

                System.out.println();

         }

      } catch (Exception e) {

         System.out.println("Auslesen von Messdaten per UDP fehlgeschlagen:");

         e.printStackTrace();

         System.exit(-1);

      }

 

      //----- Close the TCP/IP and UDP connection

      try {

         TCPInput.close();

         TCPOutput.close();

         TCPSocket.close();

         datagramSocket.close();

      } catch (Exception e2) {;} //----- ignore following errors

 

      System.out.println("Alle Aktionen erfolgreich beendet!");

      System.exit(0);

   }

}