亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:47日期:2024-07-02 09:18:52
內(nèi)容: 一個嵌入式系統(tǒng)通常需要通過串口與其主控系統(tǒng)進行全雙工通訊,譬如一個流水線控制系統(tǒng)需要不斷的接受從主控系統(tǒng)發(fā)送來的查詢和控制信息,并將執(zhí)行結(jié)果或查詢結(jié)果發(fā)送回主控系統(tǒng)。本文介紹了一個簡單的通過串口實現(xiàn)全雙工通訊的Java類庫,該類庫大大的簡化了對串口進行操作的過程。 本類庫主要包括:SerialBean.java (與其他應(yīng)用程序的接口), SerialBuffer.java (用來保存從串口所接收數(shù)據(jù)的緩沖區(qū)), ReadSerial.java (從串口讀取數(shù)據(jù)的程序)。另外本類庫還提供了一個例程SerialExample.java 作為示范。在下面的內(nèi)容中將逐一對這幾個部分進行詳細介紹。 1. SerialBeanSerialBean是本類庫與其他應(yīng)用程序的接口。該類庫中定義了SerialBean的構(gòu)造方法以及初始化串口,從串口讀取數(shù)據(jù),往串口寫入數(shù)據(jù)以及關(guān)閉串口的函數(shù)。具體介紹如下: public SerialBean(int PortID)本函數(shù)構(gòu)造一個指向特定串口的SerialBean,該串口由參數(shù)PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此類推。public int Initialize()本函數(shù)初始化所指定的串口并返回初始化結(jié)果。如果初始化成功返回1,否則返回-1。初始化的結(jié)果是該串口被SerialBean獨占性使用,其參數(shù)被設(shè)置為9600, N, 8, 1。如果串口被成功初始化,則打開一個進程讀取從串口傳入的數(shù)據(jù)并將其保存在緩沖區(qū)中。public String ReadPort(int Length)本函數(shù)從串口(緩沖區(qū))中讀取指定長度的一個字符串。參數(shù)Length指定所返回字符串的長度。public void WritePort(String Msg)本函數(shù)向串口發(fā)送一個字符串。參數(shù)Msg是需要發(fā)送的字符串。public void ClosePort()本函數(shù)停止串口檢測進程并關(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 informationreturn 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是本類庫中所定義的串口緩沖區(qū),它定義了往該緩沖區(qū)中寫入數(shù)據(jù)和從該緩沖區(qū)中讀取數(shù)據(jù)所需要的函數(shù)。 public synchronized String GetMsg(int Length)本函數(shù)從串口(緩沖區(qū))中讀取指定長度的一個字符串。參數(shù)Length指定所返回字符串的長度。public synchronized void PutChar(int c)本函數(shù)望串口緩沖區(qū)中寫入一個字符,參數(shù)c 是需要寫入的字符。在往緩沖區(qū)寫入數(shù)據(jù)或者是從緩沖區(qū)讀取數(shù)據(jù)的時候,必須保證數(shù)據(jù)的同步,因此GetMsg和PutChar函數(shù)均被聲明為synchronized并在具體實現(xiàn)中采取措施實現(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. ReadSerialReadSerial是一個進程,它不斷的從指定的串口讀取數(shù)據(jù)并將其存放到緩沖區(qū)中。 public ReadSerial(SerialBuffer SB, InputStream Port)本函數(shù)構(gòu)造一個ReadSerial進程,參數(shù)SB指定存放傳入數(shù)據(jù)的緩沖區(qū),參數(shù)Port指定從串口所接收的數(shù)據(jù)流。public void run()ReadSerial進程的主函數(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. SerialExampleSerialExample是本類庫所提供的一個例程。它所實現(xiàn)的功能是打開串口COM1,對其進行初始化,從串口讀取信息對其進行處理后將處理結(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 hereSerialBean SB = new SerialBean(1);String Msg;SB.Initialize();for (int i = 5; i
標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产成人亚洲综合在线 | 久草久操 | 国产色产综合色产在线观看视频 | 日韩午夜高清福利片在线观看 | 国产18页| 国产最新网址 | 久久久久在线 | 女同视频一区二区在线观看 | 香蕉在线观看999 | 你懂的免费网站 | 91国语精品自产拍在线观看性色 | 2022国产成人精彩在线视频 | 国产一区视频在线播放 | 日韩欧美一区二区三区不卡在线 | 99久久中文字幕伊人情人 | 国产一区二区免费在线观看 | av香港经典三级级 在线 | 色婷婷影院 | 日韩专区在线播放 | 日本中文字幕乱码免费 | 精品理论片一区二区三区 | 亚洲黄色三级网站 | 国产成人亚洲合集青青草原精品 | 狠狠五月天 | 国产黄色片视频 | 久久七 | 亚洲精品一区二区三区第四页 | 国产产一区二区三区久久毛片国语 | 国产精品免费网站 | 国产精品二区页在线播放 | 亚洲综合天堂网 | 在线亚州| 多女多p多杂交视频在线观看 | 成人午夜毛片 | 久久精品国产99久久香蕉 | 综合欧美日韩一区二区三区 | 国产视频首页 | 免费国产小视频在线观看 | 欧美一级免费观看 | 福利一区二区三区视频午夜观看 | 香蕉视频色 |