IO.java 3.6 KB

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