/* Files: SendAT.java */
import java.awt.*;
import java.awt.event.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.*;
public
class SendAT extends Frame
implements ActionListener,
ItemListener,
SerialPortEventListener,
WindowListener {
/* GUI Objects. */
private Label lATCommand, lResult;
private Button bSend;
private TextArea taResult;
private TextField tfATCommand;
private Choice chCOM;
/* Connection Objects. */
private static Enumeration portList;
private static CommPortIdentifier portId;
private static SerialPort serialPort;
private static String COM;
private static PrintStream printStream;
private static InputStream inputStream;
/**
* Main method (Entry point application).
*/
public static void main(String args[]) {
SendAT sendAT = new SendAT();
sendAT.pack();
sendAT.setVisible(true);
}
/**
* Constructor method.
*/
public SendAT() {
inizializeGUI();
}
/**
* Build the graphics of the application.
*/
private void inizializeGUI() {
/* Set Layout. */
this.setSize(new Dimension(347, 358));
this.setLayout(null);
this.setBackground(Color.lightGray);
this.setResizable(false);
this.setTitle("Send AT - http://web.tiscalinet.it/SiemensS25");
this.addWindowListener(this);
/* Set Choice. */
chCOM = new Choice();
chCOM.addItem("Select Port");
chCOM.addItem("COM1");
chCOM.addItem("COM2");
chCOM.addItem("COM3");
chCOM.addItem("COM4");
chCOM.addItem("COM5");
chCOM.setBounds(new Rectangle(15, 31, 107, 22));
chCOM.addItemListener(this);
/* Set TextField. */
tfATCommand = new TextField();
tfATCommand.setBounds(new Rectangle(14, 95, 313, 24));
tfATCommand.setText("AT+CLCK=cs,1");
/* Set TextArea. */
taResult = new TextArea("",100,100,1);
taResult.setBounds(new Rectangle(13, 190, 317, 145));
/* Set Button. */
bSend = new Button();
bSend.setBounds(new Rectangle(130, 139, 72, 24));
bSend.setLabel("send");
bSend.addActionListener(this);
bSend.setEnabled(false);
/* Set Labels. */
lATCommand = new Label();
lATCommand.setBounds(new Rectangle(15, 79, 101, 18));
lATCommand.setText("AT Command");
lResult = new Label();
lResult.setBounds(new Rectangle(15, 173, 94, 17));
lResult.setText("Result");
/* Add the graphics objects to layout. */
this.add(taResult, null);
this.add(tfATCommand, null);
this.add(bSend, null);
this.add(lResult, null);
this.add(lATCommand, null);
this.add(chCOM, null);
}
/**
* Capture the button events.
*/
public void actionPerformed(ActionEvent e) {
// get AT Command from textfield.
String sAT = tfATCommand.getText().trim();
// send AT Command to COM port.
printStream.println(sAT);
}
/**
* Capture the choice events and inizialize
* the connection.
*/
public void itemStateChanged(ItemEvent e) {
System.out.println("Connecting....");
// get port selected from choice.
COM = (String)e.getItem();
// if not port selected?
if(COM.equals("Select Port")) {
taResult.setText("Select COM");
} else {
// port selected.
bSend.setEnabled(true);
chCOM.setEnabled(false);
// get to all the present ports.
portList = CommPortIdentifier.getPortIdentifiers();
// slide the found ports.
while (portList.hasMoreElements()) {
// assign port.
portId = (CommPortIdentifier) portList.nextElement();
// is serial port?
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// is selected port?
if (portId.getName().equals(COM)) {
try {
// open port connection.
serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
// open input stream.
inputStream = serialPort.getInputStream();
// open output stream.
printStream = new PrintStream(serialPort.getOutputStream());
// set connection.
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("Connection performed");
// add port to events listener.
serialPort.addEventListener(this);
// notify data on COM.
serialPort.notifyOnDataAvailable(true);
} catch (UnsupportedCommOperationException ucoe) {
ucoe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (PortInUseException piue) {
taResult.setText("Port In Use!");
piue.printStackTrace();
} catch (TooManyListenersException tmle) {
tmle.printStackTrace();
}
}
}
}
}
}
/**
* Capture COM port events.
*/
public void serialEvent(SerialPortEvent event) {
// filter event type.
switch(event.getEventType()) {
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("\nDATA AVAILABLE");
// build COM read buffer.
byte[] readBuffer = new byte[100];
try {
// number buffer.
int numBytes;
// if data present?
while(inputStream.available() > 0) {
// read buffer.
numBytes = inputStream.read(readBuffer);
}
String sBuffer = new String(readBuffer).trim();
System.out.println("Result--> " + sBuffer);
// set textArea.
taResult.appendText(sBuffer);
} catch(IOException e) {
e.printStackTrace();
}
break;
}
}
/* Capture window events */
public void windowClosed(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void windowOpened(WindowEvent event) {}
public void windowClosing(WindowEvent event) {
System.exit(0);
}
} // end of class.