您的位置:首页 > 其它

中控系统一键关机--读取串口数据并判断

2018-03-18 20:46 375 查看
    前几天接到一个小任务,编写一个读取串口的数据并判断数据,最后根据传来数据执行相应的bat文件,在网上找了很多的资料,终于......完成啦!!!
    在执行此程序之前,需要在电脑上下Configure Virtual Serial Port Driver软件,在电脑上虚拟2个串口,因为这个程序要需要某个串口给串口2发数据,所以还需要SerialDebug来模拟com1向com2发送数据。最后需要网上下载rxtx工具包并build path在自己的项目中。
1、首先,需要编写一个配置文件config.properties,需要将变量存放在配置文件中,日后需要更改时,就不需要改代码啦,改配置文件就可以了。config.properties创建:点击项目右键new--File--将文件名的改为config.properties,这个配置文件需要放到src文件下,这样将项目打包成jar时也可以找得到。
comName为串口号,poweroff0和poweroff1为需要执行的bat文件comName = COM2
poweroff0 = cmd /c start E:/poweroff0.bat
poweroff1 = cmd /c start E:/poweroff1.bat2、读取Properties配置文件的属性,因为我需要读取3个参数,所以我写了三个get方法,可能会比较笨,因为毕竟我也是菜鸟级的,有待提高。package com.serialPort;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* 读取Properties配置文件的属性
*/
public class ReadProperties {

private static String comName = "";
private static String poweroff0 = "";
private static String poweroff1 = "";

/**
* 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件
*
* @return
*/
static InputStream in = ReadProperties.class.getClassLoader().getResourceAsStream("config.properties");
static Properties prop = new Properties();

public static String getComName() throws IOException {
prop.load(in);
comName = prop.getProperty("comName");
return comName;
}

public static String getPowerOff0() throws IOException {
prop.load(in);
poweroff0 = prop.getProperty("poweroff0");
return poweroff0;
}

public static String getPowerOff1() throws IOException {
prop.load(in);
poweroff1 = prop.getProperty("poweroff1");
return poweroff1;
}

public static void main(String args[]) throws IOException{
System.out.println(ReadProperties.getComName()); //此处输出属性判断是否已经取到数据
System.out.println(ReadProperties.getPowerOff0());
System.out.println(ReadProperties.getPowerOff1());
}
}3、从串口处读取数据并执行相应的bat文件package com.serialPort;

import java.io.IOException;
import java.io.InputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;

/**
* Com2PollingListener类使用“轮训”的方法监听串口COM2,
* 并通过COM2的输入流对象来获取该端口接收到的数据(在本文中数据来自串口COM1)。
*/
public class Com2PollingListener {
public static void main(String[] args){
//1.定义变量
CommPortIdentifier com2 = null;//未打开的端口
SerialPort serialCom2 = null;//打开的端口
InputStream inputStream = null;//端口输入流

try{
//2.获取并打开串口COM2
com2 = CommPortIdentifier.g
4000
etPortIdentifier(ReadProperties.getComName());
//System.out.println(ReadProperties.getComName());
//System.out.println(ReadProperties.getCmd());
//System.out.println(ReadProperties.getPowerOff1());
serialCom2 = (SerialPort) com2.open("Com2Listener", 1000); //com2打开的对象赋值给serialCom2

//3.获取串口的输入流对象
inputStream = serialCom2.getInputStream();

//4.从串口读入数据
//定义用于缓存读入数据的数组
byte[] cache = new byte[1024]; //放读入数据的数组,将byte数据的长度设为1024
//记录已经到达串口COM2且未被读取的数据的字节(Byte)数。
int availableBytes = 0;

//无限循环,每隔20毫秒对串口COM2进行一次扫描,检查是否有数据到达
while(true){
//获取串口COM2收到的可用字节数
availableBytes = inputStream.available();
//如果可用字节数大于零则开始循环并获取数据
while(availableBytes > 0){
System.out.println("有数据了");
//Com2PollingListener com2PollingListener= new Com2PollingListener();
//com2PollingListener.runbat();

//从串口的输入流对象中读入数据并将数据存放到缓存数组中
inputStream.read(cache);
//将获取到的数据进行转码并输出
for(int j = 0;j < cache.length && j < availableBytes; j++){
//因为COM1口发送的是使用byte数组表示的字符串,
//所以在此将接收到的每个字节的数据都强制装换为char对象即可,
//这是一个简单的编码转换,读者可以根据需要进行更加复杂的编码转换。
System.out.print((char)cache[j]);
}
System.out.println();

//用equals比较长度不相同也会返回false,否则会返回false
//将字符数组cache转为string类型,在此处一定要设置转为string类型后的长度,因为我需要与之比较的字符串poweroff1或者poweroff0长度都为9,故我设b的长度为9
                  String b= new String(cache, 0, 9);
switch (b) {                        case "poweroff0":System.out.println("比较成功0");Runtime.getRuntime().exec(ReadProperties.getPowerOff0());break;case "poweroff1":System.out.println("比较成功1");Runtime.getRuntime().exec(ReadProperties.getPowerOff1());break;default:break;} //更新循环条件 availableBytes = inputStream.available(); } //让线程睡眠20毫秒 Thread.sleep(20); } }catch(InterruptedException e){ e.printStackTrace(); }catch (NoSuchPortException e) { //找不到串口的情况下抛出该异常 e.printStackTrace(); } catch (PortInUseException e) { //如果因为端口被占用而导致打开失败,则抛出该异常 e.printStackTrace(); } catch (IOException e) { //如果获取输出流失败,则抛出该异常 e.printStackTrace(); } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