OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [examples/] [SimpleOperations.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
/**
2
 *
3
 */
4
package edu.byu.cc.plieber.fpgaenet.examples;
5
 
6
import java.io.IOException;
7
import java.net.InetAddress;
8
import java.net.UnknownHostException;
9
import java.util.ArrayList;
10
 
11
import edu.byu.cc.plieber.fpgaenet.fcp.FCPException;
12
import edu.byu.cc.plieber.fpgaenet.fcp.FCPProtocol;
13
import edu.byu.cc.plieber.util.StringUtil;
14
 
15
/**
16
 * @author Peter Lieber
17
 *
18
 */
19
public class SimpleOperations {
20
 
21
        /**
22
         * Sends or receives data to/from the FPGA over FCP through the given channel.
23
         * @param args
24
         * @throws UnknownHostException
25
         */
26
        public static void main(String[] args) throws UnknownHostException {
27
                if (args.length < 4) {
28
                        System.out
29
                                        .println("Usage: SimpleOperations <'send'|'read'> <ipaddress> <channel> <dataToSend(hex byte)|numBytesToRead> [more data...]");
30
                }
31
                FCPProtocol p = createConnection(InetAddress.getByName(args[2]));
32
                int channel = Integer.parseInt(args[3]);
33
                if (args[1] == "send") {
34
                        int numBytes = args.length - 4;
35
                        ArrayList<Byte> data = new ArrayList<Byte>();
36
                        for (int i = 4; i < args.length; i++) {
37
                                data.add(new Byte((byte) (Integer.parseInt(args[i], 16))));
38
                        }
39
                        send(p, channel, data, numBytes);
40
                } else if (args[1] == "read") {
41
                        ArrayList<Byte> data = read(p, channel, Integer.parseInt(args[4]));
42
                        System.out.println(StringUtil.listToString(data));
43
                } else {
44
                        System.out
45
                                        .println("Usage: SimpleOperations <'send'|'read'> <ipaddress> <dataToSend(hex byte)|numBytesToRead> [more data...]");
46
                }
47
        }
48
 
49
        private static ArrayList<Byte> read(FCPProtocol p, int channel, int numBytes) {
50
                try {
51
                        p.sendDataRequest(channel, numBytes);
52
                } catch (FCPException e) {
53
                        // TODO Auto-generated catch block
54
                        e.printStackTrace();
55
                }
56
                byte[] data = p.getDataResponse();
57
                ArrayList<Byte> ret = new ArrayList<Byte>();
58
                for (int i = 0; i < data.length; i++) {
59
                        ret.add(data[i]);
60
                }
61
                return ret;
62
        }
63
 
64
        private static void send(FCPProtocol p, int channel, ArrayList<Byte> data, int numBytes) {
65
                try {
66
                        p.sendData(channel, data, numBytes);
67
                } catch (FCPException e) {
68
                        // TODO Auto-generated catch block
69
                        e.printStackTrace();
70
                }
71
        }
72
 
73
        static FCPProtocol createConnection(java.net.InetAddress address) {
74
                FCPProtocol p = null;
75
                try {
76
                        p = new FCPProtocol();
77
                        p.connect(address);
78
                } catch (IOException e) {
79
                        // TODO Auto-generated catch block
80
                        e.printStackTrace();
81
                }
82
                while (!p.isConnected())
83
                        ;
84
                return p;
85
        }
86
 
87
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.