Guest User

Untitled

a guest
Apr 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. import gnu.io.CommPortIdentifier;
  2.  
  3. import gnu.io.PortInUseException;
  4.  
  5. import gnu.io.SerialPort;
  6.  
  7. import gnu.io.SerialPortEvent;
  8.  
  9. import gnu.io.SerialPortEventListener;
  10.  
  11. import gnu.io.UnsupportedCommOperationException;
  12.  
  13.  
  14.  
  15. import java.awt.event.KeyEvent;
  16.  
  17. import java.awt.event.KeyListener;
  18.  
  19. import java.io.IOException;
  20.  
  21. import java.io.InputStream;
  22.  
  23. import java.io.OutputStream;
  24.  
  25. import java.util.Enumeration;
  26.  
  27. import java.util.TooManyListenersException;
  28.  
  29. import java.lang.Boolean;
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. public class DJController implements KeyListener, SerialPortEventListener {
  38.  
  39. static CommPortIdentifier portId;
  40.  
  41. static CommPortIdentifier saveportId;
  42.  
  43.  
  44.  
  45. static Enumeration portList;
  46.  
  47. InputStream inputStream;
  48.  
  49. SerialPort serialPort;
  50.  
  51. Thread readThread;
  52.  
  53.  
  54.  
  55. static OutputStream outputStream;
  56.  
  57. static boolean outputBufferEmptyFlag = true;
  58.  
  59.  
  60.  
  61. private int baudrate;
  62.  
  63. private int stopbits;
  64.  
  65. private String portname;
  66.  
  67.  
  68.  
  69. private boolean forward = false;
  70.  
  71. private boolean back = false;
  72.  
  73. private boolean left = false;
  74.  
  75. private boolean right = false;
  76.  
  77. private boolean sendNewCmd = false;
  78.  
  79.  
  80.  
  81. static final int MANUAL_FORWARD = 0x00;
  82.  
  83. static final int MANUAL_FORWARD_RIGHT = 0x01;
  84.  
  85. static final int MANUAL_FORWARD_LEFT = 0x02;
  86.  
  87. static final int MANUAL_ROTATE_RIGHT = 0x03;
  88.  
  89. static final int MANUAL_ROTATE_LEFT = 0x04;
  90.  
  91. static final int MANUAL_BACKWARD = 0x05;
  92.  
  93. static final int MANUAL_STOP = 0x06;
  94.  
  95. static final int MANUAL_SEIZE = 0x07;
  96.  
  97. static final int MANUAL_RELEASE = 0x08;
  98.  
  99. static final int MANUAL_AUTONOM = 0x09;
  100.  
  101.  
  102.  
  103.  
  104.  
  105. public DJController(String portname, int baudrate, int stopbits)
  106.  
  107. {
  108.  
  109. System.out.println("DJController skapad");
  110.  
  111. boolean portFound = false;
  112.  
  113.  
  114.  
  115. if (stopbits > 0 && stopbits < 3)
  116.  
  117. this.stopbits = stopbits;
  118.  
  119. else
  120.  
  121. this.stopbits = SerialPort.STOPBITS_1;
  122.  
  123.  
  124.  
  125. if (baudrate > 0)
  126.  
  127. this.baudrate = baudrate;
  128.  
  129. else
  130.  
  131. this.baudrate = 38400;
  132.  
  133.  
  134.  
  135. this.portname = portname;
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. System.out.println("Setting default port to "+portname);
  144.  
  145.  
  146.  
  147. // parse ports and if the default port is found, initialized the reader
  148.  
  149. portList = CommPortIdentifier.getPortIdentifiers();
  150.  
  151. while (portList.hasMoreElements()) {
  152.  
  153. portId = (CommPortIdentifier) portList.nextElement();
  154.  
  155. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  156.  
  157. if (portId.getName().equals(portname)) {
  158.  
  159. System.out.println("Found port: "+portname);
  160.  
  161. portFound = true;
  162.  
  163. // init reader thread
  164.  
  165. initcomm();
  166.  
  167. break;
  168.  
  169. }
  170.  
  171. }
  172.  
  173.  
  174.  
  175. }
  176.  
  177. if (!portFound) {
  178.  
  179. System.out.println("Port '" + portname + "' not found.");
  180.  
  181. }
  182.  
  183. }
  184.  
  185.  
  186.  
  187. public void initcomm() {
  188.  
  189. // initalize serial port
  190.  
  191. try {
  192.  
  193. serialPort = (SerialPort) portId.open("AppFrame", 50);
  194.  
  195. } catch (PortInUseException e) {
  196.  
  197. System.out.println("Port in use!");
  198.  
  199. }
  200.  
  201.  
  202.  
  203. try {
  204.  
  205. inputStream = serialPort.getInputStream();
  206.  
  207. } catch (IOException e) {
  208.  
  209. System.out.println("IO error (inputstream??)");
  210.  
  211. }
  212.  
  213.  
  214.  
  215. try {
  216.  
  217. serialPort.addEventListener(this);
  218.  
  219. } catch (TooManyListenersException e) {
  220.  
  221. System.out.println("Too many listeners(?)");
  222.  
  223. }
  224.  
  225.  
  226.  
  227. // activate the DATA_AVAILABLE notifier
  228.  
  229. serialPort.notifyOnDataAvailable(true);
  230.  
  231.  
  232.  
  233. System.out.println("Configuring "+this.portname+" with "+this.stopbits+" stop bits and "+this.baudrate+" baud rate.");
  234.  
  235. try {
  236.  
  237. // set port parameters
  238.  
  239. serialPort.setSerialPortParams(this.baudrate, SerialPort.DATABITS_8,
  240.  
  241. this.stopbits,
  242.  
  243. SerialPort.PARITY_NONE);
  244.  
  245. } catch (UnsupportedCommOperationException e) {
  246.  
  247. System.out.println("Unsupported operation: " + e.getMessage());
  248.  
  249. }
  250.  
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257. public void initwritetoport() {
  258.  
  259. // initwritetoport() assumes that the port has already been opened and
  260.  
  261. // initialized by "public nulltest()"
  262.  
  263.  
  264.  
  265. try {
  266.  
  267. // get the outputstream
  268.  
  269. outputStream = serialPort.getOutputStream();
  270.  
  271. } catch (Exception e) {
  272.  
  273. System.out.println("No outputstream?!");
  274.  
  275. }
  276.  
  277.  
  278.  
  279. try {
  280.  
  281. // activate the OUTPUT_BUFFER_EMPTY notifier
  282.  
  283. serialPort.notifyOnOutputEmpty(true);
  284.  
  285. } catch (Exception e) {
  286.  
  287. System.out.println("Error setting event notification");
  288.  
  289. System.out.println(e.toString());
  290.  
  291. System.exit(-1);
  292.  
  293. }
  294.  
  295. }
  296.  
  297.  
  298.  
  299. public void writetoport(String msg) {
  300.  
  301. System.out.println("Writing \""+msg+"\" to "+serialPort.getName());
  302.  
  303. if (outputStream == null)
  304.  
  305. {
  306.  
  307. System.out.println("Outputstream är null?!");
  308.  
  309. } else {
  310.  
  311. try {
  312.  
  313. // write string to serial port
  314.  
  315. outputStream.write(msg.getBytes());
  316.  
  317. } catch (Exception e) {
  318.  
  319. e.printStackTrace();
  320.  
  321. }
  322.  
  323. }
  324.  
  325. }
  326.  
  327. public void serialEvent(SerialPortEvent event) {
  328.  
  329. switch (event.getEventType()) {
  330.  
  331. case SerialPortEvent.BI:
  332.  
  333. case SerialPortEvent.OE:
  334.  
  335. case SerialPortEvent.FE:
  336.  
  337. case SerialPortEvent.PE:
  338.  
  339. case SerialPortEvent.CD:
  340.  
  341. case SerialPortEvent.CTS:
  342.  
  343. case SerialPortEvent.DSR:
  344.  
  345. case SerialPortEvent.RI:
  346.  
  347. case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
  348.  
  349. break;
  350.  
  351. case SerialPortEvent.DATA_AVAILABLE:
  352.  
  353. // we get here if data has been received
  354.  
  355. byte[] readBuffer = new byte[20];
  356.  
  357. try {
  358.  
  359. // read data
  360.  
  361. while (inputStream.available() > 0) {
  362.  
  363. int numBytes = inputStream.read(readBuffer);
  364.  
  365. }
  366.  
  367. // print data
  368.  
  369. String result = new String(readBuffer);
  370.  
  371. System.out.println("Read: "+result);
  372.  
  373. } catch (IOException e) {}
  374.  
  375.  
  376.  
  377. break;
  378.  
  379. }
  380.  
  381. }
  382.  
  383.  
  384.  
  385.  
  386.  
  387. public void update()
  388.  
  389. {
  390.  
  391. if (sendNewCmd == true) {
  392.  
  393. if (forward == true && left == true)
  394.  
  395. commSend(MANUAL_FORWARD_LEFT);
  396.  
  397. else if (forward == true && right == true)
  398.  
  399. commSend(MANUAL_FORWARD_RIGHT);
  400.  
  401. else if (forward)
  402.  
  403. commSend(MANUAL_FORWARD);
  404.  
  405. else if (back)
  406.  
  407. commSend(MANUAL_BACKWARD);
  408.  
  409. else if (right)
  410.  
  411. commSend(MANUAL_ROTATE_RIGHT);
  412.  
  413. else if (left)
  414.  
  415. commSend(MANUAL_ROTATE_LEFT);
  416.  
  417. else
  418.  
  419. commSend(MANUAL_STOP);
  420.  
  421.  
  422.  
  423. sendNewCmd = false;
  424.  
  425. }
  426.  
  427.  
  428.  
  429. }
  430.  
  431. public void stopDJ()
  432.  
  433. {
  434.  
  435. commSend(MANUAL_STOP);
  436.  
  437. }
  438.  
  439. public void commSend(int cmd)
  440.  
  441. {
  442.  
  443. try {
  444.  
  445. writetoport(new String(String.valueOf(cmd)));
  446.  
  447. } catch(Exception e) {
  448.  
  449. System.err.println("Oups, writeport gick inge bra. Saknar COM4?");
  450.  
  451. }
  452.  
  453.  
  454.  
  455. }
  456.  
  457. public void keyPressed(KeyEvent e)
  458.  
  459. {
  460.  
  461. int key = e.getKeyCode();
  462.  
  463. boolean updated = false;
  464.  
  465. // sv‰ng
  466.  
  467. if (key == KeyEvent.VK_LEFT && !left)
  468.  
  469. {
  470.  
  471. updated = true;
  472.  
  473. left = true;
  474.  
  475. }
  476.  
  477. if (key == KeyEvent.VK_RIGHT && !right)
  478.  
  479. {
  480.  
  481. updated = true;
  482.  
  483. right = true;
  484.  
  485. }
  486.  
  487.  
  488.  
  489. // frambak
  490.  
  491. if (key == KeyEvent.VK_UP && !forward)
  492.  
  493. {
  494.  
  495. updated = true;
  496.  
  497. forward = true;
  498.  
  499. }
  500.  
  501. if (key == KeyEvent.VK_DOWN && !back)
  502.  
  503. {
  504.  
  505. updated = true;
  506.  
  507. back = true;
  508.  
  509. }
  510.  
  511.  
  512.  
  513. if (updated)
  514.  
  515. {
  516.  
  517. sendNewCmd = true;
  518.  
  519. }
  520.  
  521. }
  522.  
  523. public void keyReleased(KeyEvent e)
  524.  
  525. {
  526.  
  527. int key = e.getKeyCode();
  528.  
  529.  
  530.  
  531. // sv‰ng
  532.  
  533. if (key == KeyEvent.VK_LEFT)
  534.  
  535. left = false;
  536.  
  537. if (key == KeyEvent.VK_RIGHT)
  538.  
  539. right = false;
  540.  
  541.  
  542.  
  543. // frambak
  544.  
  545. if (key == KeyEvent.VK_UP)
  546.  
  547. forward = false;
  548.  
  549. if (key == KeyEvent.VK_DOWN)
  550.  
  551. back = false;
  552.  
  553.  
  554.  
  555. sendNewCmd = true;
  556.  
  557. }
  558.  
  559.  
  560.  
  561.  
  562.  
  563. public void keyTyped(KeyEvent e)
  564.  
  565. {
  566.  
  567. /*if (e.getKeyCode() == KeyEvent.VK_SPACE)
  568.  
  569. {
  570.  
  571. stopDJ();
  572.  
  573. }*/
  574.  
  575. if (e.getKeyCode() == KeyEvent.VK_Y) {
  576.  
  577. commSend(MANUAL_AUTONOM);
  578.  
  579. }
  580.  
  581. }
  582.  
  583.  
  584.  
  585. }
Add Comment
Please, Sign In to add comment