基于java的zigbee串口通訊的多線程.docx_第1頁
基于java的zigbee串口通訊的多線程.docx_第2頁
基于java的zigbee串口通訊的多線程.docx_第3頁
基于java的zigbee串口通訊的多線程.docx_第4頁
基于java的zigbee串口通訊的多線程.docx_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

import gnu.io.CommPortIdentifier;import gnu.io.PortInUseException;import gnu.io.SerialPort;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.concurrent.CountDownLatch;import com.serotonin.io.serial.SerialParameters;public class SerialCommcation implements Runnable/對象的聲明private String result=;static SerialPort serialPort;static OutputStream out;static InputStream in;String portName = null;private static int portID=2;private float airtemperature;private float airhumidity;private float soiltemperature;private float soilmoisture;private float windvelocity;private double rainfall;private Thread myThread;Overridepublic void run() / TODO Auto-generated method stubpublic SerialCommcation()portName = COM+portID; SerialParameters serialParameters = new SerialParameters(); System.out.println(系統(tǒng)應(yīng)用的端口為: + portName); /設(shè)定通訊的串行口 serialParameters.setCommPortId(portName);/設(shè)定成無奇偶校驗serialParameters.setParity(0);/設(shè)定數(shù)據(jù)位是8位serialParameters.setDataBits(8);/設(shè)定一個停止位serialParameters.setStopBits(1);serialParameters.setPortOwnerName(Numb nuts);/串行口上的波特率serialParameters.setBaudRate(9600);myThread = new Thread()public void run() while (true) update(); closeSer(); try sleep(1);catch (InterruptedException e) / TODO Auto-generated catch block/e.printStackTrace();System.out.println(InterruptedException+e);myThread.start();/讀取串口數(shù)據(jù)private synchronized void update()/ TODO Auto-generated method stubtry tryEnumeration portList = CommPortIdentifier.getPortIdentifiers();CommPortIdentifier portId = null;while(portList.hasMoreElements()portId = (CommPortIdentifier) portList.nextElement();if (portId.getName().equalsIgnoreCase(COM2)serialPort = (SerialPort) portId.open(,2000); /open方法打開通訊端口,得到一個CommPort對象,它使程序獨占端口/System.out.print(serialPort); catch (PortInUseException e) / TODO: handle exceptionSystem.out.println(PortInUseException+e);/用字節(jié)輸入流in調(diào)用read()方法讀出串口中的數(shù)據(jù)tryString recive=new String55;Float temp=null;while(true) in=new BufferedInputStream(serialPort.getInputStream();if (in.available()0) for (int i = 0; i recive.length; i+)int receivedData=in.read();recivei=Integer.toHexString(receivedData);/System.out.println(recivei);break;/空氣溫度result=recive21+recive22+recive24+recive25;result=result.substring(1, 2)+result.substring(3, 4)+.+result.substring(5, 6)+result.substring(7, 8);/System.out.println(傳感器值為:+result);temp=new Float(result);airtemperature=(float)(Math.round(temp*11.875-87.50)*10)/10;if (airtemperature0) airtemperature=0;/System.out.println(airtemperature);/空氣濕度result=recive30+recive31+recive33+recive34;result=result.substring(1, 2)+result.substring(3, 4)+.+ result.substring(5, 6)+result.substring(7, 8);/System.out.println(傳感器值為:+result);temp=new Float(result);airhumidity=(float)(Math.round(temp*6.25-25)*10)/10;if (airhumidity100) /airhumidity=100;/System.out.println(airhumidity);/土壤溫度/*result=recive39+recive40+recive42+recive43;result=result.substring(1, 2)+result.substring(3, 4)+.+result.substring(5, 6)+result.substring(7, 8);System.out.println(傳感器值為:+result);temp=new Float(result);soiltemperature=(float)(Math.round(temp*11.875-90.50)*10)/10;if(soiltemperature0)soiltemperature=0;System.out.println(soiltemperature);*/土壤濕度/*result=recive39+recive40+recive42+recive43;result=result.substring(1, 2)+result.substring(3, 4)+.+result.substring(5, 6)+result.substring(7, 8);System.out.println(傳感器值為:+result);temp=new Float(result);soilmoisture=(float)(Math.round(temp*6.25-25)*10)/10;if(soilmoisture0)soilmoisture=0;System.out.println(soilmoisture);*/soiltemperature=0;soilmoisture=0;/風(fēng)速result=recive39+recive40+recive42+recive43;result=result.substring(1, 2)+result.substring(3, 4)+.+result.substring(5, 6)+result.substring(7, 8);/System.out.println(傳感器值為:+result);temp=new Float(result);windvelocity=(float)(Math.round(temp*1.875-7.60)*10)/10;if (windvelocity=60000) rainfall=0.0;/count=0.0;/minFlag=0;/catch (IOException ioe) / TODO: handle exceptionSystem.out.println(Exception+ioe);result=/+String.valueOf(airtemperature)+/+String.valueOf(airhumidity)+ /+String.valueOf(soiltemperature)+/+String.valueOf(soilmoisture)+ /+String.valueOf(windvelocity)+/+String.valueOf(rainfall);System.out.println(result);catch (Exception e) / TODO: handle exceptionSystem.out.println(Exception+e);public

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論