mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
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
66
67
package sp_comm;
 
import java.nio.ByteBuffer;
 
import org.apache.logging.log4j.Logger;
 
import main.page_debug_inf;
 
public class SP_COMM {
    public final static int COMM_TYPE_Serial = 0;
    public final static int COMM_TYPE_Socket = 1;
    
    public CommSerialPort m_SP_Comm = null;
    public Comm_Socket m_SocketComm = null;
    
    public int m_CommType = COMM_TYPE_Serial;
    public int local_udp_port = 10002;
    private Logger m_Log = null;
    
    public SP_COMM(int type, int udp_port, Logger log) {
        m_CommType = type;
        if(m_CommType > COMM_TYPE_Socket) {
            m_CommType = COMM_TYPE_Socket;
        }
        
        local_udp_port = udp_port;
        m_Log = log;
    }
    
    public boolean openCommPort(String comm_name_or_ip, int bitrate_or_comm_port) {
        boolean res = false;
        if(COMM_TYPE_Serial == m_CommType) {
            m_SP_Comm = new CommSerialPort(bitrate_or_comm_port, m_Log);
            res = m_SP_Comm.OpenCommPort(comm_name_or_ip);
        } else {
            m_SocketComm = new Comm_Socket(comm_name_or_ip, bitrate_or_comm_port);
            res = true;
        }
        return res;
    }
    
    public boolean tr_Msg(ByteBuffer bbf_tx, ByteBuffer bbf_rx, int mult_comm_or_use_tcp, 
                            page_debug_inf debug_inf, boolean showdat) {
        boolean res = false;
        if(COMM_TYPE_Serial == m_CommType) {
            m_SP_Comm.SP_Comm(bbf_tx, bbf_rx, mult_comm_or_use_tcp, debug_inf, showdat);
            res = true;
        } else {
            if(1 == mult_comm_or_use_tcp) {
                res = m_SocketComm.SocketComm(bbf_tx, bbf_rx, debug_inf, showdat);
            } else {
                res = Comm_Socket.SocketUdpComm(local_udp_port, m_SocketComm.mDevIp, m_SocketComm.mPort, 
                                                    bbf_tx, bbf_rx, debug_inf, showdat);
            }
        }
        
        return res;
    }
    
    public void closeCommPort() {
        if(COMM_TYPE_Serial == m_CommType) {
            m_SP_Comm.serialPort.close();
        } else {
            m_SocketComm.socketClose();
        }
    }
}