603b73fbe2b000171eb4aea94a5e3c6b 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import java.io.DataOutputStream;
  2. import java.io.IOException;
  3. public class SendThread extends Thread{
  4. public static final byte FORWARD = 0;
  5. public static final byte STOP = 1;
  6. public static final byte EXIT = 2;
  7. public static final byte LEFT = 3;
  8. public static final byte RIGHT = 4;
  9. public static final byte BACKWARD = 5;
  10. private int command;
  11. private int before;
  12. private boolean run;
  13. private DataOutputStream output;
  14. public SendThread(DataOutputStream out) {
  15. super();
  16. command = -1;
  17. before = command;
  18. run = true;
  19. output = out;
  20. }
  21. @Override
  22. public void run() {
  23. while (run) {
  24. if (before != command) {
  25. try {
  26. output.write(command);
  27. output.flush();
  28. System.out.println("Send cmd");
  29. } catch (IOException e) {
  30. System.out.println("Erro send cmd");
  31. }
  32. before = command;
  33. }
  34. try {
  35. Thread.sleep(200);
  36. } catch (InterruptedException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. public void send (int c) {
  43. command = c;
  44. }
  45. }