Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gnu.io.*;
- import java.io.*;
- import java.util.*;
- public class TwoWaySerialComm
- {
- public void arduinPort(){
- CommPort arduino = getArduinoPort();
- try {
- arduino.getOutputStream().write(1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public CommPort getArduinoPort() {
- @SuppressWarnings("rawtypes")
- Enumeration ports = CommPortIdentifier.getPortIdentifiers();
- while(ports.hasMoreElements()) {
- CommPortIdentifier identifier = (CommPortIdentifier) ports.nextElement();
- if(isArduino(identifier)) {
- try {
- return identifier.open(getClass().getName(), 2000);
- } catch (PortInUseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } // 2 second timeout
- }
- }
- return null;
- }
- public boolean isArduino(CommPortIdentifier identifier) {
- if (identifier.getName()== "COM3"){
- return true;
- }
- else{
- return false;
- }
- // if you know the name of the port ahead of time you can
- // compare it here with identifier.getName(), otherwise
- // you can interface with the user like the Arduino IDE's
- // serial monitor
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement