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/] [FCPInteface.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 peteralieb
package edu.byu.cc.plieber.fpgaenet.examples;
2
import java.io.IOException;
3
import java.lang.Character.UnicodeBlock;
4
import java.net.InetAddress;
5
import java.util.ArrayList;
6
 
7
import com.trolltech.qt.gui.*;
8
 
9
import edu.byu.cc.plieber.fpgaenet.fcp.FCPException;
10
import edu.byu.cc.plieber.fpgaenet.fcp.FCPProtocol;
11
import edu.byu.cc.plieber.util.StringUtil;
12
 
13
public class FCPInteface extends QWidget{
14
 
15
    public static void main(String[] args) {
16
        QApplication.initialize(args);
17
 
18
        FCPInteface testFCPInteface = new FCPInteface(null);
19
        testFCPInteface.show();
20
 
21
        QApplication.exec();
22
 
23
        testFCPInteface.tearDown();
24
    }
25
 
26
    private QVBoxLayout mainLayout;
27
    private QHBoxLayout toprow;
28
    private QGridLayout mainrow;
29
    private QGridLayout readcon;
30
    private QVBoxLayout writecon;
31
 
32
    private QLabel lblPort;
33
    private QLineEdit txtPort;
34
    private QPushButton btnConnect;
35
    private QPushButton btnRead;
36
    private QLabel lblNumBytes;
37
    private QLineEdit txtNumBytes;
38
    private QTextEdit textRead;
39
    private QPushButton btnWrite;
40
    private QRadioButton radASCII;
41
    private QRadioButton radHex;
42
    private QTextEdit textWrite;
43
 
44
    public FCPInteface(QWidget parent){
45
        super(parent);
46
        setWindowTitle("FCP Interface");
47
        makeConnections();
48
        createWidgets();
49
        connectSignalsAndSlots();
50
    }
51
 
52
    FCPProtocol fcpProtocol;
53
        private void makeConnections() {
54
                try {
55
                        fcpProtocol = new FCPProtocol();
56
                        fcpProtocol.connect(InetAddress.getByName("192.168.1.222"), 0x3001);
57
                } catch (IOException e) {
58
                        return;
59
                }
60
                while(!fcpProtocol.isConnected());
61
        }
62
 
63
        private void createWidgets() {
64
                // TODO Auto-generated method stub
65
                mainLayout = new QVBoxLayout(this);
66
                this.setLayout(mainLayout);
67
                toprow = new QHBoxLayout();
68
                mainrow = new QGridLayout();
69
                mainLayout.addLayout(toprow);
70
                mainLayout.addLayout(mainrow);
71
                readcon = new QGridLayout();
72
                writecon = new QVBoxLayout();
73
                mainrow.addLayout(readcon, 0, 0);
74
                mainrow.addLayout(writecon, 1, 0);
75
 
76
                lblPort = new QLabel("Port");
77
                txtPort = new QLineEdit("1");
78
                btnConnect = new QPushButton("Connect");
79
                btnRead = new QPushButton("Read");
80
                lblNumBytes = new QLabel("#");
81
                txtNumBytes = new QLineEdit("1");
82
                textRead = new QTextEdit();
83
                btnWrite = new QPushButton("Write");
84
                radASCII = new QRadioButton("ASCII");
85
                radHex = new QRadioButton("Hex");
86
                textWrite = new QTextEdit();
87
 
88
                toprow.addWidget(lblPort);
89
                toprow.addWidget(txtPort);
90
                toprow.addWidget(btnConnect);
91
                readcon.addWidget(btnRead, 0, 0, 1, 2);
92
                readcon.addWidget(lblNumBytes, 1, 0);
93
                readcon.addWidget(txtNumBytes, 1, 1);
94
                mainrow.addWidget(textRead, 0, 1);
95
                writecon.addWidget(btnWrite);
96
                writecon.addWidget(radASCII);
97
                writecon.addWidget(radHex);
98
                mainrow.addWidget(textWrite, 1, 1);
99
        }
100
 
101
        private void connectSignalsAndSlots() {
102
                btnRead.clicked.connect(this, "readData()");
103
                btnWrite.clicked.connect(this, "writeData()");
104
        }
105
 
106
        @SuppressWarnings("unused")
107
        private void writeData() {
108
                //QMessageBox.information(this, "Write Data", "Writing Data ...");
109
                String text = textWrite.document().toPlainText();
110
                ArrayList<Byte> bytes = new ArrayList<Byte>();
111
                if (radHex.isChecked()) {
112
                        String[] tokens = text.split(" ");
113
                        for (String bytestr : tokens) {
114
                                byte thebyte = (byte) Integer.parseInt(bytestr, 16);
115
                                bytes.add(thebyte);
116
                        }
117
                }
118
                else {
119
                        char[] chars = text.toCharArray();
120
                        for (char bytechar : chars) {
121
                                byte thebyte = (byte) (bytechar & 0xff);
122
                                bytes.add(thebyte);
123
                        }
124
                }
125
                try {
126
                        fcpProtocol.sendData(Integer.parseInt(txtPort.text()), bytes, bytes.size());
127
                } catch (NumberFormatException e) {
128
                        // TODO Auto-generated catch block
129
                        e.printStackTrace();
130
                } catch (FCPException e) {
131
                        // TODO Auto-generated catch block
132
                        e.printStackTrace();
133
                }
134
                this.setStatusTip(StringUtil.listToString(bytes) + " written to FCP Channel " + txtPort.text());
135
        }
136
 
137
        @SuppressWarnings("unused")
138
        private void readData() {
139
                //QMessageBox.information(this, "Read Data", "Reading Data ...");
140
                int numBytes = Integer.parseInt(txtNumBytes.text());
141
                int port = Integer.parseInt(txtPort.text());
142
                try {
143
                        fcpProtocol.sendDataRequest(port, numBytes);
144
                } catch (FCPException e) {
145
                        // TODO Auto-generated catch block
146
                        e.printStackTrace();
147
                }
148
                byte[] bytes = fcpProtocol.getDataResponse();
149
                textRead.clear();
150
                textRead.append(StringUtil.arrayToString(bytes));
151
        }
152
 
153
        public  void tearDown() {
154
                fcpProtocol.disconnect();
155
        }
156
}

powered by: WebSVN 2.1.0

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