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

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

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

    談笑有鴻儒,往來無白丁

    在恰當的時間、地點以恰當的方式表達給恰當的人...  閱讀的時候請注意分類,佛曰我日里面是談笑文章,其他是各個分類的文章,積極的熱情投入到寫博的隊伍中來,支持blogjava做大做強!向dudu站長致敬>> > 我的微博敬請收聽

    [轉載]利用Java實現串口全雙工通訊

    發信人: qyjohn (Sweet Potato -- 成功戒BBS中...), 信區: Java
    標 題: 利用Java實現串口全雙工通訊 (投稿)
    發信站: BBS 水木清華站 (Sat Mar 31 06:14:40 2001)

    利用Java實現串口全雙工通訊 (投稿)

    Qingye Jiang (John)
    SMTH ID: qyjohn
    E-mail : qjiang@tsinghua.edu

    一個嵌入式系統通常需要通過串口與其主控系統進行全雙工通訊,譬如一個流水線
    控制系統需要不斷的接受從主控系統發送來的查詢和控制信息,并將執行結果或查
    詢結果發送回主控系統。本文介紹了一個簡單的通過串口實現全雙工通訊的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環境下編譯通過并成功運行。

    --
    ("`-''-/").___..--''"`-._ 云與清風常擁有,
    `6_ 6 ) `-. ( ).`-.__.`) 冰雪知音世難求。
    (_Y_.)' ._ ) `._ `. ``-..- 擊節縱歌相對笑,
    _..`--'_..-_/ /--'_.' ,' 案上詩書杯中酒。
    (il),-'' (li),' ((!.-' 2000.12.31
    ______________________________________________________________________

    ※ 來源:·BBS 水木清華站 smth.org·[FROM: 64.166.188.154]

    需要源碼的留下郵箱啊。

    posted on 2006-09-25 11:30 壞男孩 閱讀(241) 評論(0)  編輯  收藏 所屬分類: java命令學習
    主站蜘蛛池模板: 青娱乐在线免费观看视频| 无码国产精品一区二区免费式芒果 | 免费看黄视频网站| 亚洲av成人一区二区三区观看在线 | 四虎影在线永久免费观看| 中文字幕成人免费高清在线视频| 亚洲精品**中文毛片| 免费日韩在线视频| 久艹视频在线免费观看| 亚洲av永久无码一区二区三区 | 久久亚洲精品无码AV红樱桃| 免费黄色app网站| 免费看无码特级毛片| 亚洲日韩一中文字暮| 亚洲国产无套无码av电影| 成年女人午夜毛片免费看| 精品免费tv久久久久久久| 亚洲av日韩综合一区久热| 久久久久亚洲AV无码网站| 亚洲成a人一区二区三区| 国产免费不卡v片在线观看| 两个人看的www视频免费完整版| 中文字幕乱码亚洲精品一区| 亚洲成AV人在线观看天堂无码| 国产精品久免费的黄网站| xxxx日本免费| a级毛片免费全部播放无码| 狼人大香伊蕉国产WWW亚洲| 亚洲精品美女在线观看播放| 亚洲中文久久精品无码| 国产高清在线免费视频| 18禁免费无码无遮挡不卡网站 | 亚洲日本乱码一区二区在线二产线 | 国产婷婷综合丁香亚洲欧洲| 婷婷亚洲综合五月天小说| 国产成人亚洲影院在线观看| 好大好深好猛好爽视频免费| 久草视频在线免费| 日本免费一区二区三区| 中文字幕免费不卡二区| 一个人看的免费观看日本视频www|