Java實現RS485串口通信,發送和接收數據進行解析


  最近項目有一個空氣檢測儀,需要得到空氣檢測儀的實時數據,保存到數據庫當中。根據了解得到,硬件是通過rs485進行串口通訊的,需要發送16進制命令給儀器,然后通過輪詢來得到數據。

  需要先要下載RXTX的jar包,win64位下載地址:http://pan.baidu.com/s/1o6zLmTc);將解壓后的rxtxParallel.dll和rxtxSerial.dll兩個文件放在%JAVA_HOME%/jre/bin目錄下,這樣該包才能被正常的加載和調用。

  代碼如下:

package com.gpdi.sericlport;


import gnu.io.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.gpdi.utils.*;


public class ContinueRead extends Thread implements SerialPortEventListener { // SerialPortEventListener
    // 監聽器,我的理解是獨立開辟一個線程監聽串口數據
// 串口通信管理類
    static CommPortIdentifier portId;

    /* 有效連接上的端口的枚舉 */

    static Enumeration<?> portList;
    InputStream inputStream; // 從串口來的輸入流
    static OutputStream outputStream;// 向串口輸出的流
    static SerialPort serialPort; // 串口的引用
    // 堵塞隊列用來存放讀到的數據
    private BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>();

    @Override
    /**
     * SerialPort EventListene 的方法,持續監聽端口上是否有數據流
     */
    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 = null;
                int availableBytes = 0;
                try {
                    availableBytes = inputStream.available();
                    while (availableBytes > 0) {
                        readBuffer = ContinueRead.readFromPort(serialPort);
                        String needData = printHexString(readBuffer);
                        System.out.println(new Date() + "真實收到的數據為:-----" + needData);
                        availableBytes = inputStream.available();
                        msgQueue.add(needData);
                    }
                } catch (IOException e) {
                }
            default:
                break;
        }
    }

    /**
     * 從串口讀取數據
     *
     * @param serialPort 當前已建立連接的SerialPort對象
     * @return 讀取到的數據
     */
    public static byte[] readFromPort(SerialPort serialPort) {
        InputStream in = null;
        byte[] bytes = {};
        try {
            in = serialPort.getInputStream();
            // 緩沖區大小為一個字節
            byte[] readBuffer = new byte[1];
            int bytesNum = in.read(readBuffer);
            while (bytesNum > 0) {
                bytes = MyUtils.concat(bytes, readBuffer);
                bytesNum = in.read(readBuffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                    in = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }


    /**
     * 通過程序打開COM4串口,設置監聽器以及相關的參數
     *
     * @return 返回1 表示端口打開成功,返回 0表示端口打開失敗
     */
    public int startComPort() {
        // 通過串口通信管理類獲得當前連接上的串口列表
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            // 獲取相應串口對象
            portId = (CommPortIdentifier) portList.nextElement();

            System.out.println("設備類型:--->" + portId.getPortType());
            System.out.println("設備名稱:---->" + portId.getName());
            // 判斷端口類型是否為串口
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                // 判斷如果COM4串口存在,就打開該串口
                if (portId.getName().equals(portId.getName())) {
                    try {
                        // 打開串口名字為COM_4(名字任意),延遲為1000毫秒
                        serialPort = (SerialPort) portId.open(portId.getName(), 1000);

                    } catch (PortInUseException e) {
                        System.out.println("打開端口失敗!");
                        e.printStackTrace();
                        return 0;
                    }
                    // 設置當前串口的輸入輸出流
                    try {
                        inputStream = serialPort.getInputStream();
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    // 給當前串口添加一個監聽器
                    try {
                        serialPort.addEventListener(this);
                    } catch (TooManyListenersException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    // 設置監聽器生效,即:當有數據時通知
                    serialPort.notifyOnDataAvailable(true);

                    // 設置串口的一些讀寫參數
                    try {
                        // 比特率、數據位、停止位、奇偶校驗位
                        serialPort.setSerialPortParams(9600,
                                SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {
                        e.printStackTrace();
                        return 0;
                    }
                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            System.out.println("--------------任務處理線程運行了--------------");
            while (true) {
                // 如果堵塞隊列中存在數據就將其輸出
                if (msgQueue.size() > 0) {
                    String vo = msgQueue.peek();
                    String vos[] = vo.split("  ", -1);
                    getData(vos);
                    sendOrder();
                    msgQueue.take();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @Description: 發送獲取數據指令
     * @Param:
     * @return:
     * @Author: LiangZF
     * @Date: 2019/1/3
     */
    public void sendOrder() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int i = 1;
        if (i == 1) {
            // 啟動線程來處理收到的數據
            try {
                byte[] b = new byte[]{0x01, 0x03, 0x00, 0x00, 0x00, 0x0E, (byte) 0xC4, 0x0E};
                System.out.println("發送的數據:" + b);
                System.out.println("發出字節數:" + b.length);
                outputStream.write(b);
                outputStream.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                serialPort.close();
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * @Description:通過數組解析檢測數據
     * @Param: [vo]
     * @return: void
     * @Author: LiangZF
     * @Date: 2019/1/4
     */
    public void getData(String[] vos) {
        // 數組不為空
        if (vos != null || vos.length != 0) {
            // 風向數據
            long wind_direction = getNum(vos[3], vos[4]);
            System.out.println(wind_direction);
            // 風速數據
            long wind_speech = getNum(vos[5], vos[6]);
            System.out.println(wind_speech);
            // pm2.5
            long polutionPm2 = getNum(vos[7], vos[8]);
            System.out.println(polutionPm2);
            // pm10
            long polutionPm10 = getNum(vos[9], vos[10]);
            System.out.println(polutionPm10);
            // VOC
            long voc = getNum(vos[11], vos[12]);
            System.out.println(voc);
            // 溫度
            long polutionPm = getNum(vos[13], vos[14]) / 10;
            System.out.println(polutionPm);
            // 濕度
            long temperature = getNum(vos[15], vos[16]) / 10;
            System.out.println(temperature);
            // 大氣壓力
            long atmosphericPressure = getNum(vos[17], vos[18]);
            System.out.println(atmosphericPressure);
            // 臭氧
            long ozone = getNum(vos[19], vos[20]) / 1000;
            System.out.println(ozone);
            // CO
            long co = getNum(vos[21], vos[22]) / 100;
            System.out.println(co);
            Map<String, Long> map = new HashMap<>();
            map.put("O3", ozone);
            map.put("PM2.5", polutionPm2);
            map.put("PM10", polutionPm10);
            Map<String, Object> uu = AqiUtil.getAqiByPollutants(map);
            String pollutants = (String) uu.get("key");
            Integer aqi = (Integer) uu.get("value");
            insertDb(wind_direction, wind_speech, polutionPm2, polutionPm10, voc, polutionPm, temperature, atmosphericPressure, ozone, co, pollutants, aqi);
        }
    }

    // 16轉10計算
    public long getNum(String num1, String num2) {
        long value = Long.parseLong(num1, 16) * 256 + Long.parseLong(num2, 16);
        return value;
    }
    /** 
    * @Description: 保存到數據庫表中 
    * @Param: [wind_direction, wind_speech, polutionPm2, polutionPm10, voc, polutionPm, temperature, atmosphericPressure, ozone, co, pollution, aqi] 
    * @return: void 
    * @Author: LiangZF 
    * @Date: 2019/1/6 
    */ 
    public void insertDb(long wind_direction, long wind_speech, long polutionPm2, long polutionPm10, long voc, long polutionPm, long temperature, long atmosphericPressure, long ozone, long co, String pollution, Integer aqi) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        try {
            conn = DBUtil.getConn();
            String sql = "insert into air_status (wind_direction,wind_speed,particulate_matter,particulate_matter_one,voc,weather,humidity,air_pre,ozone,carbon_monoxide,del_flag,create_time,primary_pollutants,aqi)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setLong(1, wind_direction);
            ps.setLong(2, wind_speech);
            ps.setLong(3, polutionPm2);
            ps.setLong(4, polutionPm10);
            ps.setLong(5, voc);
            ps.setLong(6, polutionPm);
            ps.setLong(7, temperature);
            ps.setLong(8, atmosphericPressure);
            ps.setLong(9, ozone);
            ps.setLong(10, co);
            ps.setInt(11, 0);
            Timestamp time = new Timestamp(System.currentTimeMillis());//獲取系統當前時間
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timeStr = df.format(time);
            time = Timestamp.valueOf(timeStr);
            ps.setTimestamp(12, time);
            ps.setString(13, pollution);
            ps.setInt(14, aqi);
            int count = ps.executeUpdate();
            if (count > 0) {
                System.out.println("插入成功!");
            } else {
                System.out.println("插入失敗!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        ContinueRead cRead = new ContinueRead();
        System.out.println("asdasd");
        int i = cRead.startComPort();
        if (i == 1) {
            // 啟動線程來處理收到的數據
            cRead.start();
            try {
                //根據提供的文檔給出的發送命令,發送16進制數據給儀器
                byte[] b = new byte[]{0x01, 0x03, 0x00, 0x00, 0x00, 0x0E, (byte) 0xC4, 0x0E};
                System.out.println("發送的數據:" + b);
                System.out.println("發出字節數:" + b.length);
                outputStream.write(b);
                outputStream.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            return;
        }
    }

    // 字節數組轉字符串
    private String printHexString(byte[] b) {

        StringBuffer sbf = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sbf.append(hex.toUpperCase() + "  ");
        }
        return sbf.toString().trim();
    }
}

 

MyUtils工具類

 因為得到的byte數組會分成幾段,需要進行合並數組操作。

/**
     * 合並數組
     * 
     * @param firstArray  第一個數組
     * @param secondArray 第二個數組
     * @return 合並后的數組
     */
    public static byte[] concat(byte[] firstArray, byte[] secondArray) {
        if (firstArray == null || secondArray == null) {
            if (firstArray != null)
                return firstArray;
            if (secondArray != null)
                return secondArray;
            return null;
        }
        byte[] bytes = new byte[firstArray.length + secondArray.length];
        System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
        System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
        return bytes;
    }

 

參考文章:https://blog.csdn.net/update_java/article/details/46898937

 下載地址 https://github.com/732847260/RS485 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM