package arduino.serial; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.LinkedList; import javax.comm.*; /** * Knihovna pro práci s Arduino pomocí seriového portu * @author Jan Dvořák z Vozerovic */ public class ArduinoSerial implements SerialPortEventListener { private CommPortIdentifier comPortID; private SerialPort comPort=null; private String comAppName="MOJECOM"; //kdo drzi port private int comTimeOut=2000; //t-out je 2000ms private InputStream comInputStream; private OutputStream comOutputStream; private LinkedList buffer=new LinkedList(); private Exception noPort=new Exception("No selected Com port"); /**************************************************************************/ /** * Constructors */ public ArduinoSerial(){} public ArduinoSerial(String appName){this.comAppName=appName;} public ArduinoSerial(String appName, int connectionTimeOut){this.comAppName=appName;this.comTimeOut=connectionTimeOut;} /**************************************************************************/ /** * List of all COM ports (serial) */ public ArrayList getAllComPorts() { ArrayListout=new ArrayList(); Enumeration en=CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier cid; while(en.hasMoreElements() ) { cid=(CommPortIdentifier)en.nextElement(); if( cid.getPortType()==CommPortIdentifier.PORT_SERIAL ) { out.add( cid.getName() ); } } return out; } /**************************************************************************/ /** * Select com port */ public void selectComPort(String portID)throws Exception { //info comPortID=CommPortIdentifier.getPortIdentifier(portID); //serial port comPort=(SerialPort)comPortID.open( comAppName, comTimeOut ); //stream comInputStream=comPort.getInputStream(); //handler comPort.addEventListener(this); comPort.notifyOnDataAvailable(true); //params comPort.setSerialPortParams(9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //baud; databit; stop bit; parity bit comPort.setDTR(false); comPort.setRTS(false); } /**************************************************************************/ /** * Send one Byte */ public void sendByte(int data)throws Exception { if( comPort==null )throw noPort; comOutputStream=comPort.getOutputStream(); comOutputStream.write(data); comOutputStream.close(); } /**************************************************************************/ /** * Send more bytes */ public void sendBytes(byte data[])throws Exception { if( comPort==null )throw noPort; comOutputStream=comPort.getOutputStream(); comOutputStream.write(data); comOutputStream.close(); } /**************************************************************************/ /** * Send text as bytes */ public void sendData(String data)throws Exception { if( comPort==null )throw noPort; comOutputStream=comPort.getOutputStream(); byte dt[]=data.getBytes(); comOutputStream.write(dt); comOutputStream.close(); } /**************************************************************************/ /** * Handler */ public void serialEvent(SerialPortEvent event)throws Exception { switch( event.getEventType() ) { case SerialPortEvent.DATA_AVAILABLE: byte b=0; for( int i=0; i