📄 portwriter.java
字号:
/*
* PortWriter.java
*
* Created on February 8, 2002, 4:40 PM
*/
package unleashed.ch14;
import java.io.*;
import javax.comm.*;
//import smspack.Port;
//import smspack.SMSTools;
import java.util.*;
/**
*
* @author Stephen Potts
* @version
*/
public class PortWriter
{
static Enumeration ports;
static CommPortIdentifier pID;
static OutputStream outStream;
static InputStream inStream;
private static OutputStreamWriter out;
private static InputStreamReader in;
static SerialPort serPort;
static String messageToSend = "Tell everyone what a great book this is!\n";
/** Creates new PortFinder */
public PortWriter()
{
try{pID = (CommPortIdentifier)CommPortIdentifier.getPortIdentifier("COM4");}
catch(Exception e)
{
e.printStackTrace();
}
try
{
serPort = (SerialPort)pID.open("PortWriter",2000);
}catch (PortInUseException piue)
{
System.out.println("Exception " + piue);
}
try
{
inStream = serPort.getInputStream();
outStream = serPort.getOutputStream();
}catch (IOException ioe)
{
System.out.println("Exception " + ioe);
}
try
{
serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException ucoe)
{
System.out.println("Exception " + ucoe);
}
try{
out = new OutputStreamWriter(outStream, "ISO-8859-1");
in = new InputStreamReader(inStream, "ISO-8859-1");
}catch(Exception e){
e.printStackTrace();
}
try{
writeln("");
writeln("AT+CMGF=1"); // text 模式
read();
writeln("AT+CMGS=\"+8613421522553\""); // 目的电话号码
read();
writeln("hello thanks !!"); // 短信内容
//write(); // set PDU
write("\u001A"); // set Ctrl-Z = indicates end of PDU
read();
}
catch(Exception e)
{
e.printStackTrace();
}
//outStream.write(messageToSend.getBytes());
System.out.println("The message was sent");
}//Constructor
public static void main(String[] args)
{
PortWriter pWriter = new PortWriter();
System.out.println("COM10 found");
}//main()
public static void writeln(String s) throws Exception {
out.write(s);
out.write('\r');
out.flush();
System.out.println("write port: " + s + "\n"); // for debugging
} // writeln
public static String read() throws Exception {
int n, i;
char c;
String answer = new String("");
// do { // wait until the first byte is received
// Thread.sleep(100); // wait at least 100 msec [Timing]
// } while (in.ready() == false);
for (i = 0; i < 10; i++) { // look 5 times for character string to receive
//----- collect all characters from the serial line
while (in.ready()) { // there is a byte available
n = in.read(); // get the byte from the serial line
if (n != -1) { // one byte received
c = (char)n; // convert the received integer to a character
answer = answer + c; // collect the characters from the serial line
Thread.sleep(1); // wait 1 msec between every collected byte from the mobile phone [Timing]
} // if
else break; // no more bytes available
} // while
Thread.sleep(100); // wait 100 msec [Timing]
} // for
System.out.println("read port: " + answer + "\n"); // for debugging
return answer; // give the received string back to the caller
} // read
public static void write(String s) throws Exception {
out.write(s);
out.flush();
} // write
}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -