成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

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

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

瀏覽:123日期: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
相關文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
亚洲成人免费看| 国产精品1区2区3区在线观看| 免费的成人av| 一本色道久久综合亚洲精品不卡| 久久久一区二区三区| 国产精品一二三| 久久一区激情| 亚洲一区二区视频在线观看| 一区二区亚洲| 欧美国产精品劲爆| 99久久久久久| 欧美α欧美αv大片| 韩国理伦片一区二区三区在线播放| 一本色道综合亚洲| 午夜电影一区二区| 亚洲激情不卡| 中文字幕日韩精品一区| 欧美精选一区| 国产精品欧美久久久久无广告| 欧美日韩综合网| 国产精品人人做人人爽人人添 | 欧美99久久| 日本一区二区综合亚洲| 欧美精品亚洲| 国产精品不卡一区| 亚洲午夜极品| 亚洲素人一区二区| 999亚洲国产精| 亚洲小说春色综合另类电影| 亚洲一区二区三区精品在线观看| 一区二区三区成人在线视频| 99re热精品| 香蕉久久夜色精品国产使用方法 | 欧美精品一二三| 国产精品主播直播| 欧美成人video| 色综合天天综合狠狠| 久久久久久夜精品精品免费| 欧美91福利在线观看| 国产欧美一区二区三区在线老狼| 色综合一区二区| 国产精品久久国产精麻豆99网站| 亚洲国产午夜| 日韩国产欧美视频| 这里只有精品电影| 97超碰欧美中文字幕| 国产精品免费网站在线观看| 亚洲国产激情| 亚洲大片一区二区三区| 欧洲人成人精品| 国产精品一区在线观看乱码| xfplay精品久久| 欧美福利在线| 综合电影一区二区三区| 亚洲永久视频| 激情另类小说区图片区视频区| 欧美不卡视频一区| 亚洲黄色毛片| 日本欧美在线观看| 欧美一区二区三区色| 99视频精品在线| 亚洲女人的天堂| 欧美伊人久久大香线蕉综合69| 国产91精品免费| 18欧美乱大交hd1984| 色天天综合色天天久久| 成人高清免费在线播放| 国产精品成人免费| 在线视频你懂得一区| 成人黄动漫网站免费app| 专区另类欧美日韩| 欧美亚洲国产怡红院影院| av一本久道久久综合久久鬼色| 亚洲三级在线观看| 91精品1区2区| 94-欧美-setu| 婷婷国产v国产偷v亚洲高清| 欧美电视剧在线观看完整版| 一区二区欧美日韩| 国精品**一区二区三区在线蜜桃| 久久久噜噜噜久噜久久综合| 亚洲一区二区三区免费在线观看| 国产美女久久久久| 中文字幕在线观看一区| 久久亚洲二区| 一区二区三区四区高清精品免费观看| 欧美日韩激情一区二区三区| 午夜精品剧场| 毛片av一区二区三区| 久久精品视频在线免费观看| 最新热久久免费视频| 国产夫妻精品视频| 精品视频在线免费观看| 欧美啪啪一区| 秋霞电影网一区二区| 久久综合九色综合97婷婷女人| 国产精品视区| 国产成人免费在线观看不卡| 亚洲欧洲日韩av| 在线观看91精品国产麻豆| 亚洲高清成人| 成人午夜精品在线| 天天av天天翘天天综合网色鬼国产| 精品美女在线观看| 麻豆成人在线| 午夜精品剧场| 国产在线精品一区二区夜色| 亚洲欧美另类小说| 日韩免费福利电影在线观看| 国产精品毛片| 成人黄色网址在线观看| 日韩精品免费专区| 国产亚洲欧美日韩在线一区| 在线精品视频一区二区| 亚洲香蕉网站| 国产一区激情在线| 一区二区三区精品视频| 精品久久久久久无| 一本大道久久a久久综合| 欧美精品二区三区四区免费看视频| 国内精品视频一区二区三区八戒| 亚洲人精品午夜| 欧美mv和日韩mv国产网站| 日本乱码高清不卡字幕| 亚洲经典三级| 91在线免费看| 国产资源在线一区| 一区二区三区在线看| 亚洲国产高清在线| 欧美一区二区三区在线电影| 久久xxxx精品视频| 亚洲视屏一区| 99国内精品久久| 黄色成人免费在线| 午夜精品久久久久久久99水蜜桃| 中文幕一区二区三区久久蜜桃| 日韩一区二区在线播放| 久久亚洲国产精品日日av夜夜| 国内精品久久久久久久影视蜜臀 | 强制捆绑调教一区二区| 亚洲品质自拍视频| 久久亚洲精品国产精品紫薇 | 久久久久看片| 91在线观看美女| 蜜臀久久99精品久久久久宅男| 天涯成人国产亚洲精品一区av| 欧美日韩在线观看一区二区三区| 欧美日韩国产综合久久| 欧美日韩日本国产亚洲在线| 欧美日韩亚洲不卡| 国产精品免费av| 天天综合日日夜夜精品| 国产精品初高中精品久久| 中文字幕亚洲一区二区av在线| 国产精品久久久久久模特 | 欧美日韩一区成人| 亚洲美女少妇无套啪啪呻吟| 欧美视频观看一区| 91色porny蝌蚪| a亚洲天堂av| 国产白丝网站精品污在线入口| 日本美女一区二区| 亚洲综合一区二区精品导航| 1024国产精品| 国产精品久久久99| 国产精品你懂的在线| 亚洲国产精品精华液ab| 久久精品欧美日韩| 久久久夜色精品亚洲| 精品久久国产字幕高潮| 欧美成人精品福利| 欧美电视剧在线观看完整版| 日韩精品最新网址| 精品国产免费人成电影在线观看四季| 日韩午夜小视频| 日韩欧美专区在线| 日韩欧美一级精品久久| 日韩欧美一级二级| 精品成人在线观看| 久久久精品免费网站| 国产欧美日韩视频在线观看| 国产片一区二区| 国产精品久久毛片a| 亚洲人一二三区| 亚洲一区在线播放| 天天免费综合色| 秋霞国产午夜精品免费视频| 久久se精品一区精品二区| 激情六月婷婷久久| 国产成人精品三级| av成人老司机| 99国产精品久久久| 国产自产精品| 国产精品腿扒开做爽爽爽挤奶网站| 亚洲欧美日韩精品综合在线观看| 裸体丰满少妇做受久久99精品| 色婷婷激情一区二区三区| 欧美精品少妇一区二区三区 | 一区二区三区四区不卡视频| 视频一区在线视频|