请高手指点:用java如何控制打印口或串口来通信?指明思路也感激不尽。 look at here,http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 谢谢webwing(kevin)那篇文章是您写的吗?真是太棒啦!有并口控制的吗?如果有就更好啦! 哪儿有javax.comm.*呢?我的jdk1.4 apidocs好象没有看到这个包. 我找到啦:http://java.sun.com/products/javacomm/javadocs/packages.html COMM API 可以从SUN的站点上下载。我作过串口通信的程序,用的是GSM MODEM。做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明://这是其中关键的代码,并不全,希望你能获益import java.io.*;import javax.comm.*;import java.util.*;public class SerialPortHandle extends Thread implements SerialPortEventListener { BufferedReader br; boolean OVER=false; Vector index=new Vector(5,1); Vector response=new Vector(10,1); public SerialPortHandle() { } //得到计算机的串口 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());} }//while // 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 void sendMsg(String cmd ,SerialPort sPort) { PrintWriter pw; //write command(including msg)into SIM card try{ pw=new PrintWriter(sPort.getOutputStream()); pw.println(cmd); pw.flush(); pw.close(); System.out.println("msg has been send from Modemn"); } catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());} try{ br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this); sPort.notifyOnDataAvailable(true); } catch(TooManyListenersException e){System.out.println(e.getMessage());} catch(IOException e){} }public void getMsgFromModemn(String cmd,SerialPort sPort) { PrintWriter pw=null; //把让数据猫接收数据的命令传到数据猫 try{ pw=new PrintWriter(sPort.getOutputStream()); pw.println(cmd); pw.flush(); pw.close(); // System.out.println("the cmd that read msg from modemn had been send"); br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this); sPort.notifyOnDataAvailable(true); } catch(IOException e){System.out.println(e.getMessage());} catch(TooManyListenersException e){ System.out.println("catch exception when receiving msg from modemn"); System.out.println(e.getMessage());} } public void run(){//代码省略 } //处理侦听到的串口事件 public synchronized void serialEvent(SerialPortEvent ev) { //如果有串口事件发生 if(ev.getEventType()==ev.DATA_AVAILABLE) try{ while(true){ respmsg=br.readLine();//在打开的读数据流中读一行数据 //System.out.println("repmsg:"+respmsg); //如果读到字符串“OK”,说明数据猫的响应已经结束 if(respmsg.equals("OK")) { // System.out.println("the end!"); break;}//关闭读数据流 }//while }//try catch(IOException e){System.out.println("catch exception when receive msg from modemn"); System.out.println(e.getMessage());}} COMM API 可以从SUN的站点上下载。我作过串口通信的程序,用的是GSM MODEM。做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明://这是其中关键的代码,并不全,希望你能获益import java.io.*;import javax.comm.*;import java.util.*;public class SerialPortHandle extends Thread implements SerialPortEventListener { BufferedReader br; boolean OVER=false; Vector index=new Vector(5,1); Vector response=new Vector(10,1); public SerialPortHandle() { } //得到计算机的串口 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());} }//while // 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 void sendMsg(String cmd ,SerialPort sPort) { PrintWriter pw; //write command(including msg)into SIM card try{ pw=new PrintWriter(sPort.getOutputStream()); pw.println(cmd); pw.flush(); pw.close(); System.out.println("msg has been send from Modemn"); } catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());} try{ br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this); sPort.notifyOnDataAvailable(true); } catch(TooManyListenersException e){System.out.println(e.getMessage());} catch(IOException e){} }public void getMsgFromModemn(String cmd,SerialPort sPort) { PrintWriter pw=null; //把让数据猫接收数据的命令传到数据猫 try{ pw=new PrintWriter(sPort.getOutputStream()); pw.println(cmd); pw.flush(); pw.close(); // System.out.println("the cmd that read msg from modemn had been send"); br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this); sPort.notifyOnDataAvailable(true); } catch(IOException e){System.out.println(e.getMessage());} catch(TooManyListenersException e){ System.out.println("catch exception when receiving msg from modemn"); System.out.println(e.getMessage());} } public void run(){//代码省略 } //处理侦听到的串口事件 public synchronized void serialEvent(SerialPortEvent ev) { //如果有串口事件发生 if(ev.getEventType()==ev.DATA_AVAILABLE) try{ while(true){ respmsg=br.readLine();//在打开的读数据流中读一行数据 //System.out.println("repmsg:"+respmsg); //如果读到字符串“OK”,说明数据猫的响应已经结束 if(respmsg.equals("OK")) { // System.out.println("the end!"); break;}//关闭读数据流 }//while }//try catch(IOException e){System.out.println("catch exception when receive msg from modemn"); System.out.println(e.getMessage());}} http://www-900.ibm.com/developerWorks/cn/java/joy-comm/index.shtml这片文章我也看过,但是我觉得作者根本就没有实际的在机器上实验过,我那他的原程序实验结果根本得不到想要的结果。最后我查资料发现,他根本就没有多串口事件做处理,不知道他说的直接一连是怎样实现的。 【疑惑】-java 到底做什么用 创建抽象类问题 Vector不会为static 分配空间,是不是这样吧? 一道很不错的基础题,有兴趣的朋友可以看看! 一个逻辑问题。各位帮帮小妹 菜鸟提问:如何用数组构成链表实现数三退一 [求助]请帮帮我 关与IOstream打开2次异常 产生错误已注释!帮解释清楚给50!代码如下: 很简单的程序,就是有问题——————applet 如何把一个文件夹中的所有文件和子文件夹拷贝到另外一个已经存在的文件夹里面去? 在Java中以MS SQL2000为后端数据库? 请问如何控制JTextArea中文本的换行?
那篇文章是您写的吗?真是太棒啦!
有并口控制的吗?如果有就更好啦!
我的jdk1.4 apidocs好象没有看到这个包.
http://java.sun.com/products/javacomm/javadocs/packages.html
做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明:
//这是其中关键的代码,并不全,希望你能获益
import java.io.*;
import javax.comm.*;
import java.util.*;
public class SerialPortHandle extends Thread implements SerialPortEventListener {
BufferedReader br;
boolean OVER=false;
Vector index=new Vector(5,1);
Vector response=new Vector(10,1);
public SerialPortHandle() {
}
//得到计算机的串口
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());} }//while // 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 void sendMsg(String cmd ,SerialPort sPort)
{ PrintWriter pw; //write command(including msg)into SIM card
try{ pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
System.out.println("msg has been send from Modemn");
}
catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());}
try{
br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
}
catch(TooManyListenersException e){System.out.println(e.getMessage());}
catch(IOException e){} }public void getMsgFromModemn(String cmd,SerialPort sPort)
{ PrintWriter pw=null; //把让数据猫接收数据的命令传到数据猫
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
// System.out.println("the cmd that read msg from modemn had been send"); br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
}
catch(IOException e){System.out.println(e.getMessage());}
catch(TooManyListenersException e){
System.out.println("catch exception when receiving msg from modemn");
System.out.println(e.getMessage());} }
public void run()
{
//代码省略
} //处理侦听到的串口事件
public synchronized void serialEvent(SerialPortEvent ev)
{ //如果有串口事件发生
if(ev.getEventType()==ev.DATA_AVAILABLE) try{
while(true){
respmsg=br.readLine();//在打开的读数据流中读一行数据
//System.out.println("repmsg:"+respmsg);
//如果读到字符串“OK”,说明数据猫的响应已经结束
if(respmsg.equals("OK"))
{ // System.out.println("the end!");
break;}//关闭读数据流 }//while
}//try
catch(IOException e){System.out.println("catch exception when receive msg from modemn");
System.out.println(e.getMessage());}
}
做串口或者是并口操作时,关键是作好串口或者并口事件的处理。以串口为例说明:
//这是其中关键的代码,并不全,希望你能获益
import java.io.*;
import javax.comm.*;
import java.util.*;
public class SerialPortHandle extends Thread implements SerialPortEventListener {
BufferedReader br;
boolean OVER=false;
Vector index=new Vector(5,1);
Vector response=new Vector(10,1);
public SerialPortHandle() {
}
//得到计算机的串口
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());} }//while // 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 void sendMsg(String cmd ,SerialPort sPort)
{ PrintWriter pw; //write command(including msg)into SIM card
try{ pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
System.out.println("msg has been send from Modemn");
}
catch(IOException e){System.out.println("catch exception when send msg from Modemn:"+e.getMessage());}
try{
br=new BufferedReader(new InputStreamReader(sPort.getInputStream())); sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
}
catch(TooManyListenersException e){System.out.println(e.getMessage());}
catch(IOException e){} }public void getMsgFromModemn(String cmd,SerialPort sPort)
{ PrintWriter pw=null; //把让数据猫接收数据的命令传到数据猫
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd);
pw.flush();
pw.close();
// System.out.println("the cmd that read msg from modemn had been send"); br=new BufferedReader(new InputStreamReader(sPort.getInputStream()));
sPort.addEventListener(this);
sPort.notifyOnDataAvailable(true);
}
catch(IOException e){System.out.println(e.getMessage());}
catch(TooManyListenersException e){
System.out.println("catch exception when receiving msg from modemn");
System.out.println(e.getMessage());} }
public void run()
{
//代码省略
} //处理侦听到的串口事件
public synchronized void serialEvent(SerialPortEvent ev)
{ //如果有串口事件发生
if(ev.getEventType()==ev.DATA_AVAILABLE) try{
while(true){
respmsg=br.readLine();//在打开的读数据流中读一行数据
//System.out.println("repmsg:"+respmsg);
//如果读到字符串“OK”,说明数据猫的响应已经结束
if(respmsg.equals("OK"))
{ // System.out.println("the end!");
break;}//关闭读数据流 }//while
}//try
catch(IOException e){System.out.println("catch exception when receive msg from modemn");
System.out.println(e.getMessage());}
}