import gnu.io.CommPortIdentifier; import gnu.io.CommPortOwnershipListener; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import gnu.io.UnsupportedCommOperationException; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Enumeration; import java.util.TooManyListenersException; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JTextArea; /** * * @author andrelp */ public class GSMGateway implements SerialPortEventListener, CommPortOwnershipListener { private String comPort; // This COM Port must be connect with GSM Modem or your mobile phone private String messageString = ""; private CommPortIdentifier portId = null; private Enumeration portList; private InputStream inputStream = null; private OutputStream outputStream = null; private SerialPort serialPort; String readBufferTrial = ""; /** * * @param comm */ public GSMConnect(String comm) { this.comPort = comm; } public boolean getPort() { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(comPort)) { System.out.println("Got PortName"); return true; } } } return false; } public void connect() throws NullPointerException, TooManyListenersException { if (portId != null) { try { portId.addPortOwnershipListener(this); serialPort = (SerialPort) portId.open("GatewayGSM", 2000); serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (PortInUseException | UnsupportedCommOperationException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (IOException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } /* * Set the hardware flow control */ try { // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); serialPort.setDTR(true); } catch (UnsupportedCommOperationException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } /** * These are the events we want to know about /** These are the * events we want to know about */ serialPort.addEventListener(this); serialPort.notifyOnCTS(true); serialPort.notifyOnDSR(true); serialPort.notifyOnOutputEmpty(true); } else { System.out.println("COM Port not found!!"); throw new NullPointerException("COM Port not found!!"); } } public void send(String cmd, int timeout) { serialPort.setRTS(true); try { outputStream.write(cmd.getBytes()); outputStream.flush(); System.out.println("Sended" + cmd); Thread.sleep(timeout); } catch (InterruptedException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } } public void closePort() { serialPort.close(); } @Override public void serialEvent(SerialPortEvent serialPortEvent) { switch (serialPortEvent.getEventType()) { case SerialPortEvent.BI: break; case SerialPortEvent.OE: break; case SerialPortEvent.FE: break; case SerialPortEvent.PE: break; case SerialPortEvent.CD: break; case SerialPortEvent.CTS: //recieve a ask to send data, this side must prepare to read. count++; if (serialPort.isCTS()) { System.out.println("CTS"); try { if (inputStream.available() > 0) { System.out.println("buffer-CTS"); while (inputStream.available() > 0) { String aux; byte[] readBuffer = new byte[2048]; int numBytes = inputStream.read(readBuffer); aux = new String(readBuffer); readBuffer = new byte[2048]; System.out.println(LocalDateTime.now().toString() + ": " + aux); send("OK", 5000); } } } catch (IOException ex) { Logger.getLogger(GSMConnect.class.getName()).log(Level.SEVERE, null, ex); } } break; case SerialPortEvent.DSR: break; case SerialPortEvent.RI: break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println("OUTPUT_BUFFER_EMPTY"); serialPort.setRTS(false); break; case SerialPortEvent.DATA_AVAILABLE: break; } } @Override public void ownershipChange(int type ) { switch (type) { case CommPortOwnershipListener.PORT_UNOWNED: System.out.println(portId.getName() + ": PORT_UNOWNED"); break; case CommPortOwnershipListener.PORT_OWNED: System.out.println(portId.getName() + ": PORT_OWNED"); break; case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED: System.out.println(portId.getName() + ": PORT_INUSED"); break; } } }