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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package sp_comm;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.Enumeration;
 
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
import javax.swing.JComboBox;
 
import org.apache.logging.log4j.Logger;
 
import main.page_debug_inf;
 
import com.Com;
import com.ComFn;
 
public class CommSerialPort {
    public static int mutycomm_Type_Samd09    =    0;
    public static int mutycomm_Type_Msp430    =    1;
    public static int mutycomm_Type_SWM    =    2;
    // ¼ì²âϵͳÖпÉÓõÄͨѶ¶Ë¿ÚÀà
    private CommPortIdentifier portId;
    private Enumeration<CommPortIdentifier> portList;
    // RS-232µÄ´®ÐпÚ
    public SerialPort serialPort;
    public InputStream inputStream;
    public OutputStream outputStream;
    
    private int comm_bautrate = 9600;
    private Logger m_Log;
    
    public CommSerialPort(int bitrate, Logger log) {
        comm_bautrate = bitrate;
        m_Log = log;
    }
    
    public static void searchCommPort(JComboBox<String> cb_comm) {
        cb_comm.removeAllItems();
        @SuppressWarnings("unchecked")
        Enumeration<CommPortIdentifier> pl = CommPortIdentifier.getPortIdentifiers();
        while (pl.hasMoreElements()) {
            CommPortIdentifier port_id = pl.nextElement();
            if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                cb_comm.addItem(port_id.getName());
            }
        }
    }
    
    public void setCommBautrate(int bt_rate) {
        try {
            comm_bautrate = bt_rate;
            serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            m_Log.error(e.toString(), e);
        }
    }
    
    //³õʼ»¯´®¿Ú
    @SuppressWarnings("unchecked")
    public boolean OpenCommPort(String comm_name) {
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals(comm_name)) {
                    try {
                        serialPort = (SerialPort) portId.open("SerialPort-Test", 2000);
                        //serialPort.addEventListener(new SPComm());
                        serialPort.notifyOnDataAvailable(true);
                        /* ÉèÖô®¿ÚͨѶ²ÎÊý */
                        serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                        outputStream = serialPort.getOutputStream();
                        inputStream = serialPort.getInputStream();
                    } catch (PortInUseException | UnsupportedCommOperationException | IOException e) {
                        //e.printStackTrace();
                        m_Log.error(e.toString(), e);
                    } 
                }
            }
        }
        
        if(null == serialPort) {
            m_Log.info(comm_name + "²»´æÔÚ»òÕýÔÚʹÓÃÖÐ!");
            return false;
        } else {
            return true;
        }
    }
    
    /*********************************************************************************************/
    //Ïò´®¿Ú·¢ËÍÐÅÏ¢·½·¨
    private void sendMsg(ByteBuffer bbf_tx, int multy_comm_type,
                        page_debug_inf dt_debug_inf, boolean printdat) throws IOException {
        byte[] bt_addr = new byte[1];
        if(mutycomm_Type_Msp430 == multy_comm_type) {
            bt_addr[0] = bbf_tx.get();
        }
        
        byte[] bt_dat = new byte[bbf_tx.remaining()];
        for(int n=0; n<bt_dat.length; n++) {
            bt_dat[n] = bbf_tx.get();
        }
        
        dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " TX: ");
        if(mutycomm_Type_SWM == multy_comm_type) {
            try {
                serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_MARK);
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(mutycomm_Type_Msp430 == multy_comm_type) {
            try {
                serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_MARK);
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            if(true == printdat) {
                dt_debug_inf.addDebugInf(ComFn.bytesToHexString(bt_addr, bt_addr.length));
            }
            outputStream.write(bt_addr);
            outputStream.flush();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            try {
                serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_SPACE);
            } catch (UnsupportedCommOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(true == printdat) {
            dt_debug_inf.addDebugInf(ComFn.bytesToHexString(bt_dat, bt_dat.length) + "\n");
        } else {
            dt_debug_inf.addDebugInf("******\n");
        }
        outputStream.write(bt_dat);
        outputStream.flush();
    }
    /*********************************************************************************************/
    private void readMsg(ByteBuffer bbf_rx, page_debug_inf dt_debug_inf, 
                            boolean printdat) throws IOException, InterruptedException {
        bbf_rx.clear();
        int time_out = 0;
        byte[] rx_buf_t = new byte[2048];
        Date rx_time = null;
        while(true) {
            if(inputStream.available() > 0) {
                time_out = 0;
                int rx_cnt_t = inputStream.read(rx_buf_t);
                if((bbf_rx.position()+rx_cnt_t) < (bbf_rx.capacity()-1)) {
                    bbf_rx.put(rx_buf_t, 0, rx_cnt_t);
                    if(null == rx_time) {
                        rx_time = new Date();
                    }
                } else {
                    break;
                }
            } else {
                Thread.sleep(1);
                time_out++;
                if(0 == bbf_rx.position()) {
                    if(time_out > 500) {
                        break;
                    }
                } else {
                    if(time_out >= 100) {
                        break;
                    }
                }
            }
        }
        bbf_rx.flip();
        
        byte[] bt_t_rx = new byte[bbf_rx.limit()];
        bbf_rx.get(bt_t_rx);
        if(null == rx_time) {
            rx_time = new Date();
        }
        if(true == printdat) {
            dt_debug_inf.addDebugInf(Com.get_DTF(rx_time, Com.DTF_YMDhms_S) 
                    + " RX: " + ComFn.bytesToHexString(bt_t_rx, bt_t_rx.length) + "\n");
        } else {
            dt_debug_inf.addDebugInf(Com.get_DTF(rx_time, Com.DTF_YMDhms_S) + " RX: ******\n");
        }
        bbf_rx.position(0);
    }
    /*********************************************************************************************/
    public void SP_Comm(ByteBuffer bbf_tx, ByteBuffer bbf_rx, int multy_comm,
                            page_debug_inf dt_debug_inf, boolean printdat) {
        try {
            sendMsg(bbf_tx, multy_comm, dt_debug_inf, printdat);
            readMsg(bbf_rx, dt_debug_inf, printdat);
        } catch (IOException | InterruptedException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            m_Log.error(e.toString(), e);
        }
    }
    /*********************************************************************************************/
}