中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java的com口通訊

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import gnu.io.CommPortIdentifier;  
import gnu.io.PortInUseException;  
import gnu.io.SerialPort;  
import gnu.io.SerialPortEvent;  
import gnu.io.SerialPortEventListener;  
import gnu.io.UnsupportedCommOperationException;  
  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Enumeration;  
import java.util.TooManyListenersException;  
  
public class SimpleRead implements Runnable, SerialPortEventListener {  
    static CommPortIdentifier portId;  
  
    static Enumeration portList;  
  
    InputStream inputStream;  
  
    SerialPort serialPort;  
  
    Thread readThread;  
  
    public static void main(String[] args) {  
  
        String com="4";  
        if(args!=null&&args.length>=1)  
        com=args[0];  
          
        portList = CommPortIdentifier.getPortIdentifiers();  
        // 檢索系統(tǒng)串口  
        while (portList.hasMoreElements()) {  
            portId = (CommPortIdentifier) portList.nextElement();  
          
            /*如果端口類型是串口,則打印出其端口信息*/   
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {  
                System.out.println("------------------------");  
                System.out.println("系統(tǒng)可用串口: "+portId.getName());  
                System.out.println("------------------------");  
                // 指定COM口  
                if (portId.getName().equals("COM"+com)) {  
                    System.out.println("找到COM"+com+"口,初始化...");  
                    SimpleRead reader = new SimpleRead();  
                }else{  
                    System.out.println("無法找到COM"+com+"口,請重新指定...");  
                }  
            }  
        }  
    }  
  
    public SimpleRead() {  
        try {  
        // 打開COM串口 2000 設(shè)置毫秒數(shù) 超時等待時間  
          
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);  
            System.out.println("COM口打開成功!");  
  
        } catch (PortInUseException e) {  
            System.out.println("端口被占用");  
        }  
        try {  
            inputStream = serialPort.getInputStream();  
            System.out.println("獲得輸入流...");  
        } catch (IOException e) {  
        }  
        //進行端口監(jiān)聽 , 當(dāng)事件發(fā)生自動調(diào)用 serialEvent方法  
        try {  
            serialPort.addEventListener(this);  
        } catch (TooManyListenersException e) {  
        }  
        serialPort.notifyOnDataAvailable(true);  
          
        //設(shè)置通訊位  
        try {  
            System.out.println("設(shè)置通訊位...");  
            serialPort.setSerialPortParams(115200,// 設(shè)置波特率  
                    SerialPort.DATABITS_8,// 數(shù)據(jù)位數(shù)  
                    SerialPort.STOPBITS_1,// 停止位  
                    SerialPort.PARITY_NONE);// 奇偶位  
        } catch (UnsupportedCommOperationException e) {  
        }  
  
        // 啟動線程,監(jiān)聽  
        readThread = new Thread(this);//線程負責(zé)每接收一次數(shù)據(jù)休眠20秒鐘  
        readThread.start();  
    }  
  
    public void run() {  
        try {  
            System.out.println("監(jiān)聽...");  
            Thread.sleep(20000);//休息20秒  
              
        } catch (Exception e) {  
        }  
    }  
    // 處理偵聽到的串口事件  
    public synchronized void serialEvent(SerialPortEvent event) {  
    //    System.out.println("接收數(shù)據(jù)...\r\n");  
          
        switch (event.getEventType()) {  
        case SerialPortEvent.BI://BI - 通訊中斷.  
        case SerialPortEvent.OE://OE - 溢位錯誤.  
        case SerialPortEvent.FE://FE - 幀錯誤.  
        case SerialPortEvent.PE://PE - 奇偶校驗錯.  
        case SerialPortEvent.CD://CD - 載波檢測.  
        case SerialPortEvent.CTS://CTS - 清除發(fā)送.  
        case SerialPortEvent.DSR://DSR - 數(shù)據(jù)設(shè)備準(zhǔn)備好.  
        case SerialPortEvent.RI://RI -  振鈴指示.  
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY://OUTPUT_BUFFER_EMPTY - 輸出緩沖區(qū)已清空  
            break;  
        case SerialPortEvent.DATA_AVAILABLE://DATA_AVAILABLE - 有數(shù)據(jù)到達  
              
              
            byte[] readBuffer = new byte[10000];  
  
            try {  
                //讀數(shù)據(jù)  
                while (inputStream.available() > 0) {  
                    int numBytes = inputStream.read(readBuffer);  
                }  
              
                String str=new String(readBuffer);  
                if(str.equals("exit")){  
                    inputStream.close();serialPort.close();  
                }  
                //輸出內(nèi)容  
                System.out.println("<------開始------->");  
                System.out.println(str+"==============");  
                System.out.println("<------結(jié)束------->");  
                System.out.println("                    ");  
            } catch (IOException e) {  
            }  
            break;  
        }  
    }  
}  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:java正則表達式工具類

下一篇:利用Gson實現(xiàn)Json串和Java Bean互轉(zhuǎn)