Java comm.jar无法读取串口数据

安德烈_T 2009-07-27 03:30:02
小弟最近在做一个短信接收系统,在网上找了comm.jar包,并且正确配置了环境,可以实现通过com口连接的短信猫发送短信,但是却不能读取发送到短信猫上面的数据,且程序无异常。代码如下:请教高手
package commapi;

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {

static CommPortIdentifier portId;
static Enumeration portList;// 枚举类

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {

portList = CommPortIdentifier.getPortIdentifiers();
/*
* 不带参数的getPortIdentifiers方法获得一个枚举对象,该对象又包含了系统中管理每个端口的CommPortIdentifier对象。注意这里的端口不仅仅是指串口,也包括并口。这个方法还可以带参数。getPortIdentifiers(CommPort)获得与已经被应用程序打开的端口相对应的CommPortIdentifier对象。
* getPortIdentifier(String
* portName)获取指定端口名(比如“COM1”)的CommPortIdentifier对象。
*/

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/* getPortType方法返回端口类型 */{
if (portId.getName().equals("COM3"))/* 找Windows下的第3个串口 */{
// if
// (portId.getName().equals("/dev/term/a"))/*找Unix-like系统下的第一个串口*/
// {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
/*
* open方法打开通讯端口,获得一个CommPort对象。 它使程序独占端口。如果端口正被其他应用程序占用,将使用
* CommPortOwnershipListener事件机制,
* 传递一个PORT_OWNERSHIP_REQUESTED事件。每个端口都关联一个 InputStream
* 何一个OutputStream。
* 如果端口是用open方法打开的,那么任何的getInputStream都将返回相同的数据流对象,除非有close 被调用。
* 有两个参数,第一个为应用程序名;第二个参数是在端口打开时阻塞等待的毫秒数。
*/
} catch (PortInUseException e) {
e.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();/* 获取端口的输入流对象 */
} catch (IOException e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);/* 注册一个SerialPortEventListener事件来监听串口事件 */
} catch (TooManyListenersException e) {
e.printStackTrace();
}

serialPort.notifyOnDataAvailable(true);/* 数据可用 */

try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);/* 设置串口初始化参数,依次是波特率,数据位,停止位和校验 */
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}

readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
System.out.println("run");
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}

// 串口事件
public void serialEvent(SerialPortEvent event) {
System.out.println(event.getEventType());

switch (event.getEventType()) {
case SerialPortEvent.BI:/* Break interrupt,通讯中断 */
case SerialPortEvent.OE:/* Overrun error,溢位错误 */
case SerialPortEvent.FE:/* Framing error,传帧错误 */
case SerialPortEvent.PE:/* Parity error,校验错误 */
case SerialPortEvent.CD:/* Carrier detect,载波检测 */
case SerialPortEvent.CTS:/* Clear to send,清除发送 */
case SerialPortEvent.DSR:/* Data set ready,数据设备就绪 */
case SerialPortEvent.RI:/* Ring indicator,响铃指示 */
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:/*
* Output buffer is
* empty,输出缓冲区清空
*/
break;

case SerialPortEvent.DATA_AVAILABLE:/*
* Data available at the serial
* port,端口有可用数据。读到缓冲数组,输出到终端
*/
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {
}
break;
}
}
}

我的短信猫是连接到com3的,而且可以确定短信是发送到短信猫的,但是短信猫接收到数据以后,却没有触发事件,
...全文
737 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl3450341 2009-08-08
  • 打赏
  • 举报
回复
没做过这样的额 mark
zhanghua4109 2009-08-07
  • 打赏
  • 举报
回复
package nc.fund.test;

import java.io.*;
import java.util.*;
import javax.comm.*;
import java.awt.*;
import java.util.Properties;
import javax.swing.*;
import java.awt.event.*;


