Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
157 kaklik 1
/*
2
    AVRcamVIEW: A PC application to test out the functionallity of the
3
     AVRcam real-time image processing engine.
4
    Copyright (C) 2004    Brent A. Taylor
5
 
6
    This program is free software; you can redistribute it and/or
7
    modify it under the terms of the GNU General Public
8
    License as published by the Free Software Foundation; either
9
    version 2 of the License, or (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
    General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public
17
    License along with this program; if not, write to the Free Software
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
20
   For more information on the AVRcamVIEW, please contact:
21
 
22
   taylorba@comcast.net
23
 
24
   or go to www.jrobot.net for more details regarding the system.
25
*/
26
 
27
package avr.connection;
28
 
29
import java.io.*;
30
import java.net.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
import java.util.*;
34
 
35
import javax.comm.*;
36
 
37
/***********************************************************************
38
 * This class represents a Connection to a Serial Port.
39
 */
40
public class SerialConnection extends AbstractConnection {
41
 
42
   private SerialPort serialPort;
43
   private SerialParams params;
44
   private String comPort;
45
 
46
   public static String[] getSerialPorts() {
47
 
48
      ArrayList ports = new ArrayList();
49
 
50
      Enumeration allPorts = CommPortIdentifier.getPortIdentifiers();
51
      CommPortIdentifier identifier;
52
 
53
      while(allPorts.hasMoreElements()) {
54
         identifier = (CommPortIdentifier)allPorts.nextElement();
55
         if(identifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
56
            ports.add(identifier.getName());
57
         }
58
      }
59
 
60
      Collections.reverse(ports);
61
 
62
      return (String[])ports.toArray(new String[ports.size()]);
63
 
64
   }
65
 
66
   public SerialConnection(String comPort) {
67
      this(comPort, new SerialParams());
68
   }
69
 
70
   public SerialConnection(String comPort, SerialParams params) {
71
      this.comPort = comPort;
72
      this.params = params;
73
   }
74
 
75
   public Object getConnectionObject() {
76
      return serialPort;
77
   }
78
 
79
   public synchronized void connect() throws Exception {
80
      CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(comPort);
81
      serialPort = (SerialPort)identifier.open("AVRcamView", 2000);
82
      serialPort.setFlowControlMode(params.getFlowControl());
83
      serialPort.setSerialPortParams(params.getBaudRate(),
84
                                     params.getDataBits(),
85
                                     params.getStopBits(),
86
                                     params.getParity());
87
      setConnected();
88
   }
89
 
90
   public synchronized void disconnect() throws IOException {
91
      serialPort.close();
92
      setDisconnected();
93
   }
94
 
95
   public InputStream getInputStream() throws IOException {
96
      return serialPort.getInputStream();
97
   }
98
 
99
   public OutputStream getOutputStream() throws IOException {
100
      return serialPort.getOutputStream();
101
   }
102
 
103
   public ByteChannel getChannel() {
104
      return null;
105
   }
106
 
107
   public String toString() {
108
      return serialPort.toString();
109
   }
110
 
111
   public void setSerialParams(SerialParams params) throws UnsupportedCommOperationException {
112
      serialPort.setFlowControlMode(params.getFlowControl());
113
      serialPort.setSerialPortParams(params.getBaudRate(),
114
                                     params.getDataBits(),
115
                                     params.getStopBits(),
116
                                     params.getParity());
117
      this.params = params;
118
   }
119
 
120
   public String getComPort() {
121
      return comPort;
122
   }
123
 
124
   public SerialParams getSerialParams() {
125
      return params;
126
   }
127
 
128
}