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 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 cb_comm) { cb_comm.removeAllItems(); @SuppressWarnings("unchecked") Enumeration 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 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); } } /*********************************************************************************************/ }