public class com extends JFrame implements ActionListener,Runnable,SerialPortEventListener
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static CommDriver driver=null;
static Properties props = new Properties();
static boolean use=false;
Thread readThread;
JScrollPane p1;
JTextField Text=new JTextField(10);
JButton ok=new JButton("确定");
JTextArea area=new JTextArea(15,10);
JLabel l1=new JLabel("Message:");
com()
{
setTitle("Com");
setVisible(true);
setLayout(null);
p1=new JScrollPane(area);
add(l1);l1.setBounds(35,30,100,25);
add(Text);Text.setBounds(120,30,150,25);
add(ok);ok.setBounds(290,30,80,23);
add(p1);p1.setBounds(35,70,340,250);
ok.addActionListener(this);
setSize(400,400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-400)/2,(screen.height-400)/2);
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
String key="Driver";
String driverName="com.sun.comm.Win32Driver";
try
{
driver =(CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
}
catch(Exception a){JOptionPane.showMessageDialog(null,a);}
String message=Text.getText();
boolean portFound=false;
String defaultPort="COM1";
portList=CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
System.out.println("portId:"+portId.getName());
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
{
if(portId.getName().equals(defaultPort))
{
portFound=true;
if(!use)
{
try
{
serialPort=(SerialPort)portId.open("com",2000);
}
catch(PortInUseException a){JOptionPane.showMessageDialog(null,"端口正在使用");continue;}
}
/*通过串口对象获得写串口流对象 */
try
{
outputStream=serialPort.getOutputStream();
}
catch(IOException a){}
/*设置串口参数依次为(波特率,数据位,停止位,奇偶检验) */
try
{
serialPort.setSerialPortParams(2400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}
catch(UnsupportedCommOperationException a){}
/*设置发送缓冲区为空事件true有效,false无效*/
try
{
serialPort.notifyOnOutputEmpty(true);
}
catch(Exception a){}
/*写入数据*/
try
{
outputStream.write(message.getBytes());
area.append("\nsend to port is sucess: "+message+"***");
use=true;
}
catch(IOException a){}
}
}
}
if(!portFound)
{
JOptionPane.showMessageDialog(null,"can't find the port");
}
/*获得输入流*/
try
{
inputStream=serialPort.getInputStream();
}
catch(IOException a){}
/*增加监听*/
try
{
serialPort.addEventListener(this);
}
catch(TooManyListenersException a){}
// 设置串口数据时间有效
serialPort.notifyOnDataAvailable(true);
readThread=new Thread(this);
readThread.start();
}
}
public void run()
{
try
{
Thread.sleep(2000);
}
catch(InterruptedException e){}
}
public static void main(String[] args)
{
new com();
}
public void serialEvent(SerialPortEvent event)
{
switch(event.getEventType())
{
case SerialPortEvent.BI:/*Break interrupt,通讯中断*/
case SerialPortEvent.OE:/*Overrun error,溢位错误*/
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[1];
try
{
// InputStreamReader fis = new InputStreamReader(inputStream, "utf-8");
// while ((c = fis.read()) != -1)
// {
// Character d = new Character((char) c);
// ContentMsg = ContentMsg.concat(d.toString());
// System.out.println(ContentMsg);
/*从串口对象读取数据*/
while(inputStream.available()>0)
{
int numBytes=inputStream.read(readBuffer);
area.setLineWrap(true);
area.append(new String(readBuffer));
}
}
catch(IOException e){}
break;
}
}
}
bayougeng 2009-08-06
  • 打赏
  • 举报
回复
换成rxtx包试试。
我的博客里介绍了详细的环境配置方法。
yqsshr 2009-07-30
  • 打赏
  • 举报
回复
接分,希望2楼的对你有用
安德烈_T 2009-07-30
  • 打赏
  • 举报
回复
二楼有用?我一点都没看出来,昨天可以接收了,添加了一个指令:AT+CNMI=2,2,0,0,0

但是现实的是pdu编码,今天早上把pdu编码解析出来了

但是今天又不能接收了,把昨天的代码调出来也不行了,郁闷,不知道哪里又搞错了
  • 打赏
  • 举报
回复
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
bigbug9002 2009-07-27
  • 打赏
  • 举报
回复
Marked

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