电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:57日期:2024-07-02 09:18:52
內(nèi)容: 一個(gè)嵌入式系統(tǒng)通常需要通過(guò)串口與其主控系統(tǒng)進(jìn)行全雙工通訊,譬如一個(gè)流水線控制系統(tǒng)需要不斷的接受從主控系統(tǒng)發(fā)送來(lái)的查詢和控制信息,并將執(zhí)行結(jié)果或查詢結(jié)果發(fā)送回主控系統(tǒng)。本文介紹了一個(gè)簡(jiǎn)單的通過(guò)串口實(shí)現(xiàn)全雙工通訊的Java類庫(kù),該類庫(kù)大大的簡(jiǎn)化了對(duì)串口進(jìn)行操作的過(guò)程。 本類庫(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. SerialBeanSerialBean是本類庫(kù)與其他應(yīng)用程序的接口。該類庫(kù)中定義了SerialBean的構(gòu)造方法以及初始化串口,從串口讀取數(shù)據(jù),往串口寫(xiě)入數(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。如果串口被成功初始化,則打開(kāi)一個(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 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是本類庫(kù)中所定義的串口緩沖區(qū),它定義了往該緩沖區(qū)中寫(xiě)入數(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ū)中寫(xiě)入一個(gè)字符,參數(shù)c 是需要寫(xiě)入的字符。在往緩沖區(qū)寫(xiě)入數(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. ReadSerialReadSerial是一個(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. SerialExampleSerialExample是本類庫(kù)所提供的一個(gè)例程。它所實(shí)現(xiàn)的功能是打開(kāi)串口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 hereSerialBean SB = new SerialBean(1);String Msg;SB.Initialize();for (int i = 5; i
標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 辽宁资质代办_辽宁建筑资质办理_辽宁建筑资质延期升级_辽宁中杭资质代办 | 横河变送器-横河压力变送器-EJA变送器-EJA压力变送器-「泉蕴仪表」 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | 新车测评网_网罗汽车评测资讯_汽车评测门户报道 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 工业铝型材-铝合金电机壳-铝排-气动执行器-山东永恒能源集团有限公司 | 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 开云(中国)Kaiyun·官方网站 - 登录入口| SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | 昊宇水工|河北昊宇水工机械工程有限公司| RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 全国冰箱|空调|洗衣机|热水器|燃气灶维修服务平台-百修家电 | 365文案网_全网创意文案句子素材站| 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | 999范文网_优质范文下载写作帮手 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 一级建造师培训_一建培训机构_中建云筑建造师培训网校 | 【北京写字楼出租_写字楼租赁_办公室出租网/出售】-远行地产官网 | 定制液氮罐_小型气相液氮罐_自增压液氮罐_班德液氮罐厂家 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 垃圾处理设备_餐厨垃圾处理设备_厨余垃圾处理设备_果蔬垃圾处理设备-深圳市三盛环保科技有限公司 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 减速机电机一体机_带电机减速器一套_德国BOSERL电动机与减速箱生产厂家 |