/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rabin;
/**
*
* @author Rabin
*/
import java.io.*;
import java.util.*;
import javax.comm.*;
public class rabin implements SerialPortEventListener
{
static CommPortIdentifier portId;
static Enumeration portList;
static char mn=0x0d;
static OutputStream outputStream;
static InputStream inputStream;
static SerialPort serialPort;
Thread readThread;
public rabin()
{
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {System.out.println("100000e");}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println("100000f");}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println("hello");}
}
public rabin(String arg1,String arg2,String arg3) {
portList = CommPortIdentifier.getPortIdentifiers();
String ms[]={
"AT+CFUN="+arg2+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn,
"AT+CGDCONT=1,\"ip\",\""+arg3.toString()+"\""+mn
};
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(arg1)) {
// if (portId.getName().equals("/dev/term/a")) {
new rabin();
try
{
for(int i=0;i<ms.length;i++)
{
new hi(ms,inputStream,outputStream);
Thread.sleep(50);
}
}catch(Exception e ){
}
}
}
}
}
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:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}