You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.1 KiB

5 years ago
  1. The following appears to work for Ken Eisner <019769e@acadiau.ca>
  2. -----
  3. under a windows platform we can check for lost of DSR and it is
  4. consistant however, under linux it is not consistant. We rarely and
  5. infrequently get loss of DSR.
  6. -----
  7. We found a way around the inconsistant
  8. detection / loss of DSR. We decided to check the DSR status in the
  9. OUTPUT_BUFFER_EMPTY event (I think that is where we put it).
  10. -----
  11. Trent, here is the code which works for us.
  12. Excuse the formating.. damned thing won't space things properly...
  13. Let me know if you need anything else
  14. -Ken
  15. /**
  16. * Procedure: void serialEvent(SerialPortEvent event)
  17. * Params: SerialPortEvent event
  18. *
  19. * Define what we are doing when one of these events are fired
  20. **/
  21. public void serialEvent(SerialPortEvent event)
  22. {
  23. int numBytes = 0;
  24. int offset = 0;
  25. switch(event.getEventType())
  26. {
  27. case SerialPortEvent.BI:
  28. case SerialPortEvent.OE:
  29. case SerialPortEvent.FE:
  30. case SerialPortEvent.PE:
  31. case SerialPortEvent.CD:
  32. case SerialPortEvent.RI:
  33. case SerialPortEvent.CTS:
  34. break;
  35. case SerialPortEvent.DSR:
  36. break;
  37. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  38. /*
  39. * HACK: This is designed to work around the inconsistent loss of
  40. DSR
  41. * and inconsistent detection of DSR if program running before
  42. * cable connected
  43. *
  44. * Here we check if the DSR flag is high.
  45. * If it is then we check to see if our class var is alread true,
  46. * meaning that we have a connection.
  47. * If our class var got_dsr is false then we set our class var
  48. * got_dsr to true, lost_dsr to false
  49. * Else the we already have a connection so just set the class
  50. var
  51. * got_dsr to true
  52. * Else we check if we already have DSR
  53. * If we do then we set the lost_dsr to true, and got_dsr to
  54. false
  55. * meaning that we lost DSR
  56. */
  57. if (serial_port_.isDSR() == true)
  58. {
  59. if (got_dsr == false) /* new connection */
  60. {
  61. got_dsr = true;
  62. System.out.println("\nReceived DSR -- BEGIN OF
  63. TRANSMISSION\n");
  64. lost_dsr = false;
  65. }
  66. else /* old connection */
  67. got_dsr = true;
  68. }
  69. else
  70. {
  71. /* already got connection */
  72. if (got_dsr == true)
  73. {
  74. /*
  75. * Check if we have a bad call
  76. *
  77. * 1.) Get the current time (in milliseconds)
  78. * 2.) If the current time is less than the time of the last
  79. * character read then we have major problems
  80. * 3.) Otherwise, if the current time less the time of the last
  81. * char read is less than the length of the end tone then we
  82. * have a bad call.
  83. */
  84. Date now = new Date();
  85. long current_time = now.getTime();
  86. if (current_time <= time_of_last_char)
  87. System.err.println("CommReader() - Current time is before
  88. last char");
  89. else
  90. {
  91. if ((current_time - time_of_last_char) <= end_tone_length_)
  92. {
  93. System.out.println("\nInterrupted transmission");
  94. this.passed_client_checks_ = false;
  95. }
  96. else
  97. {
  98. System.out.println("\nGood transmission");
  99. this.passed_client_checks_ = true;
  100. }
  101. }
  102. /*
  103. * Stick a fork in us, we're done here.
  104. */
  105. sender_.close_zipfile();
  106. /*
  107. * Set lost_dsr to true and got_dsr to false
  108. * Set finished_receiving_data to true
  109. */
  110. lost_dsr = true;
  111. System.out.println("Lost DSR -- END OF TRANSMISSION");
  112. got_dsr = false;
  113. this.finished_receiving_data_ = true;
  114. }
  115. }
  116. break;
  117. case SerialPortEvent.DATA_AVAILABLE:
  118. byte[] readBuffer = new byte[2048];
  119. try
  120. {
  121. /*
  122. * Read while there is something to read, stop when done
  123. */
  124. while (is_.available() > 0)
  125. {
  126. offset = 0;
  127. numBytes = is_.read(readBuffer);
  128. Date now = new Date();
  129. time_of_last_char = now.getTime();
  130. if (numBytes > 0)
  131. {
  132. /*
  133. * The first character detected is some weird ^@ thing which we
  134. * want to ignore so we set the offset to 1 and reduce the
  135. * numBytes read by one
  136. */
  137. if (readBuffer[0] == 0)
  138. {
  139. offset = 1;
  140. numBytes--;
  141. }
  142. System.out.print(new String (readBuffer , offset, numBytes));
  143. sender_.write(readBuffer, offset, numBytes);
  144. }
  145. }
  146. }
  147. catch (Exception e)
  148. {
  149. System.err.println(e.getMessage());
  150. e.printStackTrace();
  151. }
  152. break;
  153. } /* end switch */
  154. }