123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package QuickVerifyCrap;
- import com.fazecast.jSerialComm.SerialPort;
- import javax.sql.rowset.serial.SQLOutputImpl;
- import java.io.*;
- enum NextChunkAction
- {
- WAIT,
- REPEAT,
- CONTINUE,
- FINISHED
- }
- public class SerialSpammer
- {
- static InputStream in = null;
- static OutputStream o = null;
- static NextChunkAction nextChunkReady = NextChunkAction.WAIT;
- static int waitCtr = 0;
- static Object workaround = new Object();
- public static void main(String[] args)
- {
- boolean startedStreamPlayback = false;
- System.out.println("Gobblygob");
- RandomAccessFile streamfile = null;
- int sendOffset = 0;
- SerialPort comPort = SerialPort.getCommPort("COM20");
- comPort.openPort();
- comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
- in = comPort.getInputStream();
- o = comPort.getOutputStream();
- try
- {
- new Thread(()->
-
- {
- System.out.println("MunchMunchMunch");
- try
- {
- while (true)
- {
- if(in.available()<1)
- {
- continue;
- }
- int read = in.read();
- if (read == '~')
- {
- System.out.println("RSME");
- nextChunkReady = NextChunkAction.CONTINUE;
- synchronized (workaround)
- {
- System.out.println("POKE");
- workaround.notify();
- }
- }
- if (read == '<')
- {
- nextChunkReady = NextChunkAction.REPEAT;
- synchronized (workaround)
- {
- System.out.println("POKE");
- workaround.notify();
- }
- }
- System.out.write(read);
- System.out.flush();
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }).start();
-
-
- while(true)
- {
- if(System.in.available()>0)
- {
- int chara = System.in.read();
- if(chara ==-1)
- {
- System.out.println("ZÖINKS");
- return;
- }
- if(chara == '>')
- {
- char[] filepath = new char[4096];
- int pathptr = 0;
- chara = System.in.read();
- while(chara != '\n'&&pathptr<4096)
- {
- filepath[pathptr]=(char)chara;
- pathptr++;
- chara = System.in.read();
- }
- if(pathptr==4096)
- {
- System.out.println("Zarf, pathdork");
- }
- else
- {
- String path = new String(filepath).split("\u0000")[0];
- System.out.println("Streaming: "+path);
- if(new File(path).exists())
- {
- streamfile = new RandomAccessFile(path,"r");
- nextChunkReady=NextChunkAction.CONTINUE;
- synchronized (workaround)
- {
- System.out.println("POKE");
- workaround.notify();
- }
- }
- }
- }
- else
- {
- //System.out.println("Fuick");
- o.write(chara);
- o.flush();
- if(chara=='\n')
- {
- System.out.println("waitctr="+waitCtr);
- }
- //System.out.println("ZilseZilse");
- }
- }
- if(streamfile!=null)
- {
- if(streamfile.getFilePointer()>=streamfile.length())
- {
- System.out.println("FINI");
- System.err.println("FINI");
- streamfile.close();
- streamfile=null;
- startedStreamPlayback=false;
- }
- else
- {
- if((!startedStreamPlayback)&&(streamfile.getFilePointer()==8192||streamfile.getFilePointer()==streamfile.length()))
- {
- System.out.println("PLAY");
- o.write('|');
- startedStreamPlayback=true;
- }
- switch(nextChunkReady)
- {
- case REPEAT:
- System.out.println("RTRY=("+streamfile.getFilePointer()+")");
- waitCtr=0;
- if(streamfile.getFilePointer()>=64)
- {
- streamfile.seek(streamfile.getFilePointer()-64);
- }
- case CONTINUE:
- waitCtr=0;
- System.out.println("STR=("+streamfile.getFilePointer()+")");
- byte[] readbfr = new byte[64];
- streamfile.read(readbfr);
- o.write('>');
- nextChunkReady=NextChunkAction.WAIT;
- o.write(readbfr);
- break;
- }
- synchronized (workaround)
- {
- if(nextChunkReady==NextChunkAction.WAIT)
- {
- System.out.println("GOTO WAIT");
- workaround.wait();
- }
- }
- }
- }
- }
- }
- catch (Exception e) { e.printStackTrace(); }
- comPort.closePort();
- }
- }
|