Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gnu.io.CommPortIdentifier;
- import gnu.io.PortInUseException;
- import gnu.io.SerialPort;
- import gnu.io.SerialPortEvent;
- import gnu.io.SerialPortEventListener;
- import gnu.io.UnsupportedCommOperationException;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import java.util.TooManyListenersException;
- import java.lang.Boolean;
- public class DJController implements KeyListener, SerialPortEventListener {
- static CommPortIdentifier portId;
- static CommPortIdentifier saveportId;
- static Enumeration portList;
- InputStream inputStream;
- SerialPort serialPort;
- Thread readThread;
- static OutputStream outputStream;
- static boolean outputBufferEmptyFlag = true;
- private int baudrate;
- private int stopbits;
- private String portname;
- private boolean forward = false;
- private boolean back = false;
- private boolean left = false;
- private boolean right = false;
- private boolean sendNewCmd = false;
- static final int MANUAL_FORWARD = 0x00;
- static final int MANUAL_FORWARD_RIGHT = 0x01;
- static final int MANUAL_FORWARD_LEFT = 0x02;
- static final int MANUAL_ROTATE_RIGHT = 0x03;
- static final int MANUAL_ROTATE_LEFT = 0x04;
- static final int MANUAL_BACKWARD = 0x05;
- static final int MANUAL_STOP = 0x06;
- static final int MANUAL_SEIZE = 0x07;
- static final int MANUAL_RELEASE = 0x08;
- static final int MANUAL_AUTONOM = 0x09;
- public DJController(String portname, int baudrate, int stopbits)
- {
- System.out.println("DJController skapad");
- boolean portFound = false;
- if (stopbits > 0 && stopbits < 3)
- this.stopbits = stopbits;
- else
- this.stopbits = SerialPort.STOPBITS_1;
- if (baudrate > 0)
- this.baudrate = baudrate;
- else
- this.baudrate = 38400;
- this.portname = portname;
- System.out.println("Setting default port to "+portname);
- // parse ports and if the default port is found, initialized the reader
- portList = CommPortIdentifier.getPortIdentifiers();
- while (portList.hasMoreElements()) {
- portId = (CommPortIdentifier) portList.nextElement();
- if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
- if (portId.getName().equals(portname)) {
- System.out.println("Found port: "+portname);
- portFound = true;
- // init reader thread
- initcomm();
- break;
- }
- }
- }
- if (!portFound) {
- System.out.println("Port '" + portname + "' not found.");
- }
- }
- public void initcomm() {
- // initalize serial port
- try {
- serialPort = (SerialPort) portId.open("AppFrame", 50);
- } catch (PortInUseException e) {
- System.out.println("Port in use!");
- }
- try {
- inputStream = serialPort.getInputStream();
- } catch (IOException e) {
- System.out.println("IO error (inputstream??)");
- }
- try {
- serialPort.addEventListener(this);
- } catch (TooManyListenersException e) {
- System.out.println("Too many listeners(?)");
- }
- // activate the DATA_AVAILABLE notifier
- serialPort.notifyOnDataAvailable(true);
- System.out.println("Configuring "+this.portname+" with "+this.stopbits+" stop bits and "+this.baudrate+" baud rate.");
- try {
- // set port parameters
- serialPort.setSerialPortParams(this.baudrate, SerialPort.DATABITS_8,
- this.stopbits,
- SerialPort.PARITY_NONE);
- } catch (UnsupportedCommOperationException e) {
- System.out.println("Unsupported operation: " + e.getMessage());
- }
- }
- public void initwritetoport() {
- // initwritetoport() assumes that the port has already been opened and
- // initialized by "public nulltest()"
- try {
- // get the outputstream
- outputStream = serialPort.getOutputStream();
- } catch (Exception e) {
- System.out.println("No outputstream?!");
- }
- try {
- // activate the OUTPUT_BUFFER_EMPTY notifier
- serialPort.notifyOnOutputEmpty(true);
- } catch (Exception e) {
- System.out.println("Error setting event notification");
- System.out.println(e.toString());
- System.exit(-1);
- }
- }
- public void writetoport(String msg) {
- System.out.println("Writing \""+msg+"\" to "+serialPort.getName());
- if (outputStream == null)
- {
- System.out.println("Outputstream är null?!");
- } else {
- try {
- // write string to serial port
- outputStream.write(msg.getBytes());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void serialEvent(SerialPortEvent event) {
- switch (event.getEventType()) {
- case SerialPortEvent.BI:
- case SerialPortEvent.OE:
- case SerialPortEvent.FE:
- case SerialPortEvent.PE:
- case SerialPortEvent.CD:
- case SerialPortEvent.CTS:
- case SerialPortEvent.DSR:
- case SerialPortEvent.RI:
- case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
- break;
- case SerialPortEvent.DATA_AVAILABLE:
- // we get here if data has been received
- byte[] readBuffer = new byte[20];
- try {
- // read data
- while (inputStream.available() > 0) {
- int numBytes = inputStream.read(readBuffer);
- }
- // print data
- String result = new String(readBuffer);
- System.out.println("Read: "+result);
- } catch (IOException e) {}
- break;
- }
- }
- public void update()
- {
- if (sendNewCmd == true) {
- if (forward == true && left == true)
- commSend(MANUAL_FORWARD_LEFT);
- else if (forward == true && right == true)
- commSend(MANUAL_FORWARD_RIGHT);
- else if (forward)
- commSend(MANUAL_FORWARD);
- else if (back)
- commSend(MANUAL_BACKWARD);
- else if (right)
- commSend(MANUAL_ROTATE_RIGHT);
- else if (left)
- commSend(MANUAL_ROTATE_LEFT);
- else
- commSend(MANUAL_STOP);
- sendNewCmd = false;
- }
- }
- public void stopDJ()
- {
- commSend(MANUAL_STOP);
- }
- public void commSend(int cmd)
- {
- try {
- writetoport(new String(String.valueOf(cmd)));
- } catch(Exception e) {
- System.err.println("Oups, writeport gick inge bra. Saknar COM4?");
- }
- }
- public void keyPressed(KeyEvent e)
- {
- int key = e.getKeyCode();
- boolean updated = false;
- // sv‰ng
- if (key == KeyEvent.VK_LEFT && !left)
- {
- updated = true;
- left = true;
- }
- if (key == KeyEvent.VK_RIGHT && !right)
- {
- updated = true;
- right = true;
- }
- // frambak
- if (key == KeyEvent.VK_UP && !forward)
- {
- updated = true;
- forward = true;
- }
- if (key == KeyEvent.VK_DOWN && !back)
- {
- updated = true;
- back = true;
- }
- if (updated)
- {
- sendNewCmd = true;
- }
- }
- public void keyReleased(KeyEvent e)
- {
- int key = e.getKeyCode();
- // sv‰ng
- if (key == KeyEvent.VK_LEFT)
- left = false;
- if (key == KeyEvent.VK_RIGHT)
- right = false;
- // frambak
- if (key == KeyEvent.VK_UP)
- forward = false;
- if (key == KeyEvent.VK_DOWN)
- back = false;
- sendNewCmd = true;
- }
- public void keyTyped(KeyEvent e)
- {
- /*if (e.getKeyCode() == KeyEvent.VK_SPACE)
- {
- stopDJ();
- }*/
- if (e.getKeyCode() == KeyEvent.VK_Y) {
- commSend(MANUAL_AUTONOM);
- }
- }
- }
Add Comment
Please, Sign In to add comment