日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

利用Java實現串口全雙工通訊

瀏覽:159日期:2024-07-02 09:18:52
內容: 一個嵌入式系統通常需要通過串口與其主控系統進行全雙工通訊,譬如一個流水線控制系統需要不斷的接受從主控系統發送來的查詢和控制信息,并將執行結果或查詢結果發送回主控系統。本文介紹了一個簡單的通過串口實現全雙工通訊的Java類庫,該類庫大大的簡化了對串口進行操作的過程。 本類庫主要包括:SerialBean.java (與其他應用程序的接口), SerialBuffer.java (用來保存從串口所接收數據的緩沖區), ReadSerial.java (從串口讀取數據的程序)。另外本類庫還提供了一個例程SerialExample.java 作為示范。在下面的內容中將逐一對這幾個部分進行詳細介紹。 1. SerialBeanSerialBean是本類庫與其他應用程序的接口。該類庫中定義了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 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是本類庫中所定義的串口緩沖區,它定義了往該緩沖區中寫入數據和從該緩沖區中讀取數據所需要的函數。 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 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是一個進程,它不斷的從指定的串口讀取數據并將其存放到緩沖區中。 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. SerialExampleSerialExample是本類庫所提供的一個例程。它所實現的功能是打開串口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 hereSerialBean SB = new SerialBean(1);String Msg;SB.Initialize();for (int i = 5; i
標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
精品久久久久久久| 日韩一级网站| 99热国内精品| 国产福利91精品一区二区| 神马午夜久久| 国产精品成人自拍| 国产黄大片在线观看| 国产亚洲欧洲| 伊人久久视频| 最近国产精品视频| 国产精品极品在线观看| 日韩中文字幕一区二区三区| 高清在线一区| 日韩成人一级| 日韩高清不卡在线| 91九色精品国产一区二区| 国产精品日本一区二区不卡视频| 欧美日本二区| 午夜亚洲福利| 亚洲专区欧美专区| 成人片免费看| 99视频精品全国免费| 九九久久国产| 美女久久精品| 欧美日韩 国产精品| 亚洲日韩视频| 在线日韩成人| 亚洲人成高清| 一区二区亚洲精品| 欧美特黄视频| 在线精品亚洲| 国产亚洲一区| 日本91福利区| 亚洲一级大片| 一区二区三区四区精品视频| 久久久久久久久丰满| 国产中文欧美日韩在线 | 激情综合婷婷| 欧美1区二区| 国产99久久| 综合一区在线| 美女网站一区| 人人精品人人爱| 天堂va在线高清一区| 美女精品在线观看| 国产精品美女在线观看直播 | 日韩中文字幕不卡| 亚洲午夜国产成人| 亚洲a级精品| 免费日韩精品中文字幕视频在线| 日韩精品中文字幕一区二区| 中文在线日韩| 电影亚洲精品噜噜在线观看| 中文亚洲欧美| 石原莉奈在线亚洲二区| 一区二区三区国产在线| 国产精品调教视频| 免费成人av在线播放| 精品在线99| 欧美日韩视频网站| 精品入口麻豆88视频| 国产精品亲子伦av一区二区三区 | 国产精品久久久久久久久久妞妞| 精品国模一区二区三区| 给我免费播放日韩视频| 精品一区二区三区中文字幕在线| 欧美精品观看| 国产乱码精品一区二区三区四区 | 成人国产精品一区二区网站| 欧美激情亚洲| 久久久久久久欧美精品| 麻豆精品一区二区综合av| 久久伊人国产| 国产精品chinese| 久久高清免费| 蜜臀av国产精品久久久久| 国产精品美女午夜爽爽| 国产偷自视频区视频一区二区| 国产毛片久久| 高清日韩欧美| 日精品一区二区三区| 超级白嫩亚洲国产第一| 日本vs亚洲vs韩国一区三区二区| 久久激情av| 日本久久精品| av亚洲在线观看| 色婷婷成人网| 日韩激情一区| 日韩中文字幕无砖| 精品久久久久久久| 亚洲欧洲一区| 牛牛精品成人免费视频| 九色精品91| 国产精品欧美三级在线观看| 99久久www免费| 日本午夜精品久久久久| 伊人精品一区| 国产精品亚洲欧美一级在线| 久久精品免费一区二区三区| 欧美一区不卡| 欧美日韩一二三四| 蜜臀av一区二区三区| 久久不射网站| 自拍日韩欧美| 日产精品一区二区| 欧美日韩少妇| 久久永久免费| 99香蕉国产精品偷在线观看 | 国产精品亚洲片在线播放| 卡一卡二国产精品| 99香蕉国产精品偷在线观看| 国产精品成人3p一区二区三区| 三级亚洲高清视频| 欧美日韩国产一区二区三区不卡| 国产极品模特精品一二| 国产亚洲在线| 首页国产精品| 美女视频黄免费的久久| 蜜臀av国产精品久久久久 | 福利片在线一区二区| 日韩国产欧美在线视频| 一本色道久久精品| 精品视频高潮| 美女性感视频久久| 国产激情一区| 国产精品美女久久久久久不卡| 亚洲一级大片| 亚洲欧美一级| 日韩欧美中文字幕在线视频| 蜜臀91精品一区二区三区| 欧美影院三区| 五月天综合网站| 精品在线99| 欧美色图一区| 欧美特黄一区| 99亚洲精品| 亚洲一区二区日韩| 日韩国产欧美三级| 国产精品777777在线播放 | 精品一区二区三区亚洲| 久久影院资源站| 91av亚洲| 激情综合亚洲| 一二三区精品| 奇米777国产一区国产二区| 国产亚洲字幕| 国产精品多人| 国产精品久久久久久久久免费高清| 久久av免费看| 日本美女一区| 蜜臀av性久久久久蜜臀aⅴ流畅| 亚洲午夜久久| 欧美va天堂| 亚洲婷婷丁香| 夜鲁夜鲁夜鲁视频在线播放| 麻豆成人在线| 欧美1级日本1级| 首页国产精品| 麻豆精品av| 欧美日韩一区自拍| 亚洲福利久久| 亚洲国产成人二区| 蜜桃久久av一区| 日韩精品一区二区三区免费观影| 欧美久久亚洲| 国产视频欧美| 91一区二区| 国产精品毛片aⅴ一区二区三区| 亚洲美洲欧洲综合国产一区| 成人国产精品| 久久精品九色| 国产欧美丝祙| 日韩福利视频网| 日韩av一区二区三区四区| 伊人精品久久| 日韩不卡免费视频| 午夜精品影视国产一区在线麻豆| 91精品美女| 亚洲精品护士| 国产欧美精品久久| 久久在线91| 日韩电影在线视频| 成人影视亚洲图片在线| 国产精品亚洲综合色区韩国| 爽好久久久欧美精品| 欧美亚洲国产激情| 欧美+日本+国产+在线a∨观看| 成人久久久久| 欧美成人综合| 欧美日韩精品免费观看视频完整| 久久国产日韩| 性欧美xxxx免费岛国不卡电影| 精品国产一区二| 久久九九精品| 四虎在线精品| 精品一区电影| 在线看片日韩| 成人午夜毛片| 一区二区国产在线观看| 久久精品国产福利|