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

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

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

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

    利用Java實(shí)現(xiàn)串口全雙工通訊

    蔣清野      2001 年 5 月

    內(nèi)容:
    1. SerialBean
    2. SerialBuffer
    3. ReadSerial
    4. SerialExample


     

    一個(gè)嵌入式系統(tǒng)通常需要通過串口與其主控系統(tǒng)進(jìn)行全雙工通訊,譬如一個(gè)流水線控制系統(tǒng)需要不斷的接受從主控系統(tǒng)發(fā)送來(lái)的查詢和控制信息,并將執(zhí)行結(jié)果或查詢結(jié)果發(fā)送回主控系統(tǒng)。本文介紹了一個(gè)簡(jiǎn)單的通過串口實(shí)現(xiàn)全雙工通訊的Java類庫(kù),該類庫(kù)大大的簡(jiǎn)化了對(duì)串口進(jìn)行操作的過程。

    本類庫(kù)主要包括:SerialBean.java (與其他應(yīng)用程序的接口), SerialBuffer.java (用來(lái)保存從串口所接收數(shù)據(jù)的緩沖區(qū)), ReadSerial.java (從串口讀取數(shù)據(jù)的程序)。另外本類庫(kù)還提供了一個(gè)例程SerialExample.java 作為示范。在下面的內(nèi)容中將逐一對(duì)這幾個(gè)部分進(jìn)行詳細(xì)介紹。

    1. SerialBean
    SerialBean是本類庫(kù)與其他應(yīng)用程序的接口。該類庫(kù)中定義了SerialBean的構(gòu)造方法以及初始化串口,從串口讀取數(shù)據(jù),往串口寫入數(shù)據(jù)以及關(guān)閉串口的函數(shù)。具體介紹如下:

    public SerialBean(int PortID)
    本函數(shù)構(gòu)造一個(gè)指向特定串口的SerialBean,該串口由參數(shù)PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此類推。

    public int Initialize()
    本函數(shù)初始化所指定的串口并返回初始化結(jié)果。如果初始化成功返回1,否則返回-1。初始化的結(jié)果是該串口被SerialBean獨(dú)占性使用,其參數(shù)被設(shè)置為9600, N, 8, 1。如果串口被成功初始化,則打開一個(gè)進(jìn)程讀取從串口傳入的數(shù)據(jù)并將其保存在緩沖區(qū)中。

    public String ReadPort(int Length)
    本函數(shù)從串口(緩沖區(qū))中讀取指定長(zhǎng)度的一個(gè)字符串。參數(shù)Length指定所返回字符串的長(zhǎng)度。

    public void WritePort(String Msg)
    本函數(shù)向串口發(fā)送一個(gè)字符串。參數(shù)Msg是需要發(fā)送的字符串。

    public void ClosePort()
    本函數(shù)停止串口檢測(cè)進(jìn)程并關(guān)閉串口。

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

    2. SerialBuffer

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

    public synchronized String GetMsg(int Length)
    本函數(shù)從串口(緩沖區(qū))中讀取指定長(zhǎng)度的一個(gè)字符串。參數(shù)Length指定所返回字符串的長(zhǎng)度。

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

    在往緩沖區(qū)寫入數(shù)據(jù)或者是從緩沖區(qū)讀取數(shù)據(jù)的時(shí)候,必須保證數(shù)據(jù)的同步,因此GetMsg和PutChar函數(shù)均被聲明為synchronized并在具體實(shí)現(xiàn)中采取措施實(shí)現(xiàn)的數(shù)據(jù)的同步。

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

    3. ReadSerial
    ReadSerial是一個(gè)進(jìn)程,它不斷的從指定的串口讀取數(shù)據(jù)并將其存放到緩沖區(qū)中。

    public ReadSerial(SerialBuffer SB, InputStream Port)
    本函數(shù)構(gòu)造一個(gè)ReadSerial進(jìn)程,參數(shù)SB指定存放傳入數(shù)據(jù)的緩沖區(qū),參數(shù)Port指定從串口所接收的數(shù)據(jù)流。

    public void run()
    ReadSerial進(jìn)程的主函數(shù),它不斷的從指定的串口讀取數(shù)據(jù)并將其存放到緩沖區(qū)中。

    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是本類庫(kù)所提供的一個(gè)例程。它所實(shí)現(xiàn)的功能是打開串口COM1,對(duì)其進(jìn)行初始化,從串口讀取信息對(duì)其進(jìn)行處理后將處理結(jié)果發(fā)送到串口。

    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. 編譯與調(diào)試

    本類庫(kù)中使用了Java Communication API (javax.comm)。這是一個(gè)Java擴(kuò)展類庫(kù),并不包括在標(biāo)準(zhǔn)的Java SDK當(dāng)中。如果你尚未安裝這個(gè)擴(kuò)展類庫(kù)的話,你應(yīng)該從Sun公司的Java站點(diǎn)下載這個(gè)類庫(kù)并將其安裝在你的系統(tǒng)上。在所下載的包里面包括一個(gè)安裝說(shuō)明,如果你沒有正確安裝這個(gè)類庫(kù)及其運(yùn)行環(huán)境的話,運(yùn)行這個(gè)程序的時(shí)候你會(huì)找不到串口。

    正確安裝Java Communication API并將上述程序編譯通過以后,你可以按如下方法測(cè)試這個(gè)程序。如果你只有一臺(tái)機(jī)器,你可以利用一條RS-232電纜將COM1和COM2連接起來(lái),在COM1上運(yùn)行SerialExample,在COM2上運(yùn)行Windows提供的超級(jí)終端程序。如果你有兩臺(tái)機(jī)器的話,你可以利用一條RS-232電纜將兩臺(tái)機(jī)器的COM1(或者是COM2)連接起來(lái),在一端運(yùn)行例程,另外一端運(yùn)行Windows提供的超級(jí)終端程序。如果有必要的話,可以對(duì)SerialExample中所聲明的串口進(jìn)行相應(yīng)改動(dòng)。

    posted on 2005-05-09 09:15 jinfeng_wang 閱讀(1641) 評(píng)論(4)  編輯  收藏 所屬分類: java 、ZZ

    評(píng)論

    # re: 利用Java實(shí)現(xiàn)串口全雙工通訊(zz) 2005-09-02 11:16 laitouer
    感謝摟住!  回復(fù)  更多評(píng)論
      

    # re: 利用Java實(shí)現(xiàn)串口全雙工通訊(zz) 2005-10-01 01:01 陳朋奕
    這么好的文章怎么不頂呢?
    哈哈  回復(fù)  更多評(píng)論
      

    # re: 利用Java實(shí)現(xiàn)串口全雙工通訊(zz) 2006-03-08 22:03 gang
    唉,3.0 comm.jar怎么沒有windows版了??????  回復(fù)  更多評(píng)論
      

    # re: 利用Java實(shí)現(xiàn)串口全雙工通訊(zz) 2006-03-08 22:03 gang
    3.0 comm.jar 怎么沒有windows版的了???可惜,可惜  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 亚洲经典千人经典日产| 免费观看一级毛片| 亚洲 国产 图片| 无遮挡免费一区二区三区| 国产精品亚洲αv天堂无码| 99在线精品免费视频九九视| 亚洲中文字幕久久精品无码A | 人人狠狠综合久久亚洲婷婷| 91免费福利精品国产| 4hu四虎免费影院www| 亚洲妇女无套内射精| 国产成+人+综合+亚洲专| 亚洲av综合avav中文| 亚洲日本va中文字幕久久| xxxx日本免费| 成人爽a毛片免费| 亚洲男人天堂影院| 久久久久亚洲AV片无码| 中文字幕无码精品亚洲资源网| 国产羞羞的视频在线观看免费| 亚洲中文字幕无码中文| 亚洲免费在线观看视频| 国产亚洲精品国看不卡| 一级毛片直播亚洲| 国产zzjjzzjj视频全免费| 四虎成人免费网站在线| 成人免费看黄20分钟| 成人免费午夜无码视频| 久久午夜免费视频| 99在线视频免费观看视频| 成年人免费的视频| 黄在线观看www免费看| 国产91色综合久久免费分享| 182tv免费视视频线路一二三| 人妻巨大乳hd免费看| 猫咪免费人成在线网站| 人禽伦免费交视频播放| 久久国产美女免费观看精品| 久久亚洲精品高潮综合色a片| 亚洲人成网站免费播放| 亚洲欧洲视频在线观看|