SerialSpammer.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package QuickVerifyCrap;
  2. import com.fazecast.jSerialComm.SerialPort;
  3. import javax.sql.rowset.serial.SQLOutputImpl;
  4. import java.io.*;
  5. enum NextChunkAction
  6. {
  7. WAIT,
  8. REPEAT,
  9. CONTINUE,
  10. FINISHED
  11. }
  12. public class SerialSpammer
  13. {
  14. static InputStream in = null;
  15. static OutputStream o = null;
  16. static NextChunkAction nextChunkReady = NextChunkAction.WAIT;
  17. static int waitCtr = 0;
  18. static Object workaround = new Object();
  19. public static void main(String[] args)
  20. {
  21. boolean startedStreamPlayback = false;
  22. System.out.println("Gobblygob");
  23. RandomAccessFile streamfile = null;
  24. int sendOffset = 0;
  25. SerialPort comPort = SerialPort.getCommPort("COM20");
  26. comPort.openPort();
  27. comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
  28. in = comPort.getInputStream();
  29. o = comPort.getOutputStream();
  30. try
  31. {
  32. new Thread(()->
  33. {
  34. System.out.println("MunchMunchMunch");
  35. try
  36. {
  37. while (true)
  38. {
  39. if(in.available()<1)
  40. {
  41. continue;
  42. }
  43. int read = in.read();
  44. if (read == '~')
  45. {
  46. System.out.println("RSME");
  47. nextChunkReady = NextChunkAction.CONTINUE;
  48. synchronized (workaround)
  49. {
  50. System.out.println("POKE");
  51. workaround.notify();
  52. }
  53. }
  54. if (read == '<')
  55. {
  56. nextChunkReady = NextChunkAction.REPEAT;
  57. synchronized (workaround)
  58. {
  59. System.out.println("POKE");
  60. workaround.notify();
  61. }
  62. }
  63. System.out.write(read);
  64. System.out.flush();
  65. }
  66. }
  67. catch (IOException e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }).start();
  72. while(true)
  73. {
  74. if(System.in.available()>0)
  75. {
  76. int chara = System.in.read();
  77. if(chara ==-1)
  78. {
  79. System.out.println("ZÖINKS");
  80. return;
  81. }
  82. if(chara == '>')
  83. {
  84. char[] filepath = new char[4096];
  85. int pathptr = 0;
  86. chara = System.in.read();
  87. while(chara != '\n'&&pathptr<4096)
  88. {
  89. filepath[pathptr]=(char)chara;
  90. pathptr++;
  91. chara = System.in.read();
  92. }
  93. if(pathptr==4096)
  94. {
  95. System.out.println("Zarf, pathdork");
  96. }
  97. else
  98. {
  99. String path = new String(filepath).split("\u0000")[0];
  100. System.out.println("Streaming: "+path);
  101. if(new File(path).exists())
  102. {
  103. streamfile = new RandomAccessFile(path,"r");
  104. nextChunkReady=NextChunkAction.CONTINUE;
  105. synchronized (workaround)
  106. {
  107. System.out.println("POKE");
  108. workaround.notify();
  109. }
  110. }
  111. }
  112. }
  113. else
  114. {
  115. //System.out.println("Fuick");
  116. o.write(chara);
  117. o.flush();
  118. if(chara=='\n')
  119. {
  120. System.out.println("waitctr="+waitCtr);
  121. }
  122. //System.out.println("ZilseZilse");
  123. }
  124. }
  125. if(streamfile!=null)
  126. {
  127. if(streamfile.getFilePointer()>=streamfile.length())
  128. {
  129. System.out.println("FINI");
  130. System.err.println("FINI");
  131. streamfile.close();
  132. streamfile=null;
  133. startedStreamPlayback=false;
  134. }
  135. else
  136. {
  137. if((!startedStreamPlayback)&&(streamfile.getFilePointer()==8192||streamfile.getFilePointer()==streamfile.length()))
  138. {
  139. System.out.println("PLAY");
  140. o.write('|');
  141. startedStreamPlayback=true;
  142. }
  143. switch(nextChunkReady)
  144. {
  145. case REPEAT:
  146. System.out.println("RTRY=("+streamfile.getFilePointer()+")");
  147. waitCtr=0;
  148. if(streamfile.getFilePointer()>=64)
  149. {
  150. streamfile.seek(streamfile.getFilePointer()-64);
  151. }
  152. case CONTINUE:
  153. waitCtr=0;
  154. System.out.println("STR=("+streamfile.getFilePointer()+")");
  155. byte[] readbfr = new byte[64];
  156. streamfile.read(readbfr);
  157. o.write('>');
  158. nextChunkReady=NextChunkAction.WAIT;
  159. o.write(readbfr);
  160. break;
  161. }
  162. synchronized (workaround)
  163. {
  164. if(nextChunkReady==NextChunkAction.WAIT)
  165. {
  166. System.out.println("GOTO WAIT");
  167. workaround.wait();
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. catch (Exception e) { e.printStackTrace(); }
  175. comPort.closePort();
  176. }
  177. }