Picses' sky

Picses' sky
posts - 43, comments - 29, trackbacks - 0, articles - 24

利用java实现串口全双工通讯(转)

Posted on 2007-03-26 21:38 Matthew Chen 阅读(1493) 评论(0)  编辑  收藏

一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线
控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查
询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类
库,该类库大大的简化了对串口进行操作的过程。

本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java
(用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。
另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐
一对这几个部分进行详细介绍。

1. 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 starts a
* thread which consistently monitors the serial port. Any signal captured
* 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 incoming
* 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();
}
}

2. 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 incoming
* 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();
}
}

3. ReadSerial

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

public ReadSerial(SerialBuffer SB, InputStream Port)

本函数构造一个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) {}
}
}

4. 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();
}
}

5. 编译与调试

本类库中使用了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环境下编译通过并成功运行。

  以下是用Java读取串口的程序。

这个简单的程序包括以下文件:

       IMU.java (主程序)
       ReadBuffer.java (从缓冲区读取一个消息)
       ReadSerial.java (读取串口数据并放入缓冲区)
       SerialBuffer.java (缓冲区)
       WriteSerial.java (不断的往串口送星号'*')

测试程序:

       SendCom.java (将一个数据文件往串口发送)
       SEND.TXT (供测试用的数据文件)

在这个通讯程序中使用了一个简单的协议,既不同的消息之间用星号'*'作为分隔。这个程序中的问题是ReadSerial进程和WriteSerial进程不能够同时启动,出错信息是不能够打开串口,因为同样一个串口不能够同时被打开两次(在ReadSerial中声明了FileReader和在WriteSerial中声明了FileWriter)。这样是不能够实现全双工通讯的。

/*
*
* IMU.java 1.0
* Main Program for Serial Communication
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

import java.io.*;

class IMU
{
       public static void main(String[] args)
       {
               //TO DO: Add your JAVA codes here

               File ComPort = new File("COM1");

       SerialBuffer SB = new SerialBuffer();
       ReadSerial r1 = new ReadSerial(SB, ComPort);
       ReadBuffer r2 = new ReadBuffer(SB);
       WriteSerial r3 = new WriteSerial(ComPort);

       r1.start();
       r2.start();
       r3.start();
   }
}

/*
*
* ReadBuffer.java 1.0
* Program to Read the Serial Buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu

*
*/

import java.io.*;

public class ReadBuffer extends Thread
{
   private SerialBuffer ComBuffer;

   public ReadBuffer(SerialBuffer SB)
   {
       ComBuffer = SB;
   }

   public void run()
   {
               String Msg;

       while (true)
       {
               Msg = ComBuffer.GetMsg();
               System.out.println(Msg);
       }

   }
}

/*
*
* ReadSerial.java 1.0
* Program to read characters from the serial port and put it
* to the buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
*       qjiang@tsinghua.edu
*
*/

import java.io.*;

public class ReadSerial extends Thread
{
   private SerialBuffer ComBuffer;
   private File ComPort;

   public ReadSerial(SerialBuffer SB, File Port)
   {
       ComBuffer = SB;
       ComPort = Port;
   }

   public void run()
   {
               int c;
               try
               {
                       FileReader in = new FileReader(ComPort);

while (true)
           {
               c = in.read();
               ComBuffer.PutChar(c);
           }
               try
               {
                       FileReader in = new FileReader(ComPort);
               while (true)
           {
               c = in.read();

               ComBuffer.PutChar(c);
           }
       } catch (IOException e) {}
   }
}


/*
*
* SerialBuffer.java 1.0
* Class that implements the serial buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
*   American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu

*
*/

public class SerialBuffer
{
   private String Content = "";
   private String CurrentMsg, TempContent;
   private boolean available = false;

