资源描述:
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; @Override public void run() { // TODO Auto-generated method stub } public SerialCommcation(){ portName = "COM"+portID; SerialParameters serialParameters = new SerialParameters(); System.out.println("系统应用的端口为:" + portName); //设定通讯的串行口 serialParameters.setCommPortId(portName); //设定成无奇偶校验 serialParameters.setParity(0); //设定数据位是8位 serialParameters.setDataBits(8); //设定一个停止位 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(); } //读取串口数据 private synchronized void update(){ // TODO Auto-generated method stub try { try{ Enumeration> 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 exception System.out.println("PortInUseException"+e); } //用字节输入流in调用read()方法读出串口中的数据 try{ String[] recive=new String[55]; 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(); recive[i]=Integer.toHexString(receivedData);// System.out.println(recive[i]); } break; } } //空气温度 result=recive[21]+recive[22]+recive[24]+recive[25]; 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 (airtemperature<0) { airtemperature=0; }// System.out.println(airtemperature); //空气湿度 result=recive[30]+recive[31]+recive[33]+recive[34]; 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 (airhumidity<0) { airhumidity=0; }// if (airhumidity>100) {// airhumidity=100;// }// System.out.println(airhumidity); //土壤温度 /* result=recive[39]+recive[40]+recive[42]+recive[43]; 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(soiltemperature<0){ soiltemperature=0; } System.out.println(soiltemperature);*/ //土壤湿度 /*result=recive[39]+recive[40]+recive[42]+recive[43]; 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(soilmoisture<0){ soilmoisture=0; } System.out.println(soilmoisture);*/ soiltemperature=0; soilmoisture=0; //风速 result=recive[39]+recive[40]+recive[42]+recive[43]; 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<0) { windvelocity=0; }// System.out.println(windvelocity); //雨量// if (recive[16].equals("30")) {// count=count+1;// minFlag=minFlag+5000;// }// // if (minFlag>=60000) { rainfall=0.0;// count=0.0;// minFlag=0;// }// }catch (IOException ioe) { // TODO: handle exception System.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 exception System.out.println("Exception"+e); } } public String readPort() { System.out.println(result); return result; } public void closeSer(){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block// e.printStackTrace(); System.out.println("IOException"+e); } serialPort.close(); }}
展开阅读全文