Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. // *********************************************
  2.  
  3. import java.awt.AWTException;
  4. import java.awt.Robot;
  5. import java.awt.event.KeyEvent;
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import gnu.io.CommPortIdentifier;
  10. import gnu.io.SerialPort;
  11. import gnu.io.SerialPortEvent;
  12. import gnu.io.SerialPortEventListener;
  13. import java.util.Enumeration;
  14. import javax.swing.*;
  15. import javax.swing.event.*;
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import javax.swing.JFrame;
  19. import java.awt.Color;
  20.  
  21. public class SerialTest implements SerialPortEventListener {
  22. SerialPort serialPort;
  23. /** The port we're normally going to use. */
  24. private static final String PORT_NAMES[] = { "/dev/tty.usbserial-A9007UX1", // Mac OS X
  25. "/dev/ttyUSB0", // Linux
  26. "COM1", // Windows
  27. };
  28. /**
  29. * A BufferedReader which will be fed by a InputStreamReader converting the
  30. * bytes into characters making the displayed results codepage independent
  31. */
  32. private BufferedReader input;
  33. /** The output stream to the port */
  34. private OutputStream output;
  35. /** Milliseconds to block while waiting for port open */
  36. private static final int TIME_OUT = 2000;
  37. /** Default bits per second for COM port. */
  38. private static final int DATA_RATE = 9600;
  39.  
  40. public void initialize() {
  41. CommPortIdentifier portId = null;
  42. Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  43.  
  44. // First, Find an instance of serial port as set in PORT_NAMES.
  45. while (portEnum.hasMoreElements()) {
  46. CommPortIdentifier currPortId = (CommPortIdentifier) portEnum
  47. .nextElement();
  48. for (String portName : PORT_NAMES) {
  49. if (currPortId.getName().equals(portName)) {
  50. portId = currPortId;
  51. break;
  52. }
  53. }
  54. }
  55. System.out.println("Port ID: ");
  56. System.out.println(portId);
  57. System.out.println("");
  58. if (portId == null) {
  59. System.out.println("Could not find COM port.");
  60. return;
  61. }
  62.  
  63. try {
  64. // open serial port, and use class name for the appName.
  65. serialPort = (SerialPort) portId.open(this.getClass().getName(),
  66. TIME_OUT);
  67.  
  68. // set port parameters
  69. serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8,
  70. SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  71.  
  72. // open the streams
  73. input = new BufferedReader(new InputStreamReader(
  74. serialPort.getInputStream()));
  75. output = serialPort.getOutputStream();
  76.  
  77. // add event listeners
  78. serialPort.addEventListener(this);
  79. serialPort.notifyOnDataAvailable(true);
  80. } catch (Exception e) {
  81. System.err.println(e.toString());
  82. }
  83. }
  84.  
  85. /**
  86. * This should be called when you stop using the port. This will prevent
  87. * port locking on platforms like Linux.
  88. */
  89. public synchronized void close() {
  90. if (serialPort != null) {
  91. serialPort.removeEventListener();
  92. serialPort.close();
  93. }
  94. }
  95.  
  96. /**
  97. * Handle an event on the serial port. Read the data and print it.
  98. */
  99. public synchronized void serialEvent(SerialPortEvent oEvent) {
  100. if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  101. try {
  102. String inputLine = input.readLine();
  103. // ENGINE START
  104. if (inputLine.equals("Start Engine")) {
  105. System.out.println("Engine Start Engaged");
  106. try {
  107. Robot robot = new Robot();
  108. robot.keyPress(KeyEvent.VK_S);
  109. robot.delay(500);
  110. robot.keyRelease(KeyEvent.VK_S);
  111. } catch (AWTException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. // WINDSHIELD WIPERS
  116. if (inputLine.equals("Windshield Wipers")) {
  117. System.out.println("Windshield Wipers Engaged");
  118. try {
  119. Robot robot = new Robot();
  120. robot.keyPress(KeyEvent.VK_W);
  121. robot.delay(500);
  122. robot.keyRelease(KeyEvent.VK_W);
  123. } catch (AWTException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. // PIT SPEED LIMITER
  128. if (inputLine.equals("Pit Speed Limiter")) {
  129. System.out.println("Pit Limiter Engaged");
  130. try {
  131. Robot robot = new Robot();
  132. robot.keyPress(KeyEvent.VK_P);
  133. robot.delay(500);
  134. robot.keyRelease(KeyEvent.VK_P);
  135. } catch (AWTException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. // HEADLIGHTS
  140. if (inputLine.equals("Headlights")) {
  141. System.out.println("Headlights Engaged");
  142. try {
  143. Robot robot = new Robot();
  144. robot.keyPress(KeyEvent.VK_H);
  145. robot.delay(500);
  146. robot.keyRelease(KeyEvent.VK_H);
  147. } catch (AWTException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151. } catch (Exception e) {
  152. System.err.println(e.toString());
  153. }
  154. }
  155. if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
  156.  
  157. }
  158. // Ignore all the other eventTypes, but you should consider the other
  159. // ones.
  160. }
  161.  
  162. public static void main(String[] args) throws Exception {
  163. SerialTest main = new SerialTest();
  164. main.initialize();
  165. Thread t = new Thread() {
  166. public void run() {
  167. // the following line will keep this app alive for 1000 seconds,
  168. // waiting for events to occur and responding to them (printing
  169. // incoming messages to console).
  170. try {
  171. Thread.sleep(1000000);
  172. } catch (InterruptedException ie) {
  173. }
  174. }
  175. };
  176. t.start();
  177. System.out.println("- Started -");
  178. System.out.println("");
  179. }
  180. }
  181.  
  182. // *********************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement