9度通讯程序适用于9度多组设备
Administrator
2023-12-27 c736aaea5522e295b42676f094d313e7f6a4afb4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.dev.fbs9100;
 
import java.util.Enumeration;
 
import com.battdata_rt.BattData_RT_Array;
import com.config.AppConfig;
import com.sql.MysqlConnPool;
 
import gnu.io.CommPortIdentifier;
 
public class FBS9100S_SerialPortThread extends Thread{
    private FBS9100S_SerialPortClient SerialPortClient;                //´æ·Åµ±Ç°´®¿ÚͨÐŶÔÏó
    private MysqlConnPool m_ConnPool = null;
    private BattData_RT_Array m_Data;
    private AppConfig m_app_cfg;
    
    public FBS9100S_SerialPortThread(MysqlConnPool pool, BattData_RT_Array data, AppConfig cfg){
        m_ConnPool = pool;
        m_Data = data;
        m_app_cfg = cfg;
    }
    
    @Override
    public void run() {
        System.out.println(this.getName() + " - FBS9100S_SerialPortThread Started");
        System.out.println(m_app_cfg.getSerialPort()+"ÉèÖõĴ®¿ÚΪ");
        while (true) {
            try {
                // »ñµÃµ±Ç°ËùÓпÉÓô®¿Ú
                Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();
                for(;portList.hasMoreElements();){
                    CommPortIdentifier port = portList.nextElement();
                    if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                        if(port.getName().equalsIgnoreCase(m_app_cfg.getSerialPort())){
                            if(SerialPortClient == null){
                                //Ê×´ÎÆô¶¯
                                SerialPortClient  = new FBS9100S_SerialPortClient(port,m_ConnPool,m_Data);    
                                SerialPortClient.start();
                            }else if(!SerialPortClient.S_thread_run_flag){
                                try {
                                    if(SerialPortClient.serialPort != null){
                                        SerialPortClient.serialPort.close();
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                //·ÇÊ×´ÎÆô¶¯,µ«ÊÇÉÏ´ÎͨÐÅʧ°Ü
                                sleep(1000);
                                SerialPortClient  = new FBS9100S_SerialPortClient(port,m_ConnPool,m_Data);    
                                SerialPortClient.start();
                            }                            
                        }                        
                    }
                }                
                sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }                
    }
    
    public static void main(String[] args) {
        
    }
}