当前位置: 技术问答>java相关
谁熟悉连接串口的FALCOM A2D 数据猫的编程?
来源: 互联网 发布时间:2015-06-02
本文导语: //此程序的功能是用AT命令实现对连接在串口COM1的FALCOM A2D数据猫//的操作,数据猫的波特率为9600 package serialporttest; /** * Title: * Description: * Copyright: Copyright (c) 2002 * Company: * @author * @version 1.0 */ im...
//此程序的功能是用AT命令实现对连接在串口COM1的FALCOM A2D数据猫//的操作,数据猫的波特率为9600
package serialporttest;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
import java.io.*;
import javax.comm.*;
import java.util.*;
public class test extends Thread {
private static SerialPort sPort=null;
public test() {
sPort=setSerialPort();
}
//得到计算机的串口
public static SerialPort getSerialPort()
{
SerialPort sPort=null;
CommPortIdentifier portID;
String owner=new String("modemn");
int keeptime=500;
Enumeration portList;
portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements())
{
portID=(CommPortIdentifier)portList.nextElement();
if(portID.getName().equals("COM1"))
try{sPort=(SerialPort)portID.open(owner,keeptime);}
catch(PortInUseException e){System.out.println(e.getMessage());}
}
return sPort;
}
//对计算机的串口进行设置
public static SerialPort setSerialPort()
{
SerialPort sPort=null;
SerialPortEventListener listener=null;
PrintWriter pw=null;
BufferedReader br=null;
DataInputStream in=null;
String msg=null;
byte[] b=null;
SerialPortEvent sPortEvent=null;
sPort=getSerialPort();
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 run()
{
new sendCmd(sPort).start();
new receiveCmd(sPort).start();
}
public static void main(String[] args)
{
new test().start();
}
}
import java.io.*;
import javax.comm.*;
public class sendCmd extends Thread {
SerialPort sPort=null;
public sendCmd(SerialPort sPort) {
this.sPort=sPort;
}
public void send(SerialPort sPort)
{
String cmd1="AT+CGMI";//FALCOM A2D数据猫的AT命令
String cmd2="AT+CGMM";
String cmd3="AT+CGSN";
PrintWriter pw=null;
//发出AT命令给数据猫
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd1);
pw.flush();
pw.close();
System.out.println("command has been send");
} catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
}
public void run()
{
while(true)
{
try{sleep(100);}
catch(InterruptedException e){System.out.println(e.getMessage());}
send(sPort);
break;
}//while
}
}
import java.io.*;
import javax.comm.*;
import java.util.*;
public class receiveCmd extends Thread {
SerialPort sPort=null;
public receiveCmd(SerialPort sPort) {
this.sPort=sPort;
}
public void receive(SerialPort sPort)
{
BufferedReader br=null;
String msg=null;
SerialPortEvent sEvent=null;
DataInputStream in=null;
byte[] b=null;
//读数据猫的响应值
try{
in=new DataInputStream(sPort.getInputStream());
System.out.println("msg is receiveing...");
msg=in.readUTF();
in.close();
System.out.println("msg from modemn is:"+msg);
}
catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
//catch (TooManyListenersException e) {e.getMessage();}
}
public void run()
{
while(true)
{
try{sleep(100);}
catch(InterruptedException e){System.out.println(e.getMessage());}
receive(sPort);
break;
}
}
}
运行结果如下:
serial port is;COM1
baudrate:9600
databits:8
parity:0
stopbits:1
flow control mode:0
command has been send
msg is receiving.....
在从串口接收数据的时候,程序不能正常的执行下去了,不知道是怎么回事,请指教。
package serialporttest;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
import java.io.*;
import javax.comm.*;
import java.util.*;
public class test extends Thread {
private static SerialPort sPort=null;
public test() {
sPort=setSerialPort();
}
//得到计算机的串口
public static SerialPort getSerialPort()
{
SerialPort sPort=null;
CommPortIdentifier portID;
String owner=new String("modemn");
int keeptime=500;
Enumeration portList;
portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements())
{
portID=(CommPortIdentifier)portList.nextElement();
if(portID.getName().equals("COM1"))
try{sPort=(SerialPort)portID.open(owner,keeptime);}
catch(PortInUseException e){System.out.println(e.getMessage());}
}
return sPort;
}
//对计算机的串口进行设置
public static SerialPort setSerialPort()
{
SerialPort sPort=null;
SerialPortEventListener listener=null;
PrintWriter pw=null;
BufferedReader br=null;
DataInputStream in=null;
String msg=null;
byte[] b=null;
SerialPortEvent sPortEvent=null;
sPort=getSerialPort();
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 run()
{
new sendCmd(sPort).start();
new receiveCmd(sPort).start();
}
public static void main(String[] args)
{
new test().start();
}
}
import java.io.*;
import javax.comm.*;
public class sendCmd extends Thread {
SerialPort sPort=null;
public sendCmd(SerialPort sPort) {
this.sPort=sPort;
}
public void send(SerialPort sPort)
{
String cmd1="AT+CGMI";//FALCOM A2D数据猫的AT命令
String cmd2="AT+CGMM";
String cmd3="AT+CGSN";
PrintWriter pw=null;
//发出AT命令给数据猫
try{
pw=new PrintWriter(sPort.getOutputStream());
pw.println(cmd1);
pw.flush();
pw.close();
System.out.println("command has been send");
} catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
}
public void run()
{
while(true)
{
try{sleep(100);}
catch(InterruptedException e){System.out.println(e.getMessage());}
send(sPort);
break;
}//while
}
}
import java.io.*;
import javax.comm.*;
import java.util.*;
public class receiveCmd extends Thread {
SerialPort sPort=null;
public receiveCmd(SerialPort sPort) {
this.sPort=sPort;
}
public void receive(SerialPort sPort)
{
BufferedReader br=null;
String msg=null;
SerialPortEvent sEvent=null;
DataInputStream in=null;
byte[] b=null;
//读数据猫的响应值
try{
in=new DataInputStream(sPort.getInputStream());
System.out.println("msg is receiveing...");
msg=in.readUTF();
in.close();
System.out.println("msg from modemn is:"+msg);
}
catch(IOException e){System.out.println("catch exception when send cmd to modemn");
System.out.println(e.getMessage());}
//catch (TooManyListenersException e) {e.getMessage();}
}
public void run()
{
while(true)
{
try{sleep(100);}
catch(InterruptedException e){System.out.println(e.getMessage());}
receive(sPort);
break;
}
}
}
运行结果如下:
serial port is;COM1
baudrate:9600
databits:8
parity:0
stopbits:1
flow control mode:0
command has been send
msg is receiving.....
在从串口接收数据的时候,程序不能正常的执行下去了,不知道是怎么回事,请指教。
|
java的com开发包不是有例子可以参考吗?