IO.java 3.8 KB

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