资源简介
从串口读取传感器采集到的温湿度数据,然后存入数据库,代码可以运行我采集成功了
代码片段和文件信息
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.util.ArrayList;
import java.sql.*;
public class SimpleRead implements Runnable SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
SensorNode sn;
int flag;
int id;
String i;
static Statement stmt;
static double tmaxhmax;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = “COM1“;
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println(“Found port: “+defaultPort);
portFound = true;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println(“port “ + defaultPort + “ not found.“);
}
}
public SimpleRead() {
flag=0;
sn=new SensorNode();
try {
serialPort = (SerialPort) portId.open(“SimpleReadApp“ 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyondataavailable(true);
try {
serialPort.setSerialPortParams(9600 SerialPort.DATABITS_8
SerialPort.STOPBITS_1
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
String ts=new String();
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
ts=ts+toHex(readBuffernumBytes);
}
if(flag==0){
if(ts.startsWith(“37“)){
flag=1;
sn.clearAll();
sn.setType(swap(ts.sub
- 上一篇:基于P2P的局域网即时通信系统 java实现源码
- 下一篇:文件输出与读入
相关资源
- android传感器高级编程源码+apk
- 利用android手机自带的加速度传感器和
- 基于Android的温湿度采集系统
- JAVA串口采集传感器数据
- Android 弹力球碰撞(传感器-12_110).
- Android 光线控制页面背景(传感器-1
- Android 仿微信摇一摇(传感器-12_104)
- Android 话筒吹雾小游戏(传感器-12_1
- 光线感应器
- Android 使用陀螺仪控制全景视图(传感
- Android 计步器(传感器-12_103).zip
- 无线传感器网路java实现+界面展示
- java 串口通讯 rs232
- Android的重力传感器3轴加速度传感器简
- 安卓传感器使用源码(指南针/重力感
- android 简单计步app(加速度传感器)
- Android摄像头传感器、定位等功能代码
- android传感器
- android传感器(加速度、方位、光线)
- android加速度传感器源码
评论
共有 条评论