Advertisement
Guest User

Java simulation code

a guest
Apr 20th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Enumeration;
  5. import java.io.OutputStream;
  6. import gnu.io.*;
  7.  
  8. public class SerialGenerator {
  9.  
  10. private static final String PORT_NAME = "COM3";
  11. static SerialPort serialPort;
  12. private static BufferedReader input = null;
  13. private static OutputStream output = null;
  14. private static CommPortIdentifier portId;
  15. private static final String PORT_NAMES[] = {
  16. "/dev/tty.usbserial-A9007UX1", // Mac OS X
  17. "/dev/ttyACM0", // Raspberry Pi
  18. "/dev/ttyUSB0", // Linux
  19. "COM3", // Windows
  20. };
  21. private static final int TIME_OUT = 2000;
  22. /** Default bits per second for COM port. */
  23. private static final int DATA_RATE = 9600;
  24.  
  25.  
  26. public static void main(String[] args) throws IOException, UnsupportedCommOperationException, InterruptedException {
  27.  
  28. setUpPort();
  29. System.out.print("Enter goal speed:\t");
  30. float goalSpeed = getInput();
  31. System.out.print("Enter current speed:\t");
  32. float currentSpeed = getInput();
  33. simulation( goalSpeed, currentSpeed );
  34.  
  35. }
  36.  
  37. private static float getInput() throws IOException {
  38. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  39.  
  40. return new Float(reader.readLine());
  41. }
  42.  
  43. private static void setUpPort() throws IOException, UnsupportedCommOperationException {
  44.  
  45. Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
  46.  
  47. //First, Find an instance of serial port as set in PORT_NAMES.
  48. while (portEnum.hasMoreElements()) {
  49. CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  50. for (String portName : PORT_NAMES) {
  51. if (currPortId.getName().equals(portName)) {
  52. portId = currPortId;
  53. break;
  54. }
  55. }
  56. }
  57. try {
  58. serialPort = (SerialPort) portId.open(PORT_NAME, TIME_OUT);
  59. } catch (PortInUseException e) {
  60. System.err.println("Port not connected!");
  61. e.printStackTrace();
  62. }
  63. serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  64. input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
  65. output = serialPort.getOutputStream();
  66.  
  67. }
  68.  
  69. private static void simulation( float goalSpeed, float currentSpeed ) throws IOException, InterruptedException {
  70.  
  71. while( true ) {
  72. if ( currentSpeed > 18 ) {
  73. goalSpeed = 1;
  74. }
  75. String outputString = goalSpeed + "," + currentSpeed;
  76. System.out.println("Sending: " + outputString);
  77. System.out.println();
  78. output.write(outputString.getBytes());
  79. System.out.println("Return Value: ");
  80. try {
  81. String inputRet = input.readLine();
  82. System.out.println(inputRet);
  83. } catch ( NullPointerException e ) {
  84. System.out.println("No return value!" );
  85. } catch ( IOException e ) {
  86. System.out.println("No return value!" );
  87. }
  88.  
  89. finally {
  90. currentSpeed += 1;
  91. Thread.sleep(1500);
  92. }
  93.  
  94. }
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement