金笛工业手机论坛  

返回   金笛工业手机论坛 > 开发者乐园 > 短信开发资料

短信开发资料 短信开发技巧,代码分析

回复
 
LinkBack 主题工具 显示模式
旧 2009-10-20   #1 (permalink)
高级会员
 
注册日期: 2009-07-09
住址: 亦庄经济开发区2号院大族广场1502
帖子: 526
susu8988 正向着好的方向发展
默认 Java串行端口通讯技术慨论

串行通讯协议有很多种,像RS232,RS485,RS422,甚至现今流行的USB等都是串行通讯协议。而串行通讯技术的应用无处不在。可能大家见的最多就是电脑的串口与Modem的通讯。记得在PC机刚开始在中国流行起来时(大约是在90年代前五年),那时甚至有人用一条串行线进行两台电脑之间的数据共享。除了这些,手机,PDA,USB鼠标、键盘等等都是以串行通讯的方式与电脑连接。而笔者工作性质的关系,所接触到的就更多了,像多串口卡,各种种类的具有串口通讯接口的检测与测量仪器,串口通讯的网络设备等。
虽然串行通讯有很多种,但笔者所知的在整个电子通讯产品方面,以RS232的通讯方式最为多见。虽然USB接口的电子产品也是层出不穷,但了解一下Java在串行通讯方面的技术还有有必要的,说不定有哪位读者还想用此技术写一个PDA与电脑之间数据共享的程序呢。
本文主要以RS232为主来讲解Java的串行通讯技术。
RS232通讯基础

  RS-232-C(又称 EIA RS-232-C,以下简称RS232)是在1970年由美国电子工业协会(EIA)联合贝尔系统、调制解调器厂家及计算机终端生产厂家共同制定的用于串行通讯的标准。RS232是一个全双工的通讯协议,它可以同时进行数据接收和发送的工作。RS232的端口通常有两种:9针(DB9)和25针(DB25)。

  DB9和DB25的常用针脚定义







  常见的边线方式

  常见的通讯方式是三线式,这种方式是将两个RS232设备的发送端(TXD)和接收端(RXD)及接地端(GND)互相连接,也是许多读者所知道的连接方式:







  这种方式分别将两端的RS232接口的2--3,3---2,5(7)---5(7)针脚连接起来。其中2是数据接收线(RXD),3是数据发送线(TXD),5(7)是接地(RND)。如果有一台式PC,和一部NoteBook电脑,就可以用这种方式连线了。用三线式可以将大多数的RS232设备连接起来。但如果你认死了2--3,3--2,5(7)--5(7)对接这个理,会发现在连某些RS232设备时并不奏效。这是因为有些设备在电路内部已将2和3线调换过来了,你只要2,3,5(7)针一一对应就行了。



  小技巧:如何辨别TXD和RXD端口?

  搞电子的人手边应该常备一个电表,用来测测电压,电阻什么的会很有用。你只要分别测一下RS232端口的2--5或3--5针脚之间的电压,通常TXD针脚与GND之间会有3~15V左右的负电压,表示它是TXD针脚。




安装Java Communications API

  Sun的J2SE中并没有直接提供以上提到的任何一种串行通讯协议的开发包,而是以独立的jar包形式发布在java.sun.com网站上(从这里下载)----即comm.jar,称之为Javatm Communications API,它是J2SE的标准扩展。comm.jar并不是最近才有,早在1998年时,sun就已经发布了这个开发包。comm.jar分别提供了对常用的RS232串行端口和IEEE1284并行端口通讯的支持。目前sun发布的comm.jar只有Windows和Solaris平台两个版本,如果你需要Linux平台下的,可以在http://www.geeksville.com/~kevinh/linuxcomm.html找到。

  在使用comm.jar之前,必须知道如何安装它。这也是困扰许多初学java RS232通讯者的一个难题。如果我们电脑上安装了JDK, 它将同时为我们安装一份JRE(Java Runtime Entironment),通常我们运行程序时都是以JRE来运行的。所以以下的安装适用于JRE。如果你是用JDK来运行程序的,请将相应的<JRE_HOME>改成<JDK_HOME>。

  下载了comm.jar开发包后,与之一起的还有两个重要的文件,win32com.dll和javax.comm.properties。 comm.jar提供了通讯用的java API,而win32com.dll提供了供comm.jar调用的本地驱动接口。而javax.comm.properties是这个驱动的类配置文件。首先将comm.jar复制到<JRE_HOME>\lib\ext目录。再将win21com.dll复制到你的RS232应用程序运行的目录,即user.dir。然后将javax.comm.properties复制到<JRE_HOME>\lib目录。

  通讯前的准备

  如果你手头上没有现成的提供了标准RS232串口的设备,你可以将自己的电脑模拟成两台不同的串口设备。通常电脑主机后面的面板提供了两个9针的串口,请将这两个串口的2,3,5脚按前面介绍的方法连接。电子市场都有现成的连接头卖,请不要买那种封装的严严实实的接头,而要买用螺丝封装可以拆开的连接头,这样可以方便自己根据需要连接各个针脚。

  Comm API基础

  我无意于在此详细描述Comm API每个类和接口的用法,但我会介绍Comm API的类结构和几个重要的API用法。

  所有的comm API位于javax.comm包下面。从Comm API的javadoc来看,它介绍给我们的只有区区以下13个类或接口:



javax.comm.CommDriver
javax.comm.CommPort
javax.comm.ParallelPort
javax.comm.SerialPort
javax.comm.CommPortIdentifier
javax.comm.CommPortOwnershipListener
javax.comm.ParallelPortEvent
javax.comm.SerialPortEvent
javax.comm.ParallelPortEventListener (extends java.util.EventListener)
javax.comm.SerialPortEventListener (extends java.util.EventListener)
javax.comm.NoSuchPortException
javax.comm.PortInUseException
javax.comm.UnsupportedCommOperationException



下面讲解一下几个主要类或接口。

  1.枚举出系统所有的RS232端口

  在开始使用RS232端口通讯之前,我们想知道系统有哪些端口是可用的,以下代码列出系统中所有可用的RS232端口:


Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while (en.hasMoreElements())
{
portId = (CommPortIdentifier) en.nextElement();
/*如果端口类型是串口,则打印出其端口信息*/
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portId.getName());
}
}




  在我的电脑上以上程序输出以下结果:

  COM1
  COM2

  CommPortIdentifier类的getPortIdentifiers方法可以找到系统所有的串口,每个串口对应一个CommPortIdentifier类的实例。

  2.打开端口

  如果你使用端口,必须先打开它。



try{
CommPort serialPort = portId.open("My App", 60);
/* 从端口中读取数据*/
InputStream input = serialPort.getInputStream();
input.read(...);
/* 往端口中写数据*/
OutputStream output = serialPort.getOutputStream();
output.write(...)
...
}catch(PortInUseException ex)
{ ... }




  通过CommPortIdentifier的open方法可以返回一个CommPort对象。open方法有两个参数,第一个是String,通常设置为你的应用程序的名字。第二个参数是时间,即开启端口超时的毫秒数。当端口被另外的应用程序占用时,将抛出PortInUseException异常。

  在这里CommPortIdentifier类和CommPort类有什么区别呢?其实它们两者是一一对应的关系。CommPortIdentifier主要负责端口的初始化和开启,以及管理它们的占有权。而CommPort则是跟实际的输入和输出功能有关的。通过CommPort的getInputStream()可以取得端口的输入流,它是java.io.InputStream接口的一个实例。我们可以用标准的InputStream的操作接口来读取流中的数据,就像通过FileInputSteam读取文件的内容一样。相应的,CommPort的getOutputStream可以获得端口的输出流,这样就可以往串口输出数据了。

  3. 关闭端口

  使用完的端口,必须记得将其关闭,这样可以让其它的程序有机会使用它,不然其它程序使用该端口时可能会抛出端口正在使用中的错误。很奇怪的是,CommPortIdentifier类只提供了开启端口的方法,而要关闭端口,则要调用CommPort类的close()方法。

通讯方式

  CommPort的输入流的读取方式与文件的输入流有些不一样,那就是你可能永远不知这个InputStream何时结束,除非对方的OutputStream向你发送了一个特定数据表示发送结束,你收到这个特定字符后,再行关闭你的InputStream。而comm.jar提供了两种灵活的方式让你读取数据。
1. 轮询方式(Polling)

  举个例子,你同GF相约一起出门去看电影,但你的GF好打扮,这一打扮可能就是半小时甚至一小时以上。这时你就耐不住了,每两分钟就催问一次“好了没?”,如此这样,直到你的GF说OK了才算完。这个就叫轮询(Polling)。

  在程序中,轮询通常设计成一个封闭的循环,当满足某个条件时即结束循环。刚才那个例子中,你的GF说“OK了!”,这个就是结束你轮询的条件。在单线程的程序中,当循环一直执行某项任务而又无法预知它何时结束时,此时你的程序看起来可能就像死机一样。在VB程序中,这个问题可以用在循环结构中插入一个doEvent语句来解决。而Java中,最好的方式是使用线程,就像以下代码片断一样。



