Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Enumeration;
- import java.io.OutputStream;
- import gnu.io.*;
- public class SerialGenerator {
- private static final String PORT_NAME = "COM3";
- static SerialPort serialPort;
- private static BufferedReader input = null;
- private static OutputStream output = null;
- private static CommPortIdentifier portId;
- private static final String PORT_NAMES[] = {
- "/dev/tty.usbserial-A9007UX1", // Mac OS X
- "/dev/ttyACM0", // Raspberry Pi
- "/dev/ttyUSB0", // Linux
- "COM3", // Windows
- };
- private static final int TIME_OUT = 2000;
- /** Default bits per second for COM port. */
- private static final int DATA_RATE = 9600;
- public static void main(String[] args) throws IOException, UnsupportedCommOperationException, InterruptedException {
- setUpPort();
- System.out.print("Enter goal speed:\t");
- float goalSpeed = getInput();
- System.out.print("Enter current speed:\t");
- float currentSpeed = getInput();
- simulation( goalSpeed, currentSpeed );
- }
- private static float getInput() throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- return new Float(reader.readLine());
- }
- private static void setUpPort() throws IOException, UnsupportedCommOperationException {
- Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
- //First, Find an instance of serial port as set in PORT_NAMES.
- while (portEnum.hasMoreElements()) {
- CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
- for (String portName : PORT_NAMES) {
- if (currPortId.getName().equals(portName)) {
- portId = currPortId;
- break;
- }
- }
- }
- try {
- serialPort = (SerialPort) portId.open(PORT_NAME, TIME_OUT);
- } catch (PortInUseException e) {
- System.err.println("Port not connected!");
- e.printStackTrace();
- }
- serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
- output = serialPort.getOutputStream();
- }
- private static void simulation( float goalSpeed, float currentSpeed ) throws IOException, InterruptedException {
- while( true ) {
- if ( currentSpeed > 18 ) {
- goalSpeed = 1;
- }
- String outputString = goalSpeed + "," + currentSpeed;
- System.out.println("Sending: " + outputString);
- System.out.println();
- output.write(outputString.getBytes());
- System.out.println("Return Value: ");
- try {
- String inputRet = input.readLine();
- System.out.println(inputRet);
- } catch ( NullPointerException e ) {
- System.out.println("No return value!" );
- } catch ( IOException e ) {
- System.out.println("No return value!" );
- }
- finally {
- currentSpeed += 1;
- Thread.sleep(1500);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement