OpenCores
URL https://opencores.org/ocsvn/mod_mult_exp/mod_mult_exp/trunk

Subversion Repositories mod_mult_exp

[/] [mod_mult_exp/] [trunk/] [sw/] [WebExponentiator/] [src/] [main/] [java/] [webexponentiator/] [util/] [Communication.java] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 gajos
package webexponentiator.util;
2
 
3
import java.io.InputStream;
4
import java.io.OutputStream;
5
import java.math.BigInteger;
6
import java.util.ArrayList;
7
import java.util.HashMap;
8
 
9
import org.apache.commons.lang3.ArrayUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
 
13
import com.fazecast.jSerialComm.SerialPort;
14
import com.fazecast.jSerialComm.SerialPortDataListener;
15
import com.fazecast.jSerialComm.SerialPortEvent;
16
 
17
public class Communication implements SerialPortDataListener {
18
 
19
        protected final Log logger = LogFactory.getLog(getClass());
20
 
21
        String selectedPort = "COM3";
22
 
23
        // just a boolean flag that i use for enabling
24
        // and disabling buttons depending on whether the program
25
        // is connected to a serial port or not
26
        private boolean bConnected = false;
27
 
28
        // the timeout value for connecting with the port
29
        final static int TIMEOUT = 2000;
30
 
31
        // for containing the ports that will be found
32
        private SerialPort[] ports = null;
33
        // map the port names to CommPortIdentifiers
34
        private HashMap<String, SerialPort> portMap = new HashMap<String, SerialPort>();
35
 
36
        // this is the object that contains the opened port
37
        private SerialPort serialPort = null;
38
 
39
        // input and output streams for sending and receiving data
40
        private InputStream input = null;
41
        private OutputStream output = null;
42
 
43
        public Communication() {
44
 
45
        }
46
 
47
        // a string for recording what goes on in the program
48
        // this string is written to the GUI
49
        String logText = "";
50
 
51
        // search for all the serial ports
52
        // pre style="font-size: 11px;": none
53
        // post: adds all the found ports to a combo box on the GUI
54
        public ArrayList<String> searchForPorts() {
55
 
56
                ArrayList<String> result = new ArrayList<String>();
57
 
58
                ports = SerialPort.getCommPorts();
59
                logger.info("COM ports identified");
60
 
61
                for (SerialPort port:ports) {
62
                        String portName = port.getDescriptivePortName();
63
                        if (portName.contains("COM3")) {
64
                                selectedPort = portName;
65
                        }
66
                        // get only serial ports
67
                        result.add(portName);
68
                        portMap.put(portName, port);
69
                }
70
 
71
                return result;
72
        }
73
 
74
        public String connect() {
75
                serialPort = (SerialPort) portMap.get(selectedPort);
76
                serialPort.setBaudRate(115200);
77
                serialPort.setNumDataBits(8);
78
                serialPort.setNumStopBits(1);
79
                serialPort.setParity(SerialPort.ODD_PARITY);
80
 
81
                        // the method below returns an object of type CommPort
82
                        setConnected(serialPort.openPort());
83
 
84
                        // for controlling GUI elements
85
 
86
                        // logging
87
                        logger.info(selectedPort + " opened successfully.");
88
 
89
                        return "COM3 connected ok";
90
        }
91
 
92
        public boolean initIOStream() {
93
                // return value for whether opening the streams is successful or not
94
                boolean successful = false;
95
 
96
                        input = serialPort.getInputStream();
97
                        output = serialPort.getOutputStream();
98
 
99
                        successful = true;
100
                        return successful;
101
 
102
        }
103
 
104
        public void initListener() {
105
                        serialPort.addDataListener(this);
106
        }
107
 
108
        public void disconnect() {
109
                // close the serial port
110
                try {
111
                        serialPort.removeDataListener();
112
                        input.close();
113
                        output.close();
114
                        serialPort.closePort();
115
                        setConnected(false);
116
 
117
                        logger.info("Disconnected.");
118
                } catch (Exception e) {
119
                        logger.error("Failed to close " + serialPort.getDescriptivePortName() + "("
120
                                        + e.toString() + ")");
121
                }
122
        }
123
 
124
        public String readData() {
125
                try {
126
                        byte[] buffer = new byte[65];
127
                        int n = input.read(buffer);
128
                        if (n > 0) {
129
                                if (n == 1) {
130
                                        BigInteger command = new BigInteger(new byte[] { 0,
131
                                                        buffer[0] });
132
                                        String s = "Command = " + command.toString(16);
133
                                        return s;
134
                                } else {
135
                                        buffer = ArrayUtils.subarray(buffer, 0, buffer.length - 1);
136
                        buffer = ArrayUtils.add(buffer, (byte)0);
137
                        ArrayUtils.reverse(buffer);
138
                        BigInteger data = new BigInteger(buffer);
139
                                        return data.toString(16);
140
                                }
141
                        }
142
                } catch (Exception e) {
143
                        logger.error("Failed to read data. (" + e.toString() + ")");
144
                }
145
                return null;
146
        }
147
 
148
        @Override
149
        public int getListeningEvents() {
150
                return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
151
        }
152
 
153
        @Override
154
        public void serialEvent(SerialPortEvent evt) {
155
                if (evt.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
156
                        try {
157
                                byte[] buffer = new byte[10];
158
                                int n = input.read(buffer);
159
                                if (n > 0) {
160
                                        if (n == 1) {
161
                                                BigInteger command = new BigInteger(new byte[] { 0,
162
                                                                buffer[0] });
163
                                                final String s = "Command = " + command.toString(16)
164
                                                                + "\n";
165
                                                //TODO Something for retrieve data
166
                                                //window.appendText(s);
167
                                        } else {
168
                                                ArrayUtils.reverse(buffer);
169
                                                BigInteger data = new BigInteger(buffer);
170
                                                //TODO Something for retrieve data
171
                                                //window.appendText(data.toString(10) + "\n");
172
                                        }
173
                                }
174
                        } catch (Exception e) {
175
                                logger.error("Failed to read data. (" + e.toString() + ")");
176
                        }
177
                }
178
        }
179
 
180
        public void writeByte(byte b) {
181
                try {
182
                        output.write(b);
183
                        Thread.sleep(1);
184
                } catch (Exception e) {
185
                        logger.error("Failed to write data. (" + e.toString() + ")");
186
                }
187
        }
188
 
189
        // method that can be called to send data
190
        // pre style="font-size: 11px;": open serial port
191
        // post: data sent to the other device
192
        public void writeData(String str) {
193
                try {
194
                        BigInteger bi = new BigInteger(str, 2);
195
                        byte[] b = bi.toByteArray();
196
                        if (b.length == 2) {
197
                                output.write(b[1]);
198
                        } else {
199
                                output.write(b);
200
                        }
201
                        Thread.sleep(1);
202
                } catch (Exception e) {
203
                        logger.error("Failed to write data. (" + e.toString() + ")");
204
                }
205
        }
206
 
207
        final public boolean getConnected() {
208
                return bConnected;
209
        }
210
 
211
        private void setConnected(boolean bConnected) {
212
                this.bConnected = bConnected;
213
        }
214
 
215
}

powered by: WebSVN 2.1.0

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