好象要使用JAVA的一个什么包,具体不记得了
解决方案 »
- 请问car.supper这里是什么意思啊?
- JAVA课本上的问题求解
- 同时创建文件和向文件中输入数据问题
- 关于编码问题!急
- 为什么struts和spring集成是为什么在spring的配置文件中一加配置hibernate的的sessionfactory就找不到action啊一去掉就能找到action?
- 请问一个关于JFileChooser的问题!!!
- 如何在Applet内用专用数据库驱动程序访问ACCESS数据库呀????
- 各位介绍一下有关学习Java的网站。
- 求助,关于Jar文件的动态装入
- 能不能把java文件编译成可执行文件,但不需要java虚拟机?
- 求救!如何编译需要引用其他jar包的java文件?
- java如何实现定时的操作?
可以到java.sun.com寻找。里面有一些Demo.看看那个BlackBox就可以了。
记得IBM的DW上面也有个文章.
import javax.comm.*;
import java.util.*;
public class test {
private SerialPort sPort=null;
public static boolean lock=false;
public test() {
//sPort=setSerialPort(); } //得到计算机的串口
public SerialPort getSerialPort(String com)
{
SerialPort sPort=null;
CommPortIdentifier portID;
String owner=new String("modemn");
int keeptime=1000;
Enumeration portList;
portList=CommPortIdentifier.getPortIdentifiers(); while(portList.hasMoreElements())
{
portID=(CommPortIdentifier)portList.nextElement();
if(portID.getName().equals(com))
try{sPort=(SerialPort)portID.open(owner,keeptime);}
catch(PortInUseException e){System.out.println(e.getMessage());} } System.out.println("serial name is :"+sPort.getName());
try{
//设置串口的参数
sPort.setSerialPortParams(9600,//波特率
SerialPort.DATABITS_8,//数据位数
SerialPort.STOPBITS_1, //停止位
SerialPort.PARITY_NONE);//奇偶位
}catch (UnsupportedCommOperationException e) {System.out.println(e.getMessage());} int baudrate=sPort.getBaudRate();
int databits=sPort.DATABITS_8;
int parity=sPort.getParity();
int stopbits=sPort.getStopBits();
int flowcontrolMode=sPort.getFlowControlMode();
System.out.println("baudrate:"+baudrate);
System.out.println("databits:"+databits);
System.out.println("parity:"+parity);
System.out.println("stopbits:"+stopbits);
System.out.println("flow control mode is:"+flowcontrolMode); return sPort;
}
public static void main(String[] args)
{test t=new test();
SerialPort sPort=t.getSerialPort("COM1");
//SerialPort sPort2=t.getSerialPort("COM2"); new sendCmd(sPort).start();
//new receiveCmd(sPort).start();
//new delCmd(sPort).start();
}}*********************************************************
public class sendCmd extends Thread implements SerialPortEventListener { SerialPort sPort=null;
BufferedReader br=null;
public sendCmd(SerialPort sPort) {
this.sPort=sPort;
//this.setPriority(10);
} public void send(SerialPort sPort)
{
String cmd1="AT+CMGS=\"+8613939060564\"\r hello";
cmd1+=(char)(Integer.parseInt("1a",16))+"z";//用ctrl-z表示结束并发送
String cmd2="AT+CMGL=\"REC UNREAD\"";
String cmd3="AT+CMGL=\"ALL\"";
PrintWriter pw=null; try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd1);
pw.flush();
pw.close();
System.out.println("command has been send");
br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
} catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
catch(TooManyListenersException e){}
} public void run()
{
send(sPort);
}
public synchronized void serialEvent(SerialPortEvent ev)
{
if(ev.getEventType()==ev.DATA_AVAILABLE)
try{
while(true)
{
String msg= br.readLine();
System.out.println("response is:"+msg);
//if(msg.equals("OK"))
//{
// br.close();
// break;
// }//if
}//while
}//try
catch(IOException e){}
}
}