   public synchronized String GetMsg()
   {
       int SepMark;

       if ((SepMark = Content.indexOf('*')) == -1)
       {
               available = false;
               while (available == false)
               {
               try
               {
                   wait();
               } catch (InterruptedException e) { }
               }
               SepMark = Content.indexOf('*');
       }

       CurrentMsg = Content.substring(0, SepMark);
       TempContent = Content.substring(SepMark+1);
       Content = TempContent;
       notifyAll();
       return CurrentMsg;
   }

   public synchronized void PutChar(int c)
   {
       Character d = new Character((char) c);
       Content = Content.concat(d.toString());
       if (c == '*')
       {
               available = true;
       }
       notifyAll();
   }
}


/*
*
* WriteSerial.java 1.0
* Program to send a character to the serial port
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

import java.io.*;

public class WriteSerial extends Thread
{
   private SerialBuffer ComBuffer;
   private File ComPort;

   public WriteSerial(File Port)
   {
       ComPort = Port;
   }

   public void run()
   {
               int c;
               try
               {
                       FileWriter out = new FileWriter(ComPort);
               while (true)
           {
               out.write('*');
           }
       } catch (IOException e)
       {
               System.out.println(e.getMessage());
       }
   }
}


import java.io.*;

public class SendCom
{
   public static void main(String[] args)
   {

               File OutFile = new File("SEND.TXT");
               File ComPort = new File("COM2");

               int c;

               try
               {
                       FileReader in = new FileReader(OutFile);
                       FileWriter out = new FileWriter(ComPort);
                       while ((c = in.read()) != -1)
                               out.write(c);
                       in.close();
                       out.close();
               } catch (IOException e) {}

       }
}

 

SEND.TXT*

This is a sample of the data file for program testing. *

It should be in the same directory as the SendCom.class file.*

When you run this sample program, connect your COM1 and COM2 with a
serial cable so that you can test this program on one machine. If
you have two machines, you can connect the two machine via a serial
cable and test it. Modified the definition of ComPort in the program
if necessary. *

Thank you for testing this program. If you have any suggestions please
kindly let me know. *



这是一个通过JAVA的扩展包(javax.comm)从串口读取值的类,
  本类库(javax.comm)主要包括:
 SerialBean.java (与其他应用程序的接口),
 SerialBuffer.java (用来保存从串口所接收数据的缓冲区),
 ReadSerial.java (从串口读取数据的程序)。


 整个类的设计思路大概就是:
 实现一个线程一直在监听串口中是否有数据传送过来,
 如果有的话开始拍照,并取得照片后,直接打印出来。因为涉及到商业秘密,所以拍照函数和取得照片函数采用的是空函数,请见谅!
 若要运行此函数必须下载该扩展包,并加载到你的CLASSPATH下。当然,你若要测试并运行此类的话。可能要花费不少的精力。
 如果你只有一台机器,你可以利用一条RS-232电缆将COM1和COM2连接起来,在COM1上运行SerialExample,在COM2上运行Windows提供的超级终端程序。
 如果你有两台机器的话,你可以利用一条RS-232电缆将两台机器的COM1(或者是COM2)连接起来,在一端运行例程,另外一端运行Windows提供的超级终端程序。
 
该类有相当详细的注释。我想应该不难理解吧!

import java.io.*;
import java.awt.*;
import java.awt.print.*;
import javax.print.*;
import javax.comm.*; //这是一个Java扩展类库,并不包括在标准的Java SDK当中。
//如果你尚未安装这个扩展类那么你需要到sun公司的Java站点下载这个类库

class SuanKou implements Runnable{
  private java.io.FileOutputStream fos=null;
  private Thread th=null;
  private long shiJian=500;//设置间隔的时间
   //private int duanKou=1;//设置端口号
   private String Msg = null;
   private SerialBean SB=null;
   private int isSucces=-1;
 
   //构造函数
  public SuanKou(long sj,int dk) {
    System.out.println("构造函数");
    try{
    fos=new java.io.FileOutputStream("c:\\log.txt");
   
    this.shiJian=sj;//设定监听端口的时间
   
    //this.duanKou=dk;//端口号
   
    SB = new SerialBean(dk);//创建一个接口对象,参数为端口号
   
   isSucces = SB.Initialize();//初使化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1
  
    th=new Thread(this);//创建一个线程对象
  
    th.start();//创建SuanKou这个对象就开始启动线程
 }catch(java.io.FileNotFoundException fe){
  fe.printStackTrace();
 }catch(Exception e){
  e.printStackTrace();
 }
  }

//拍照函数
  public void snape() {
    System.out.println("拍照函数");
  }

//取得照片函数
  public void getPicture() {
    System.out.println("取得照片函数");

  }
 
  //实现线程接口的RUN方法
  public void run(){
    Thread.sleep(shiJian);
    this.ReadSuanKou();
  }
 
  //定期到串口读取数据和函数,如果能读到数据就调用snape(),getPicture(),函数,写日志,打印
  private void ReadSuanKou() {
  
   //如果初使化成功的话,
    if (isSucces == 1) {
    
      //从串口(缓冲区)中读取指定长度的一个字符串。(500)
     
      try{
      
      Msg = SB.ReadPort(500);

      //如果可以从串口读到数据的话

      if (Msg != null) {
        this.snape();//调用拍照函数
       
        this.getPicture();//调用取得照片函数
       
        java.text.SimpleDateFormat df=new java.text.SimpleDateFormat("EEEE-MMMM-dd-yyyy");

 Date date=new Date();

 String dateFormat=df.format(date);
 
        fos.write(dateFormat.getBytes());//在日志文件中记录当前时间
       
        fos.write(Msg.getBytes());//将读到的数据写到日志文件中
 
  //调用打印方法 注:若要打印,要打印的对象必须实现java.awt.print.Printable接口。即getPicture
 
  //返回的对象必须实现该接口,因为不知道getPicture返回什么对象,故这个打印功能无法实现,不过大概的思路是
 
  /** 1,需要一个打印服务对象。这可通过三种方式实现:
      在jdk1.4之前的版本,必须要实现java.awt.print.Printable接口
      或通过Toolkit.getDefaultToolkit().getPrintJob来获取打印服务对象;
      在jdk1.4中则还可以通过javax.print.PrintSerivceLookup来查找定位一个打印服务对象。

                  2、需要开始一个打印工作。这也有几种实现方法:在jdk1.4之前可以通过java.awt.print.PrintJob
   (jdk1.1提供的,现在已经很少用了)调用print或printAll方法开始打印工作;
   也可以通过java.awt.print.PrinterJob的printDialog显示打印对话框,
   然后通过print方法开始打印;在jdk1.4中则可以通过javax.print.ServiceUI的printDialog显示打印对话框,
   然后调用print方法开始一个打印工作。
 
  根据以上的提示去做,不会很难!
  */
 
