1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| package packageExemple;
import java.io.*;
import javax.comm.*;
import java.util.*;
public class PortWriter {
static Enumeration ports;
static CommPortIdentifier pID;
static OutputStream outStream;
static SerialPort serPort;
static BufferedReader is = null;
static PrintStream os;
static int i = 0;
public PortWriter() throws Exception {
try {
// serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000);
serPort = (SerialPort) pID.open("COM3", 2000);
System.out.println();
System.out.println();
serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
try {
is = new BufferedReader(new InputStreamReader(serPort.getInputStream()));
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
is = null;
}
os = new PrintStream(serPort.getOutputStream(), true, "US-ASCII");
} catch (PortInUseException e) {
System.out.println("PortInUseException : " + e);
} catch (Exception e) {
System.out.println("PortInUseException : " + e);
}
}
public static void main(String[] args) throws Exception {
ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
pID = (CommPortIdentifier) ports.nextElement();
System.out.println("Port " + pID.getName());
if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (pID.getName().equals("COM3")) {
PortWriter pWriter = new PortWriter();
System.out.println("USB found");
sendData();
try {
closingPort();
} catch (Exception e) {
System.out.println("error in calling closingPort()");
System.out.println(e.toString());
}
}
}
}
}
public static void sendData() {
try {
String sCMGF = "AT+CMGF=1\r\n";
send(sCMGF);
Thread.sleep(1000);
readLine();
String sCMGS = "AT+CMGS=\"+336xxxxxxxx\"\r\n";
send(sCMGS);
Thread.sleep(1000);
readLine();
String smsMessage = "hi test5666 testing done\032\r\n";
send(smsMessage);
} catch (Exception e) {
System.out.println("could not write to outputstream:");
System.out.println(e.toString());
}
}
// called finally to close the port
public static void closingPort() throws Exception {
try {
if (is != null)
is.close();
} catch (IOException e) {
System.out.println("could not close is");
System.out.println(e.toString());
}
try {
if (os != null)
os.close();
} catch (Exception e) {
System.out.println("could not close os");
System.out.println(e.toString());
}
try {
if (serPort != null)
serPort.close();
} catch (Exception e) {
System.out.println("could not close serPort");
System.out.println(e.toString());
}
}
// send the string to the gsm modem
public static void send(String cmd) {
try {
os.write(cmd.getBytes());
} catch (IOException e) {
System.out.println("IO Exception : " + e);
}
}
// to read line after each send of string
public static void readLine() {
try {
// Read the response
String response = is.readLine();
System.out.println("Response is :" + response);
} catch (IOException e) {
System.out.println("IO Exception : " + e);
}
}
} |
Partager