<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    FORTUNE

    THE WAY TO THE MASTER...
    posts - 49, comments - 18, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java串口編程(起步

    Posted on 2006-03-03 11:00 fortune 閱讀(5829) 評論(6)  編輯  收藏 所屬分類: 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()
    本函數停止串口檢測進程并關閉串口。
    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();
         }
     }
    2. SerialBuffer

    SerialBuffer是本類庫中所定義的串口緩沖區,它定義了往該緩沖區中寫入數據和從該緩沖區中讀取數據所需要的函數。

    public synchronized String GetMsg(int Length)
    本函數從串口(緩沖區)中讀取指定長度的一個字符串。參數Length指定所返回字符串的長度。

    public synchronized void PutChar(int c)
    本函數望串口緩沖區中寫入一個字符,參數c 是需要寫入的字符。

    在往緩沖區寫入數據或者是從緩沖區讀取數據的時候,必須保證數據的同步,因此GetMsg和PutChar函數均被聲明為synchronized并在具體實現中采取措施實現的數據的同步。
    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();
       }
     }
       3. ReadSerial
    ReadSerial是一個進程,它不斷的從指定的串口讀取數據并將其存放到緩沖區中。

    public ReadSerial(SerialBuffer SB, InputStream Port)
    本函數構造一個ReadSerial進程,參數SB指定存放傳入數據的緩沖區,參數Port指定從串口所接收的數據流。

    public void run()
    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();
       }
     }
     本類庫中使用了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環境下編譯通過并成功運行。

    評論

    # re: java串口編程(起步  回復  更多評論   

    2006-11-28 15:58 by pc1ibm@yahoo.com.cn
    我拷貝了上面的代碼,作了一點修改。編譯通過。但,運行時拋出NoSuchMethod 異常。請執教。

    # re: java串口編程(起步  回復  更多評論   

    2007-10-18 10:23 by fenix
    直接拷貝上述代碼可用。試過了。

    # re: java串口編程(起步[未登錄]  回復  更多評論   

    2007-11-22 13:45 by robin
    請把整個批project的代碼貼出來好嗎?我試過了怎么不行啊,我在Eclipse里面試的。

    # re: java串口編程(起步[未登錄]  回復  更多評論   

    2008-03-05 16:43 by Felix
    我的臺機和筆記本都沒有rs232口,郁悶,我用兩個usb 轉 rs232 實現的連接.

    # re: java串口編程(起步  回復  更多評論   

    2008-04-17 14:58 by 加超
    文章寫的相當非常很棒,值得借鑒與學習,文章作者真是高手啊。。。

    # re: java串口編程(起步  回復  更多評論   

    2008-05-14 10:59 by tt
    Eclipse 下也可直接使用,寫的很好
    主站蜘蛛池模板: 男女免费观看在线爽爽爽视频| 99久久99久久精品免费观看| 免费看片A级毛片免费看| 亚洲国产精品综合福利专区| 99免费视频观看| 亚洲视频一区在线观看| 免费视频专区一国产盗摄| 亚洲国产日韩在线| 好爽…又高潮了毛片免费看| 亚洲黄页网在线观看| 日本高清免费不卡在线| 日韩在线观看视频免费| 久久久久久久亚洲精品| 国内精品一级毛片免费看| 精品日韩亚洲AV无码一区二区三区| 亚欧免费一级毛片| 亚洲1234区乱码| 日本免费网站在线观看| 一个人看的hd免费视频| 国产V亚洲V天堂无码久久久| 最近新韩国日本免费观看 | 久久精品国产亚洲一区二区| 全部免费毛片在线播放| 亚洲综合色7777情网站777| 破了亲妺妺的处免费视频国产| gogo免费在线观看| 久久99亚洲网美利坚合众国| 国外成人免费高清激情视频| 国产成人无码免费看片软件| 久久国产精品亚洲一区二区| 少妇高潮太爽了在线观看免费| 狠狠热精品免费观看| 久久伊人久久亚洲综合| 成年女人毛片免费播放视频m| 日韩在线观看免费完整版视频| 亚洲成a人片在线观看中文动漫| 手机在线毛片免费播放| 成全高清在线观看免费| 四虎必出精品亚洲高清| 亚洲V无码一区二区三区四区观看| 妞干网免费视频在线观看|