  java.awt.print.PrinterJob pj=new java.awt.print.PrinterJob();
 
  pj.printDialog();//显示打印对话框
  pj.print();//开始打印
 
      }

      //SB.WritePort("OK");若要向串口返回一个结果可以调用该函数?

     
 }catch(Exception e){
 
  e.printStackTrace();

 }finally{
  try{
   fos.close();
    //停止串口检测进程并关闭串口。
        SB.ClosePort();
     
  }catch(Exception e){
   e.printStackTrace();
  }
 }

    }
    else {
      throw RuntimeException("读取串口数据时出错!");
    }

  }

 

  //主函数
  public static void main(String args[]) {
  
    new SuanKou(1000,1);//参数为间隔的时间,和端口号


  }
}


 

关键字:   Java Comm 串口    

本人因为项目开发的需要,需要PC机和硬件的通讯,而这个通讯通过Comm串口实现,而最好又是全双工的通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java (用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。
1. 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()
本函数停止串口检测进程并关闭串口。
2. SerialBuffer
SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。
public synchronized String GetMsg(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public synchronized void PutChar(int c)
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采取措施实现的数据的同步。
3. ReadSerial
ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
public ReadSerial(SerialBuffer SB, InputStream Port)
本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。
public void run()
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
4. SerialExample
SerialExample是本类库所提供的一个例程。它所实现的功能是打开串口COM1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
5. 编译与调试
本类库中使用了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环境下编译通过并成功运行。

如想要这几个文件的原代码,请留言,而且也能帮忙调试,因为关键在环境部署上,不能出错。

应广大读者的需要,本人把代码简单做了整理,特意发出来让大家相互学习!

本文介绍一个利用Java Comm API (javax.comm)实现串口全双工通讯的简单程序。这个程序首先打开并初始化一个串口,然后启动如下三个进程:ReadSerial进程从该串口读取数据并放入缓冲区,ReadBuffer进程从缓冲区读取数据并打印到屏幕, WriteSerial进程每5秒钟向该串口发送一个星号(*)。

在这个示例程序中使用了一个简单的协议,既不同的消息之间用星号'*'作为分隔。缓冲区程序根据是否收到星号作为存在等待处理的消息的判断依据。

Java Comm API不是标准的Java API,因此的标准的运行环境中并不提供这个包。如果你的系统上还没有安装这个包,你可以从SUN公司的网站下载。在这个包里面有一个安装指南,如果你没有正确安装这个包,可能你不能够正确运行这个例程。

这个简单的例程包括以下文件:

IMU.java (主程序)
ReadBuffer.java (从缓冲区读取一个消息)
ReadSerial.java (读取串口数据并放入缓冲区)
SerialBuffer.java (缓冲区)
WriteSerial.java (每5秒钟往串口送一个星号'*')

测试程序:

SendCom.java (将一个数据文件往串口发送)
SEND.TXT (供测试用的数据文件)

测试方法:

1 正确安装Java Comm API后编译本例程
2 将计算机的COM1和COM2用一条串口线连接起来
3 运行IMU。如果你这时候打开Windows自带的超级终端并连接到COM2的话,你应该能够看见有星号出现在超级终端的屏幕上。超级终端的参数设置为9600, N, 8, 1, none。
4 关闭超级终端,运行SendCom。这时候你应该能够从IMU的屏幕上看到数据文件里面的内容。

/*
*
* IMU.java 1.0
* Main Program for Serial Communication
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

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

class IMU
{

static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream out;
static InputStream in;

public static void main(String[] args)
{

try
{

//Declare the serial port, and open it.

portId = CommPortIdentifier.getPortIdentifier("COM1");
try
{

serialPort = (SerialPort) portId.open("IMU_App", 2000);

} catch (PortInUseException e)
{

System.out.println(e.getMessage());

}

//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)
{

System.out.println(e.getMessage());

}

//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)
{

System.out.println(e.getMessage());

}

} catch (NoSuchPortException e)
{

System.out.println(e.getMessage());

}

//Declare the serial buffer area, a thread to read from the seriial port,
//a thread to read from the serial buffer for processing, and a thread
//to write to the serial port.

SerialBuffer SB = new SerialBuffer();
ReadSerial r1 = new ReadSerial(SB, in);
ReadBuffer r2 = new ReadBuffer(SB);
WriteSerial r3 = new WriteSerial(out);

//Start all three threads.

r1.start();
r2.start();
r3.start();

}

}

/*
*
* SerialBuffer.java 1.0
* Class that implements the serial buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

public class SerialBuffer
{

private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;


//read a message from the serial buffer. The star character '*' is used
//as the seperation mark between different messages.

public synchronized String GetMsg()
{

int SepMark;

if ((SepMark = Content.indexOf('*')) == -1)
{

available = false;
while (available == false)
{

try
{
        wait();
} catch (InterruptedException e) { }

}
SepMark = Content.indexOf('*');

}

CurrentMsg = Content.substring(0, SepMark);
TempContent = Content.substring(SepMark+1);
Content = TempContent;
notifyAll();
return CurrentMsg;

}

//Put a character to the serial buffer

public synchronized void PutChar(int c)
{

Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (c == '*')
{
        available = true;
}
notifyAll();

}

}

/*
*
* ReadSerial.java 1.0
* Program to read characters from the serial port and put it
* to the buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

import java.io.*;

public class ReadSerial extends Thread
{

private SerialBuffer ComBuffer;
private InputStream ComPort;

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) {}

}

}

/*
*
* ReadBuffer.java 1.0
* Program to Read the Serial Buffer
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

import java.io.*;

public class ReadBuffer extends Thread
{

private SerialBuffer ComBuffer;

public ReadBuffer(SerialBuffer SB)
{
        ComBuffer = SB;
}

public void run()
{

String Msg;
while (true)
{
        Msg = ComBuffer.GetMsg();
        System.out.println(Msg);
}

}

}

/*
*
* WriteSerial.java 1.0
* Program to send a star to the serial port every 5 seconds.
*
* Created: March 27, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
* qjiang@tsinghua.edu
*
*/

import java.io.*;

public class WriteSerial extends Thread
{

private SerialBuffer ComBuffer;
private OutputStream ComPort;

public WriteSerial(OutputStream Port)
{
        ComPort = Port;
}

public void run()
{

int c;
try
{

while (true)
{

ComPort.write('*');
try
{
        wait(5000);
} catch (InterruptedException e) { }

}

} catch (IOException e)
{
        System.out.println(e.getMessage());
}

}

}

/*
*
* SendCom.java 1.0
*
* Project: Java Based Information Exchange Support System
* Onboard Plug-in System
* Sending data through serial port
*
* Created: March 15, 2001
*
* Author : Qingye Jiang (John)
* American GNC Corporation
* 888 Easy St, Simi Valley CA 93065-1812
*
* Contact: (805) 582-0582 (Tel), (805) 582-0098 (Fax)
*
*/

import java.io.*;

public class SendCom
{

public static void main(String[] args)
{

File OutFile = new File("SEND.TXT");
File ComPort = new File("COM2");

int c;

try
{

FileReader in = new FileReader(OutFile);
FileWriter out = new FileWriter(ComPort);
while ((c = in.read()) != -1)
        out.write(c);
in.close();
out.close();

} catch (IOException e) {}

}

}

SEND.TXT*

This is a sample of the data file for program testing. *

It should be in the same directory as the SendCom.class file.*

When you run this sample program, connect your COM1 and COM2 with a
serial cable so that you can test this program on one machine. If
you have two machines, you can connect the two machine via a serial
cable and test it. Modified the definition of ComPort in the program
if necessary. *

Thank you for testing this program. If you have any suggestions please
kindly let me know. *

小结:

在上面的例程中,大多数的程序仅对我前天发的《一个使用Java读取串口的程序》
一文做了小篇幅的改动,有几个程序和数据文件都没有改动。但是为了本文的完整
性,仍然将雷同的内容也贴了一遍,还望斑竹多多见谅。

这个例程和前面一个例程的区别在于前面一个例程使用了文件IO,而本例程使用了
Comm API。在C语言里面用fopen()函数来打开串口是可读也可写的,但是在Java里
面声明了File以后并不立即打开文件,文件是在声明FileReader或者FileWriter时
才打开的。由于串口不能同时被打开两次,所以读操作和写操作不能够同时进行,
不能够实现全双工通讯。

Comm API虽然能够实现全双工通讯,但是由于它不是标准的Java API,代码的可移
植性难以保证。如果程序并不要求实现全双工的话,我认为利用文件操作不失为一
个好办法。


只有注册用户登录后才能发表评论。


网站导航: