IO.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package weerstation1;
  2. //
  3. // Port IO emulator class. All read/writes are send over TCP/IP to the host
  4. // using a dedicated protocol on port 81.
  5. // Use IO.init(host, port) to modify behaviour
  6. // by Wim Verhoef
  7. // Avans Hogeschool 's-Hertogenbosch
  8. //
  9. // @author Wim Verhoef
  10. // @version 1.1, December 6 2004
  11. //
  12. import java.io.*;
  13. import java.net.*;
  14. public class IO {
  15. public static String host = "localhost";
  16. public static int port = 81;
  17. /**
  18. * Initialize.
  19. * Before calling the emulated IO routines, use one of the init routines
  20. * to set the host name and port number of the server emulating the HW.
  21. **/
  22. public static boolean init() {
  23. return (init("localhost", 81));
  24. }
  25. public static boolean init(String newHost) {
  26. return (init(newHost, 81));
  27. }
  28. public static boolean init(int newPort) {
  29. return (init("localhost", newPort));
  30. }
  31. public static boolean init(String newHost, int newPort) {
  32. host = newHost;
  33. port = newPort;
  34. return startConnectionToServer();
  35. }
  36. /**
  37. * Read a short value (16-bits) from an I/O port.
  38. * @param address port address (16-bits)
  39. * @return short value (16-bits)
  40. **/
  41. public static short readShort(short address) {
  42. return remoteIO( (short) 0, address, (short) 0);
  43. }
  44. public static short readShort(int address) {
  45. return remoteIO( (short) 0, (short) address, (short) 0);
  46. }
  47. /**
  48. * Write a short value (16-bits) to an I/O port.
  49. * @param address port address (16-bits)
  50. * @param value short value (16-bits)
  51. **/
  52. public static void writeShort(short address, short value) {
  53. remoteIO( (short) 1, address, value);
  54. }
  55. public static void writeShort(int address, int value) {
  56. remoteIO( (short) 1, (short) address, (short) value);
  57. }
  58. /**
  59. * Wait a specified time. The routine uses Thread.sleep so below 20 ms
  60. * timing becomes very erraneous, but works fine for longer times
  61. *
  62. * @param lMilliSeconds number of milliseconds to wait
  63. */
  64. public static void delay(long lMilliSeconds) {
  65. if (lMilliSeconds > 0) {
  66. try {
  67. Thread.sleep(lMilliSeconds);
  68. }
  69. catch (InterruptedException ex) {
  70. }
  71. }
  72. }
  73. //============================= below are the support routines =====================
  74. private static Socket client = null;
  75. private static DataOutputStream output = null;
  76. private static DataInputStream input = null;
  77. private static void closeConnectionToServer() {
  78. try {
  79. if (output != null)
  80. output.close();
  81. if (input != null)
  82. input.close();
  83. if (client != null)
  84. client.close();
  85. }
  86. catch (IOException e) {
  87. // no errors plz. e.printStackTrace();
  88. }
  89. }
  90. private static boolean startConnectionToServer() {
  91. closeConnectionToServer(); // close any pending connection
  92. try {
  93. client = new Socket(InetAddress.getByName(host), port);
  94. client.setSoTimeout(2000); // we expect answers within 2 seconds
  95. client.setTcpNoDelay(true); //disable nagles algorithm for short packets
  96. output = new DataOutputStream(new BufferedOutputStream(client.
  97. getOutputStream()));
  98. input = new DataInputStream(new BufferedInputStream(client.getInputStream()));
  99. }
  100. catch (Exception e) {
  101. //e.printStackTrace();
  102. client = null;
  103. return false;
  104. }
  105. return true;
  106. }
  107. private static short remoteIO(short IOCode, short address, short value) {
  108. if (client == null)
  109. return (short) - 1;
  110. short readvalue = (short) 0;
  111. try {
  112. output.writeShort(IOCode);
  113. output.writeShort(address);
  114. output.writeShort(value);
  115. output.flush();
  116. readvalue = input.readShort();
  117. }
  118. catch (Exception e) {
  119. // no errors plz. e.printStackTrace();
  120. }
  121. return readvalue;
  122. }
  123. }