*/\r
\r
import java.io.BufferedReader;\r
-//import java.io.BufferedWriter;\r
import java.io.DataInputStream;\r
import java.io.DataOutputStream;\r
import java.io.IOException;\r
import java.io.InputStreamReader;\r
import java.io.InterruptedIOException;\r
-//import java.io.OutputStreamWriter;\r
import java.net.InetAddress;\r
import java.net.Socket;\r
import java.net.UnknownHostException;\r
* This class does the TCP/IP work to operate the XPort Most of the get and set\r
* methods herein follow the same pattern: Establish a connection, prepare a\r
* command, send this command and disconnect.\r
- * \r
+ * \r
*/\r
public class Communication {\r
\r
- /** connection to XPort */\r
- protected Socket socket = null;\r
+ /** connection to XPort */\r
+ protected Socket socket = null;\r
+\r
+ /** dis contains data provided by XPort */\r
+ protected BufferedReader dis = null;\r
+\r
+ protected DataInputStream dis2 = null;\r
+\r
+ /** dos contains data sent to XPort */\r
+ protected DataOutputStream dos = null;\r
+\r
+ /**\r
+ * This method converts the given String ipa_str and the int port into a\r
+ * useable TCP/IP address and then calls a helper method tcpip_connect() to\r
+ * establish a connection to the XPort\r
+ * \r
+ * @param ipa_str\r
+ * @param port\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public void connect(String ipa_str, int port) throws IOException, Exception {\r
+ InetAddress ipa = null;\r
+ try {\r
+ ipa = InetAddress.getByName(ipa_str);\r
+ } catch (UnknownHostException UHEx) {\r
+ throw UHEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try {\r
+ tcpip_connect(ipa, port);\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * this method will end the current connection to the XPort\r
+ * \r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized void disconnect() throws IOException, Exception {\r
+ try {\r
+ tcpip_disconnect();\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * uses a TCP/IP connection to send a String to the XPort\r
+ * \r
+ * @param sendString\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized void send(String sendString) throws IOException,\r
+ Exception {\r
+ System.out.println(sendString);\r
+ try {\r
+ dos.writeBytes(sendString);// , 0, sendString.length());\r
+ // write data to dos as String starting at String[0]\r
+ // and ending at String[String.length()]\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try {\r
+ dos.flush(); // write dos to XPort\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * uses a TCP/IP connection to send a String to the XPort\r
+ * \r
+ * @param sendByte\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized void send(int sendByte[]) throws IOException, Exception {\r
+ dos.flush();\r
+ byte[] b = new byte[sendByte.length];\r
+ for (int i = 0; i < sendByte.length; i++) {\r
+ b[i] = (byte)(sendByte[i] & 255);\r
+ }\r
+ try {\r
+ dos.write(b); // bytewise write data to dos\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try {\r
+ dos.flush(); // write dos to XPort\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+\r
+ return;\r
+ }\r
+\r
+ /**\r
+ * closes the TCP/IP connection\r
+ * \r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ private void tcpip_disconnect() throws IOException, Exception {\r
+ if (socket.isConnected()) {\r
+ try {\r
+ dis.close(); // close input and output stream\r
+ dos.close(); // as well as the socket\r
+ socket.close();\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * reads data supplied by the XPort and returns a String\r
+ * \r
+ * @return\r
+ * @throws InterruptedIOException\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public String receiveString() throws InterruptedIOException, IOException,\r
+ Exception {\r
+ String antwort = "";\r
+ int length = 255;\r
+ byte[] in = new byte[length];\r
+\r
+ try {\r
+ dis.ready(); // check if data is available (actually redundant\r
+ // since\r
+ // the calling method from the GUI class already made\r
+ // this sure but better safe than sorry... :-)\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+\r
+ try {\r
+\r
+ length = readBuffer(in, length); // call helper method to read\r
+ // from dis\r
+ antwort = new String(in, 0, length);\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ return antwort;\r
+ }\r
\r
- /** dis contains data provided by XPort */\r
- protected BufferedReader dis = null;\r
- \r
- protected DataInputStream dis2 =null;\r
- \r
- /** dos contains data sent to XPort */\r
- protected DataOutputStream dos=null;\r
- \r
- /**\r
- * This method converts the given String ipa_str and the int port into a\r
- * useable TCP/IP address and then calls a helper method tcpip_connect() to\r
- * establish a connection to the XPort\r
- * \r
- * @param ipa_str\r
- * @param port\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public void connect(String ipa_str, int port) throws IOException, Exception {\r
- InetAddress ipa = null;\r
- try {\r
- ipa = InetAddress.getByName(ipa_str);\r
- } catch (UnknownHostException UHEx) {\r
- throw UHEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try {\r
- tcpip_connect(ipa, port);\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- }\r
+ /**\r
+ * this method will read from DataInputStream dis, convert the chars read\r
+ * into ASCII and return its answer as a String\r
+ * \r
+ * @return\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized int readBuffer(int[] gelesen, int length)\r
+ throws IOException, Exception {\r
+ int i = 0; // init helper variables\r
+ // byte [] gelesen = new byte[length.intValue()];\r
+ // while (!dis.ready() && i++<20) {\r
+ // Thread.sleep(100);\r
+ // }\r
+ // i=0;\r
+ while ((dis.ready()) & (i < length)) { // loop while data available\r
+ if (gelesen != null)\r
+ gelesen[i++] = dis2.readUnsignedByte();// read from dis\r
+ else\r
+ dis2.readUnsignedByte();// read from dis\r
+ }\r
\r
- /**\r
- * this method will end the current connection to the XPort\r
- * \r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized void disconnect() throws IOException, Exception {\r
- try {\r
- tcpip_disconnect();\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- }\r
+ return i;\r
+ }\r
\r
- /**\r
- * uses a TCP/IP connection to send a String to the XPort\r
- * \r
- * @param sendString\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized void send(String sendString) throws IOException,\r
- Exception {\r
- System.out.println(sendString);\r
- try {\r
- dos.writeBytes(sendString);//, 0, sendString.length());\r
- // write data to dos as String starting at String[0]\r
- // and ending at String[String.length()]\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try {\r
- dos.flush(); // write dos to XPort\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- return;\r
- }\r
+ /**\r
+ * this method will read from DataInputStream dis, convert the chars read\r
+ * into ASCII and return its answer as a String\r
+ * \r
+ * @return\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized int readBuffer(byte[] gelesen, int length)\r
+ throws IOException, Exception {\r
+ int i = 0; // init helper variables\r
+ // byte [] gelesen = new byte[length.intValue()];\r
+ // while (!dis.ready() && i++<20) {\r
+ // Thread.sleep(100);\r
+ // }\r
+ // i=0;\r
+ while ((dis.ready()) & (i < length)) { // loop while data available\r
+ gelesen[i++] = (byte) dis2.readUnsignedByte();// read from dis\r
\r
- /**\r
- * uses a TCP/IP connection to send a String to the XPort\r
- * \r
- * @param sendByte\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized void send(int sendByte[]) throws IOException,\r
- Exception {\r
- int dummy;\r
- try {\r
- for (int i = 0; i < sendByte.length; i++) {\r
- dummy= sendByte[i] & 255;\r
- dos.write(dummy); // bytewise write data to dos\r
- }\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try {\r
- dos.flush(); // write dos to XPort\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
+ }\r
\r
- return;\r
- }\r
+ return i;\r
+ }\r
\r
- /**\r
- * closes the TCP/IP connection\r
- * \r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- private void tcpip_disconnect() throws IOException, Exception {\r
- if (socket.isConnected()) {\r
- try {\r
- dis.close(); // close input and output stream\r
- dos.close(); // as well as the socket\r
- socket.close();\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- }\r
- }\r
+ /**\r
+ * reads data supplied by the XPort and returns an array of bytes\r
+ * \r
+ * @param length\r
+ * @return\r
+ * @throws InterruptedIOException\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public byte[] receiveByteArray(int length) throws InterruptedIOException,\r
+ IOException, Exception {\r
+ byte[] antwort = { 0, // this method hardly differs from the string\r
+ 0, 0, 0, 0 }; // reading one, so same comments apply\r
+ try {\r
+ dis.ready();\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try {\r
+ antwort = readByteArrayBuffer(length);\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ return antwort;\r
+ }\r
\r
- /**\r
- * reads data supplied by the XPort and returns a String\r
- * \r
- * @return\r
- * @throws InterruptedIOException\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public String receiveString() throws InterruptedIOException, IOException,\r
- Exception {\r
- String antwort = "";\r
- int length=255;\r
- byte[] in = new byte[length];\r
- \r
- try {\r
- dis.ready(); // check if data is available (actually redundant since\r
- // the calling method from the GUI class already made\r
- // this sure but better safe than sorry... :-)\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- \r
- try {\r
- \r
- length = readBuffer(in,length); // call helper method to read from dis\r
- antwort= new String(in,0,length);\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- return antwort;\r
- }\r
+ /**\r
+ * this method will read from DataInputStream dis and return its answer as\r
+ * an array of bytes\r
+ * \r
+ * @param length\r
+ * @return\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public synchronized byte[] readByteArrayBuffer(int length)\r
+ throws IOException, Exception {\r
+ /*\r
+ * this method again hardly differs from the string reading one, so same\r
+ * comments apply here as well\r
+ */\r
\r
- /**\r
- * this method will read from DataInputStream dis, convert the chars read\r
- * into ASCII and return its answer as a String\r
- * \r
- * @return\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized int readBuffer(int[] gelesen,int length) throws IOException, Exception {\r
- int i=0; // init helper variables\r
- //byte [] gelesen = new byte[length.intValue()];\r
- while (!dis.ready() && i++<200) {\r
- Thread.sleep(100);\r
- }\r
- i=0;\r
- while ((dis.ready())& (i<length)){ // loop while data available\r
- if (gelesen!=null)\r
- gelesen[i++]=dis2.readUnsignedByte();// read from dis\r
- else \r
- dis2.readUnsignedByte();// read from dis\r
- }\r
- \r
- \r
- return i;\r
- }\r
+ int lesen = 0;\r
+ byte gelesen[] = new byte[length];\r
+ int i = 0;\r
+ int timeout = 0;\r
+ while (!dis.ready() && timeout < 8) { // wait max 2 seconds for data\r
+ try {\r
+ wait(250);\r
+ timeout++;\r
+ } catch (InterruptedException IntEx) {\r
+ }\r
+ }\r
\r
- /**\r
- * this method will read from DataInputStream dis, convert the chars read\r
- * into ASCII and return its answer as a String\r
- * \r
- * @return\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized int readBuffer(byte[] gelesen,int length) throws IOException, Exception {\r
- int i=0; // init helper variables\r
- //byte [] gelesen = new byte[length.intValue()];\r
- while (!dis.ready() && i++<200) {\r
- Thread.sleep(100);\r
- }\r
- i=0;\r
- while ((dis.ready())& (i<length)){ // loop while data available\r
- gelesen[i++]=(byte)dis2.readUnsignedByte();// read from dis\r
+ if (timeout == 8)\r
+ throw (new Exception("Timeout"));\r
\r
- }\r
- \r
- \r
- return i;\r
- }\r
- \r
- \r
- /**\r
- * reads data supplied by the XPort and returns an array of bytes\r
- * \r
- * @param length\r
- * @return\r
- * @throws InterruptedIOException\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public byte[] receiveByteArray(int length) throws InterruptedIOException,\r
- IOException, Exception {\r
- byte[] antwort = { 0, // this method hardly differs from the string\r
- 0, 0, 0, 0 }; // reading one, so same comments apply\r
- try {\r
- dis.ready();\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try {\r
- antwort = readByteArrayBuffer(length);\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- return antwort;\r
- }\r
+ while (dis.ready()) {\r
+ while (i < length) {\r
+ try {\r
+ // --Plymo-- 7.1.2005 "& 0xFF"\r
+ lesen = (dis.read()) & 0xFF;\r
+ gelesen[i] = (byte) lesen;\r
+ i++;\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ }\r
+ }\r
\r
- /**\r
- * this method will read from DataInputStream dis and return its answer as\r
- * an array of bytes\r
- * \r
- * @param length\r
- * @return\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public synchronized byte[] readByteArrayBuffer(int length)\r
- throws IOException, Exception {\r
- /*\r
- * this method again hardly differs from the string reading one, so same\r
- * comments apply here as well\r
- */\r
+ System.out.println("readBytearray Ende");\r
+ return gelesen;\r
+ }\r
\r
- int lesen = 0;\r
- byte gelesen[] = new byte[length];\r
- int i = 0;\r
- int timeout = 0;\r
- while (!dis.ready() && timeout < 8) { // wait max 2 seconds for data\r
- try {\r
- wait(250);\r
- timeout++;\r
- } catch (InterruptedException IntEx) {\r
- }\r
- }\r
- \r
- if (timeout ==8) throw(new Exception("Timeout"));\r
- \r
- while (dis.ready()) {\r
- while (i < length) {\r
- try {\r
- // --Plymo-- 7.1.2005 "& 0xFF"\r
- lesen = (dis.read()) & 0xFF;\r
- gelesen[i] = (byte) lesen;\r
- i++;\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- }\r
- }\r
- \r
- System.out.println("readBytearray Ende");\r
- return gelesen;\r
- }\r
+ /**\r
+ * this class does all the actual TCP/IP connection setup work\r
+ * \r
+ * @param ipa\r
+ * @param port\r
+ * @throws IOException\r
+ * @throws Exception\r
+ */\r
+ public void tcpip_connect(InetAddress ipa, int port) throws IOException,\r
+ Exception {\r
+ try {\r
+ // really establish a TCP connection (at last...)\r
+ socket = new Socket(ipa.getHostAddress(), port);\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try { // set up a buffered stream to read from XPort and write to PC\r
+ dis = new BufferedReader(new InputStreamReader(socket\r
+ .getInputStream()));\r
+ dis2 = new DataInputStream(socket.getInputStream());\r
+ } catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ try { // set up a buffered stream to read from PC and write to XPort\r
+ dos = new DataOutputStream(socket.getOutputStream());\r
\r
- /**\r
- * this class does all the actual TCP/IP connection setup work\r
- * \r
- * @param ipa\r
- * @param port\r
- * @throws IOException\r
- * @throws Exception\r
- */\r
- public void tcpip_connect(InetAddress ipa, int port) throws IOException,\r
- Exception {\r
- try {\r
- // really establish a TCP connection (at last...)\r
- socket = new Socket(ipa.getHostAddress(), port);\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try { // set up a buffered stream to read from XPort and write to PC\r
- dis = new BufferedReader(new InputStreamReader(socket\r
- .getInputStream()));\r
- dis2= new DataInputStream(socket.getInputStream());\r
- } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- try { // set up a buffered stream to read from PC and write to XPort\r
- dos= new DataOutputStream (socket.getOutputStream());\r
- \r
- /* dos = new BufferedWriter(new OutputStreamWriter(socket\r
- .getOutputStream()));\r
-*/ } catch (IOException IOEx) {\r
- throw IOEx;\r
- } catch (Exception Ex) {\r
- throw Ex;\r
- }\r
- }\r
+ /*\r
+ * dos = new BufferedWriter(new OutputStreamWriter(socket\r
+ * .getOutputStream()));\r
+ */} catch (IOException IOEx) {\r
+ throw IOEx;\r
+ } catch (Exception Ex) {\r
+ throw Ex;\r
+ }\r
+ }\r
\r
- /**\r
- * this method simply returns the value of DataInputStream dis' method\r
- * ready()\r
- * \r
- * @return\r
- * @throws Exception\r
- */\r
- public boolean DataReady() throws Exception {\r
- try {\r
- return dis.ready();\r
- } catch (Exception Ex) {\r
- return false;\r
- }\r
- }\r
+ /**\r
+ * this method simply returns the value of DataInputStream dis' method\r
+ * ready()\r
+ * \r
+ * @return\r
+ * @throws Exception\r
+ */\r
+ public boolean DataReady() throws Exception {\r
+ try {\r
+ return dis.ready();\r
+ } catch (Exception Ex) {\r
+ return false;\r
+ }\r
+ }\r
}\r