JAVA读写串口Bean范例

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()
本函数停止串口检测进程并关闭串口。

隐藏行号 复制代码 Source
  1. package serial;
  2.  import java.io.*;
  3.  import java.util.*;
  4.  import javax.comm.*;
  5.  
  6.  public class SerialBean
  7.  {
  8.    static String PortName;
  9.    CommPortIdentifier portId;
  10.    SerialPort serialPort;
  11.    static OutputStream out;
  12.    static InputStream  in;
  13.    SerialBuffer SB;
  14.    ReadSerial   RT;
  15.      
  16.      public SerialBean(int PortID)
  17.      {
  18.        PortName = "COM" + PortID;
  19.      }
  20.      
  21.      public int Initialize()
  22.      {
  23.        int InitSuccess = 1;
  24.        int InitFail    = -1;
  25.      try
  26.      {
  27.        portId = CommPortIdentifier.getPortIdentifier(PortName);
  28.        try
  29.        {
  30.          serialPort = (SerialPort)
  31.          portId.open("Serial_Communication", 2000);
  32.        } catch (PortInUseException e)
  33.        {
  34.          return InitFail;
  35.        }
  36.        //Use InputStream in to read from the serial port, and OutputStream
  37.        //out to write to the serial port.
  38.        try
  39.        {
  40.          in  = serialPort.getInputStream();
  41.          out = serialPort.getOutputStream();
  42.        } catch (IOException e)
  43.        {
  44.          return InitFail;
  45.        }
  46.        //Initialize the communication parameters to 9600, 8, 1, none.
  47.        try
  48.        {
  49.           serialPort.setSerialPortParams(9600,
  50.                SerialPort.DATABITS_8,
  51.                SerialPort.STOPBITS_1,
  52.                SerialPort.PARITY_NONE);
  53.        } catch (UnsupportedCommOperationException e)
  54.        {
  55.          return InitFail;
  56.        }
  57.      } catch (NoSuchPortException e)
  58.      {
  59.        return InitFail;
  60.      }
  61.      // when successfully open the serial port,  create a new serial buffer,
  62.      // then create a thread that consistently accepts incoming signals from
  63.      // the serial port. Incoming signals are stored in the serial buffer.
  64.      SB = new SerialBuffer();
  65.      RT = new ReadSerial(SB, in);
  66.      RT.start();
  67.      // return success information
  68.      return InitSuccess;
  69.      }
  70.      
  71.      public String ReadPort(int Length)
  72.      {
  73.        String Msg;
  74.        Msg = SB.GetMsg(Length);
  75.        return Msg;
  76.      }
  77.      
  78.      public void WritePort(String Msg)
  79.      {
  80.        int c;
  81.        try
  82.        {
  83.          for (int i = 0; i < Msg.length(); i++)
  84.            out.write(Msg.charAt(i));
  85.        } catch (IOException e)  {}
  86.      }
  87.      
  88.      public void ClosePort()
  89.      {
  90.        RT.stop();
  91.        serialPort.close();
  92.      }
  93.  }

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

隐藏行号 复制代码 Source
  1. package serial;
  2.  
  3.  public class SerialBuffer
  4.  {
  5.    private String Content = "";
  6.    private String CurrentMsg, TempContent;
  7.    private boolean available = false;
  8.    private int LengthNeeded = 1;
  9.      
  10.    public synchronized String GetMsg(int Length)
  11.    {
  12.      LengthNeeded = Length;
  13.      notifyAll();
  14.      if (LengthNeeded > Content.length())
  15.      {
  16.        available = false;
  17.        while (available == false)
  18.        {
  19.          try
  20.          {
  21.            wait();
  22.          } catch (InterruptedException e) { }
  23.        }
  24.      }
  25.      CurrentMsg  = Content.substring(0, LengthNeeded);
  26.      TempContent = Content.substring(LengthNeeded);
  27.      Content = TempContent;
  28.      LengthNeeded = 1;
  29.      notifyAll();
  30.      return CurrentMsg;
  31.    }
  32.      
  33.    public synchronized void PutChar(int c)
  34.    {
  35.      Character d = new Character((char) c);
  36.      Content = Content.concat(d.toString());
  37.      if (LengthNeeded < Content.length())
  38.      {
  39.        available = true;
  40.      }
  41.      notifyAll();
  42.    }
  43.  }

 

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

隐藏行号 复制代码 Source
  1. public void run()
  2. ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
  3. package serial;
  4.  import java.io.*;
  5.  
  6.  public class ReadSerial extends Thread
  7.  {
  8.    private SerialBuffer ComBuffer;
  9.    private InputStream ComPort;
  10.      
  11.    public ReadSerial(SerialBuffer SB, InputStream Port)
  12.    {
  13.      ComBuffer = SB;
  14.      ComPort = Port;
  15.    }
  16.    public void run()
  17.    {
  18.      int c;
  19.      try
  20.      {
  21.        while (true)
  22.        {
  23.          c = ComPort.read();
  24.          ComBuffer.PutChar(c);
  25.        }
  26.      } catch (IOException e) {}
  27.    }
  28.  }

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

隐藏行号 复制代码 Source
  1. import serial.*;
  2.  import java.io.*;
  3.  
  4.  class SerialExample
  5.  {
  6.    public static void main(String[] args)
  7.    {
  8.      //TO DO: Add your JAVA codes here
  9.      SerialBean SB = new SerialBean(1);
  10.      String Msg;
  11.      SB.Initialize();
  12.      for (int i = 5; i <= 10; i++)
  13.      {
  14.        Msg = SB.ReadPort(i);
  15.        SB.WritePort("Reply: " + Msg);
  16.      }
  17.      SB.ClosePort();
  18.    }
  19.  }

本类库中使用了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,bean,String,import,character,border)