public TestPort extend Thread
{
...
InputStream input = serialPort.getInputStream();
StringBuffer buf = new StringBuffer();
boolean stopped = false;
...
public void run()
{
try {
while( !stopped )
int ch = input.read();
if ( ch=='q' || ch=='Q' )
{
/* 结束读取,关闭端口...*/
stopped = true;
...
}
else
{
buf.append((char)ch);
...
}
}catch (InterruptedException e) { }
}

}




  2. 监听方式(listening)

  Comm API支持标准的Java Bean型的事件模型。也就是说,你可以使用类似AddXXXListener这样的方法为一个串口注册自己的监听器,以监听方式进行数据读取。

  如要对端口监听,你必须先取得CommPortIdentifier类的一个实例,
CommPort serialPort = portId.open("My App", 60);

  从而取得SerialPort,再调用它的addEventListener方法为它添加监听器,
serialPort.addEventListener(new MyPortListener());

  SerialPort的监听器必须继承于SerialPortEventListener接口。当有任何SerialPort的事件发生时,将自动调用监听器中的serialEvent方法。Serial Event有以下几种类型:

  BI - 通讯中断.
  CD - 载波检测.
  CTS - 清除发送.
  DATA_AVAILABLE - 有数据到达.
  DSR - 数据设备准备好.
  FE - 帧错误.
  OE - 溢位错误.
  OUTPUT_BUFFER_EMPTY - 输出缓冲区已清空.
  PE - 奇偶校验错.
  RI -  振铃指示.
 
  下面是一个监听器的示例:



public void MyPortListener implements SerialPortEventListener
{
 public void serialEvent(SerialPortEvent evt)
 {
  switch (evt.getEventType())
  {
   case SerialPortEvent.CTS :
    System.out.println("CTS event occured.");
    break;
   case SerialPortEvent.CD :
    System.out.println("CD event occured.");
    break;
   case SerialPortEvent.BI :
    System.out.println("BI event occured.");
    break;
   case SerialPortEvent.DSR :
    System.out.println("DSR event occured.");
    break;
   case SerialPortEvent.FE :
    System.out.println("FE event occured.");
    break;
   case SerialPortEvent.OE :
    System.out.println("OE event occured.");
    break;
   case SerialPortEvent.PE :
    System.out.println("PE event occured.");
    break;
   case SerialPortEvent.RI :
    System.out.println("RI event occured.");
    break;
   case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
    System.out.println("OUTPUT_BUFFER_EMPTY event occured.");
    break;
   case SerialPortEvent.DATA_AVAILABLE :
    System.out.println("DATA_AVAILABLE event occured.");
    int ch;
    StringBuffer buf = new StringBuffer();
    InputStream input = serialPort.getInputStream
    try { 
     while ( (ch=input.read()) > 0) {
      buf.append((char)ch);
     }
     System.out.print(buf);
    } catch (IOException e) {}
    break; 
   }
}




  这个监听器只是简单打印每个发生的事件名称。而对于大多数应用程序来说,通常关心是DATA_AVAILABLE事件,当数据从外部设备传送到端口上来时将触发此事件。此时就可以使用前面提到过的方法,serialPort.getInputStream()来从InputStream中读取数据了。


Windows下设置及简单的串口程序

1、下载java Communications api开发包。
2、将win32com.dll拷贝入C:\j2sdk1.4.2_04\bin

3、将comm.jar拷贝入C:\j2sdk1.4.2_04\jre\lib\ext

4、将javax.comm.properties拷贝入C:\j2sdk1.4.2_04\jre\lib

5、编译CommTest.java文件

import java.io.*;

import java.util.*;

import javax.comm.*;

public class CommTest{

public static void main(String[] args){

SerialPort serialPort=null;

DataOutputStream doutput=null;

InputStream inputStream;

CommPortIdentifier portId=null;

String messageString="hello \n";

try{

portId=CommPortIdentifier.getPortIdentifier("COM1");

}catch(NoSuchPortException ne) {

System.out.println("ne"); ne.printStackTrace();

}

try{

serialPort=(SerialPort) portId.open("TestComm", 5);

OutputStream output = serialPort.getOutputStream();

doutput=new DataOutputStream(output);

inputStream = serialPort.getInputStream();

}catch(PortInUseException ex) {

System.out.println("ex"); ex.printStackTrace();

}catch(IOException ie) {

System.out.println("ie");

ie.printStackTrace();

//serialPort.close();

}

try {

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

} catch (UnsupportedCommOperationException e) {}

}

try {

doutput.write(messageString.getBytes());

} catch (IOException e) {}

}

6、串口打开后,用InputStream和DataOutputStream读写就可以了。

7、由于串口为共享资源,所以在设计程序时应采用单例模式。

简单的通过串口实现全双工通讯的Java类库

一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java (用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。

SerialBean

SerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:

public SerialBean(int PortID)
本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。
public int Initialize()
本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600, N, 8, 1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。
public String ReadPort(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public void WritePort(String Msg)
本函数向串口发送一个字符串。参数Msg是需要发送的字符串。
public void ClosePort()
本函数停止串口检测进程并关闭串口。


SerialBean的源代码如下:

package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full dulplex
* information exchange through the srial port.
*
*/
public class SerialBean
{
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
SerialBuffer SB;
ReadSerial RT;
/**
*
* Constructor
*
* @param PortID the ID of the serial to be used. 1 for COM1,
* 2 for COM2, etc.
*
*/
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize()
{
int InitSuccess = 1;
int InitFail = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public String ReadPort(int Length)
{
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg The string to be sent.
*
*/
public void WritePort(String Msg)
{
int c;
try
{
for (int i = 0; i < Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e) {}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort()
{
RT.stop();
serialPort.close();
}
}









SerialBuffer

SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。

public synchronized String GetMsg(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public synchronized void PutChar(int c)
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采取措施实现的数据的同步。


SerialBuffer的源代码如下:

package serial;
/**
*
* This class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length)
{
LengthNeeded = Length;
notifyAll();
if (LengthNeeded > Content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded < Content.length())
{
available = true;
}
notifyAll();
}
}









ReadSerial

ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。

public ReadSerial(SerialBuffer SB, InputStreamPort)
本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。
public void run()
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。


ReadSerial的源代码如下:

package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB The buffer to save the incoming messages.
* @param Port The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
while (true)
{
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}









SerialExample

SerialExample是本类库所提供的一个例程。它所实现的功能是打开串口COM1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。

import serial.*;
import java.io.*;
/**
*
* This is an example of how to use the SerialBean. It opens COM1 and reads
* six messages with different length form the serial port.
*
*/
class SerialExample
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1);
String Msg;
SB.Initialize();
for (int i = 5; i <= 10; i++)
{
Msg = SB.ReadPort(i);
SB.WritePort("Reply: " + Msg);
}
SB.ClosePort();
}
}









编译与调试

本类库中使用了Java Communication API (javax.comm)。这是一个Java扩展类库,并不包括在标准的Java SDK当中。如果你尚未安装这个扩展类库的话,你应该从Sun公司的Java站点下载这个类库并将其安装在你的系统上。在所下载的包里面包括一个安装说明,如果你没有正确安装这个类库及其运行环境的话,运行这个程序的时候你会找不到串口。

正确安装Java Communication API并将上述程序编译通过以后,你可以按如下方法测试这个程序。如果你只有一台机器,你可以利用一条RS-232电缆将COM1和COM2连接起来,在COM1上运行SerialExample,在COM2上运行Windows提供的超级终端程序。如果你有两台机器的话,你可以利用一条RS-232电缆将两台机器的COM1(或者是COM2)连接起来,在一端运行例程,另外一端运行Windows提供的超级终端程序。如果有必要的话,可以对SerialExample中所声明的串口进行相应改动。

本程序在Windows 2000 + Java SDK 1.3环境下编译通过并成功运行。



本文来自CSDN博客,转载请标明出处:Java串行端口通讯技术慨论 - 小生物语 - CSDN博客
__________________
地址:北京亦庄经济开发区荣华南路2号院大族广场T2栋1502

电话:010-82356575/76/77转6070 联系人:苏秋英

手机微信同号:13811870548 QQ: 1170923055
susu8988 当前在线   回复时引用此帖
回复

书签


当前查看此主题的会员: 1 (0 位会员和 1 位游客)
 

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为北京时间。现在的时间是 11:51


Powered by vBulletin® 版本 3.8.3
版权所有 ©2000 - 2024,Jelsoft Enterprises Ltd.