Anonymní profil dr – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil dr – Programujte.comAnonymní profil dr – Programujte.com

 

Příspěvky odeslané z IP adresy 88.102.97.–

dr
Java › client
5. 6. 2013   #177319

package client;

import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JPanel;
import java.awt.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
*
* @author fojtjan
*/
public class Client_test extends JPanel {

    /**
     * @param args the command line arguments
     */
    // int sample = 0;
    int[] sample = new int[1000];
    float[] seconds = new float[1000];
    short[] channel1 = new short[1000];
    short[] channel2 = new short[1000];
    float[] channel3 = new float[1000];
    float[] channel4 = new float[1000];
    byte dataType = 0x02;
    int lastSample = 0;
    int[] averages = new int[1000];

    public Client_test() {
        try {
            DataInputStream input = null;
            DataOutputStream output = null;

            Socket client = new Socket("localhost", 1234);
            input = new DataInputStream(client.getInputStream());
            output = new DataOutputStream(client.getOutputStream());
            output.writeByte(dataType);

            int counter = 0;

            switch (dataType) {
                case 0x01://načtení RAW dat

                    counter = 0;
                    while (counter < 1000) {
                        sample[counter] = Integer.reverseBytes(input.readInt());

                        channel1[counter] = Short.reverseBytes(input.readShort());
                        channel2[counter] = Short.reverseBytes(input.readShort());
                        lastSample = counter;

                        //System.out.println(sample[counter] + " " + channel1[counter] + " " + channel2[counter]);
                        counter++;
                    }


                    //moving average, plovoucí průměr
                    int avgWin = 20;//800 když chci baseline
                    int sum = 0;
                            //1. kanál
                    for (int i = 0; i < lastSample - avgWin; i++) {//nesmí být delší než j; proto - avgWin
                        //int average = 0;
                        for (int j = i; j < i + avgWin; j++) {
                            sum += channel1[j];
                        }
                        float average = sum / avgWin;
                        averages[i] = Math.round(average);
                        sum = 0;
                        //System.out.println("Prumer 1.kanalu: " + averages[i]);
                    }
                    //druhý kanál
                    //               int sum = 0;
                    //                int[] averages = new int[1000];
                    //                for(int i = 0; i < lastSample - avgWin; i++)
                    //                {                      
                    //                    //int average = 0;
                    //                    for(int j = i; j < i + avgWin; j++)
                    //                    {
                    //                        sum += channel2[j];
                    //                    }
                    //                    float average = sum/avgWin;
                    //                    averages2[i]= Math.round(average);
                    //                    sum = 0;
                    //                    System.out.println("Prumer 2.kanalu: " + averages2[i]);
                    //                }
                    break;
                case 0x02://Human data

                    counter = 0;
                    while (counter < 1000) {
                        seconds[counter] = Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
                        channel3[counter] = Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
                        channel4[counter] = input.readFloat();//Float.intBitsToFloat(Integer.reverseBytes(input.readInt()));
                        lastSample = counter;
                        System.out.println(seconds[counter] + " " + channel3[counter] + " " + channel4[counter]);
                        counter++;
                    }

                    break;
            }
            client.close();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Client_test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Client_test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void paintComponent(Graphics g) {//vykreslení

        super.paintComponent(g);

        Graphics2D graph = (Graphics2D) g;

        Dimension size = getSize();

        int w = size.width;
        int h = size.height;

        switch (dataType) {
            case 0x01:
                graph.setColor(Color.red);//kanál1
                for (int i = 0; i < lastSample; i++) {
                    int x = sample[i];
                    int y = ((channel1[i]) + 500) / 4;
                    int x1 = sample[i + 1];
                    int y1 = ((channel1[i + 1]) + 500) / 4;

                    graph.drawLine(x, y, x1, y1);
                }
                graph.setColor(Color.black); //moving average + baseline(avgWin se zvětší na cca počet vzorků
                for (int i = 0; i < lastSample; i++) {
                    int x = sample[i];
                    int y = ((averages[i]) + 500) / 4;
                    int x1 = sample[i + 1];
                    int y1 = ((averages[i + 1]) + 500) / 4;

                    graph.drawLine(x, y, x1, y1);
                }
                graph.setColor(Color.blue);
                for (int i = 0; i < lastSample; i++) {//druhý kanál + přidat moving průměr ;-) stejně jako o 3 řádky výš
                    int x = sample[i];
                    int y = ((channel2[i]) + 2000) / 4;
                    int x1 = sample[i + 1];
                    int y1 = ((channel2[i + 1]) + 2000) / 4;

                    graph.drawLine(x, y, x1, y1);
                }


                break;

            case 0x02://vykreslení human dat
                graph.setColor(Color.red);
                for (int i = 0; i < lastSample; i++) {
                    int x = Math.round(seconds[i] * 500);
                    int y = (int) (Math.round(channel3[i] * 200.0) + 200);//+ posunutí; * roztažení
                    int x1 = Math.round(seconds[i + 1] * 500);
                    int y1 = (int) (Math.round(channel3[i + 1] * 200.0) + 200);

                    graph.drawLine(i, y, i + 1, y1);
                }
                graph.setColor(Color.blue);
                for (int i = 0; i < lastSample; i++) {
                    int x = Math.round(seconds[i] * 500);
                    int y = (int) ((Math.round(channel4[i] * 200.0)) + 500);
                    int x1 = Math.round(seconds[i + 1] * 500);
                    int y1 = (int) ((Math.round(channel4[i + 1] * 200)) + 500);

                    graph.drawLine(i, y, i + 1, y1);
                }
                break;

        }

    }

    public static void main(String[] args) {
        // TODO code application logic here

        Client_test client = new Client_test();

        JFrame frame = new JFrame("Okno");//definice okna pro vykreslení
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(800, 800);
        frame.add(client);
        frame.setVisible(true);      
    }
}

MM
Java › client-server-client
5. 6. 2013   #177318

package client;

import java.awt.Graphics;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import javax.swing.JPanel;
import java.awt.*;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class Server {

   
    final int LEN = 10000000; //proměná která se zadává do polí jako největší číslo, můžeš pak měnit pouze toto číslo a mění se ti to všude ;-)
    int[] sample = new int[LEN]; //pole vzorků -> vydělením vzorkovací frekvencí vznikne čas
    float[] seconds = new float[LEN]; //pole sekund v ms -> nepoužívá se zde(server)
    short[] channel1 = new short[LEN]; //první kanál, prvních dat -> pomocí citlivosti vydělím kanál1 a vzniknou data kanálu 3
    short[] channel2 = new short[LEN]; // druhý kanál, prvních dat -> pomocí citlivosti vydělím kanál1 a vzniknou data kanálu 4
    //float[] channel3 = new float[LEN];
    //float[] channel4 = new float[LEN];
    byte dataType = 0x01;
    int lastSample = 0; //číslo posledního vzorku, určuje i počet vzorků
    int[] averages = new int[LEN];

    public Server() {
        try {
            DataInputStream input = null;
            DataOutputStream output = null;

            Socket client = new Socket("localhost", 1234);
            input = new DataInputStream(client.getInputStream());
            output = new DataOutputStream(client.getOutputStream());
            output.writeByte(dataType);

            int counter = 0;

            switch (dataType) {
                case 0x01:

                    counter = 0;
                    while (counter < sample.length) {//načtení dat ze serveru
                        sample[counter] = Integer.reverseBytes(input.readInt());//číslo vzorku

                        channel1[counter] = Short.reverseBytes(input.readShort());
                        channel2[counter] = Short.reverseBytes(input.readShort());
                        lastSample = counter;//uložení čísla posledního vzorku, poslední cyklus

                        // System.out.println(sample[counter] + " " + channel1[counter] + " " + channel2[counter]);
                        counter++;
                    }
                    break;
            }
        } catch (EOFException ex) {//HODNĚ DŮLEŽITÉ, po skončení načítání vystokočí tato hláška a uzavře se spojení se serverem
        } catch (UnknownHostException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(lastSample);
    }

    public static void main(String[] args) {
        try {
            // TODO code application logic here
            //Client client = new Client();

            ServerSocket socket = new ServerSocket(1234);//spuštění serveru
            Server_listener s = new Server_listener(socket);
            s.run();


        } catch (IOException ex) {
            Logger.getLogger(Server.class
                    .getName()).log(Level.SEVERE, null, ex);
        }


    }

    private static class Server_listener implements Runnable {

        Server client = new Server();//spuštění třídy Server, načtení atd...
        DataInputStream input = null;
        DataOutputStream output = null;
   
        ServerSocket server;

        public Server_listener(ServerSocket server) {
            this.server = server;

            //this.channel

        }

        @Override
        public void run() {
            while (true) {//cyklus serveru, musí být na začátku a true, aby mohl obsluhovat více klientů
                try {
                    Socket socket;
                    byte command = 0;
                    int counter = 0;


                    socket = server.accept();
                    System.err.println("Socket accepted");
                    DataInputStream input = new DataInputStream(socket.getInputStream());
                    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
                    command = input.readByte();


                    switch (command) {
                        case 0x01:
                            counter = 0;
                            while (counter < client.LEN) {//posílá data klienta dalšímu klientovi 0x01

                                output.writeInt(Integer.reverseBytes(client.sample[counter]));
                                output.writeShort(Short.reverseBytes(client.channel1[counter]));
                                output.writeShort(Short.reverseBytes(client.channel2[counter]));
                                counter++;

                            }
                            break;
                        case 0x02:
                            counter = 0;
                            while (counter < client.LEN) {

                                output.writeInt(Integer.reverseBytes(Float.floatToIntBits((float) client.sample[counter] / 250.0F)));//převedení vzorků na milisekundy, vydělením vzorku vzorkovací frekvencí
                                output.writeInt(Integer.reverseBytes(Float.floatToIntBits((float) client.channel1[counter] / 400.0F)));//převedení prvního kanálu na mV, vydělením citlivostí- citlivost = channel1/channel3
                                //output.writeInt(Integer.reverseBytes(Float.floatToIntBits((float) client.channel2[counter] / 400.0F)));//převedení prvního kanálu na mV, vydělením citlivostí- citlivost = channel1/channel3
                                output.writeFloat((float) client.channel2[counter] / 400.0F);
                                counter++;
                            }
                            break;
                    }
                    socket.close();//uzavření spojení
                    System.err.println("Socket closed");

                } catch (java.net.SocketException e) {//důležité
                } catch (IOException ex) {
                    Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

BL
Java › Vykreslení vzorku
29. 5. 2013   #176920

public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D graph = (Graphics2D) g;

        Dimension size = getSize();

        float w = size.height;
        int h = size.height;

        switch (typ) {
            case 0x01:

                graph.setColor(Color.blue);
                for (int i = 0; i < lastSample; i++) {

                    int x = Math.round((i * (float) w) / lastSample);
                    int y = channel1[i];
                    graph.drawLine(x, y, x, y);

                }
               
                graph.setColor(Color.red);
                for (int i = 0; i < lastSample; i++) {

                    int x = Math.round((i * (float) w) / lastSample);
                    int y = channel2[i];
                    graph.drawLine(x, y, x, y);

                }

}

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý