package com.dev.ntm; import gnu.io.SerialPort; import gnu.io.UnsupportedCommOperationException; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Date; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField; import org.apache.logging.log4j.Logger; import sp_comm.CommSerialPort; import sp_comm.SP_COMM; import main.page_debug_inf; import com.Com; import com.PlaySound; import com.dev.fbs9600_mon.SPCommMon; import com.dev.fbs9600_mon.SPCommMon_DFU; import com.dev.ntm.cmd.NTM_Cmd; import com.dev.ntm.cmd.NTM_ComBuf; import com.dev.ntm.cmd.NTM_MonData; import com.dev.ntm.cmd.NTM_TestData; import com.dev.ntm.cmd.NTM_TestParam; public class SPCommNTM implements Runnable { public boolean CommThreadRunning = false; private ByteBuffer CommRxBuffer = ByteBuffer.allocate(15000); private ByteBuffer CommTxBuffer = ByteBuffer.allocate(1048); public int comm_tx_cnt = 0; public int comm_rx_cnt = 0; private int comm_bautrate = 9600; public int dt_target_addr = 0; //--------------------------------------------------------// //--------------------------------------------------------// public int dev_version = 0; public String dev_type = ""; //--------------------------------------------------------// private short dt_cmd = NTM_Cmd.CMD_LogIn; private short dt_usr_cmd = NTM_Cmd.CMD_NULL; public short dt_cmd_ack = 0; public Date dt_cmd_ack_time = new Date(); private int dt_cmd_cnt = 0; private boolean mLoginStat = false; private ByteBuffer dt_datatoCMD = ByteBuffer.allocate(0); private ByteBuffer dt_datatoINF = ByteBuffer.allocate(0); public NTM_Cmd m_NTM_Cmd = new NTM_Cmd(); public NTM_TestData m_TestData = new NTM_TestData(); public NTM_TestParam m_TestParam = new NTM_TestParam(); public NTM_MonData m_MonData = new NTM_MonData(); public String mDevInf = ""; public String mCurrentBattGroupName = ""; public int mBattGroupNameCnt = 0; public int mReadBattGroupNameindex = 0; public int mSetBattGroupNameindex = 0; public String m_SelectedFilePathName = ""; //Êý¾Ý¹ÜÀíÒ³ÃæÑ¡ÔñÊý¾ÝÎļþÃû³Æ,¸ñʽΪ£ºµç³Ø×éÃû/Êý¾ÝÎļþÃû public ArrayList mBattGroupNames = new ArrayList(); public int mTestFileCnt = 0; public int mReadTestFileindex = 0; public int mSetTestFileindex = 0; public ArrayList mTestFiles = new ArrayList(); private boolean mReadFileStat = false; private int mReadFileTimeOut = 0; public int mDatablockCnt = 0; public int mDatablockIndex = 0; private page_debug_inf dt_debug_inf; private int m_COMM_PORT_TYPE = 0; //private CommSerialPort m_SP_Comm = null; //private Comm_Socket m_SocketComm = null; private SP_COMM m_SP_COMM = null; SPCommMon_DFU m_SPCommDFU = null; public JTextField tf_opres_inf = null; public boolean dt_update_en = false; private String dt_update_file = ""; public JTextArea dt_show_dfu_inf = null; public byte[] dt_task_inf = new byte[1524]; private Logger m_Log = null; public SPCommNTM(int comm_type, int addr_t, int bitrate, JTextArea show_dfu_inf, page_debug_inf debug, Logger log) { m_Log = log; m_SP_COMM = new SP_COMM(comm_type, 12245, m_Log); dt_target_addr = addr_t & 0x00FF; comm_bautrate = bitrate; dt_show_dfu_inf = show_dfu_inf; dt_debug_inf = debug; } public void setCommBautrate(int bt_rate) { if(SP_COMM.COMM_TYPE_Serial != m_SP_COMM.m_CommType) { return; } try { comm_bautrate = bt_rate; m_SP_COMM.m_SP_Comm.serialPort.setSerialPortParams(comm_bautrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //³õʼ»¯´®¿Ú public boolean OpenCommPort(String comm_name_or_ip, int bitrate_or_comm_port) { boolean res = m_SP_COMM.openCommPort(comm_name_or_ip, bitrate_or_comm_port); if(true == res) { CommTxBuffer.order(ByteOrder.LITTLE_ENDIAN); CommRxBuffer.order(ByteOrder.LITTLE_ENDIAN); comm_tx_cnt = 0; comm_rx_cnt = 0; CommThreadRunning = true; } return res; } private ByteBuffer makeCommTxData() { ByteBuffer cmd_buf = ByteBuffer.allocate(0); ByteBuffer inf_buf = ByteBuffer.allocate(0); if(/*(false == mReadFileStat) &&*/ (NTM_Cmd.CMD_NULL == dt_usr_cmd)) { if(false == mLoginStat) { dt_cmd = NTM_Cmd.CMD_LogIn; } else { if("" == mDevInf) { dt_cmd = NTM_Cmd.CMD_GetVersion; } else { if(++dt_cmd_cnt > 1000) { dt_cmd_cnt = 0; } dt_cmd = NTM_Cmd.CMD_GetDischargeData; if(0 == dt_cmd_cnt%3) { dt_cmd = NTM_Cmd.CMD_GetMonData; } if(0 == dt_cmd_cnt%5) { dt_cmd = NTM_Cmd.CMD_HeartBeat; //get current battgroup name } } } } else { dt_cmd = dt_usr_cmd; cmd_buf = dt_datatoCMD; inf_buf = dt_datatoINF; dt_usr_cmd = NTM_Cmd.CMD_NULL; } ByteBuffer buf_t = NTM_ComBuf.makeNTMCommBuf(dt_target_addr, dt_cmd, cmd_buf, inf_buf, false); return buf_t; } public boolean tr_Msg(ByteBuffer bbf_tx, ByteBuffer bbf_rx, boolean showdat) { if(++comm_tx_cnt > Com.CNT_MAX) { comm_tx_cnt = 0; } return m_SP_COMM.tr_Msg(bbf_tx, bbf_rx, CommSerialPort.mutycomm_Type_Samd09, dt_debug_inf, showdat); } public void exitCommThread() { CommThreadRunning = false; if(null != m_SPCommDFU) { m_SPCommDFU.setDFUCommState(false, false); } } public void setCommCmd(short cmd, ByteBuffer cmd_data, ByteBuffer inf_data) { if(NTM_Cmd.CMD_NULL != dt_usr_cmd) { return; } if(true == mReadFileStat) { if(mReadFileTimeOut < 100) { mReadFileTimeOut += 1; } if(mReadFileTimeOut >= 10) { mReadFileStat = false; } else { if((NTM_Cmd.CMD_GetBattGroupNames!=cmd) && (NTM_Cmd.CMD_GetTestFileNames!=cmd) && (NTM_Cmd.CMD_GetTestFile!=cmd)) { return; } } } if((NTM_Cmd.CMD_GetBattGroupNames==cmd) || (NTM_Cmd.CMD_GetTestFileNames==cmd) ||(NTM_Cmd.CMD_GetTestFile==cmd)) { mReadFileStat = true; mReadFileTimeOut = 0; } dt_usr_cmd = cmd; dt_datatoCMD = cmd_data; dt_datatoINF = inf_data; } public void setUpdateCmd(boolean update_en, String filepath) { dt_update_en = update_en; dt_update_file = filepath; if(true == dt_update_en) { dt_usr_cmd = NTM_Cmd.CMD_StartDFU; dt_datatoCMD = null; dt_datatoINF = null; } else { if(null != m_SPCommDFU) { m_SPCommDFU.setDFUCommState(false, false); } } } private void playSound() { new PlaySound("6133.wav"); } public void setCommCmdAck(short ack_t) { dt_cmd_ack = ack_t; dt_cmd_ack_time = new Date(); } public void setSelectFilePathName(String path) { m_SelectedFilePathName = path; } public String getSelectFilePathName() { return m_SelectedFilePathName; } /*********************************************************************************************/ public String getDataFromCommBuf(ByteBuffer bf) { String msg = ""; if(true == m_NTM_Cmd.putByteBuffer(bf)) { switch(m_NTM_Cmd.CMD) { case NTM_Cmd.CMD_LogIn: { mLoginStat = true; dev_version = m_NTM_Cmd.DB1; dev_type = String.valueOf(m_NTM_Cmd.DB3) + "." + String.valueOf(m_NTM_Cmd.DB2); dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " Login NTM device, dev_type: " + dev_type + ", dev_version: " + dev_version + "\n"); } break; case NTM_Cmd.CMD_LogOut: { dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " Logout NTM device, dev_version: " + mDevInf.replaceAll("\n", ",") + "\n"); } break; case NTM_Cmd.CMD_GetVersion: { byte[] bt = new byte[bf.limit()]; bf.get(bt); mDevInf = new String(bt); dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " Get version: " + mDevInf + "\n"); } break; case NTM_Cmd.CMD_HeartBeat: { byte[] bt = new byte[bf.limit()]; bf.get(bt); try { mCurrentBattGroupName = new String(bt, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; case NTM_Cmd.CMD_GetBattGroupNames: { byte[] bt = new byte[bf.limit()]; bf.get(bt); mBattGroupNameCnt = m_NTM_Cmd.DB1*256 + m_NTM_Cmd.DB2; mReadBattGroupNameindex = m_NTM_Cmd.DB3*256 + m_NTM_Cmd.DB4; String bg_name = ""; try { bg_name = new String(bt, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(0 == mReadBattGroupNameindex) { mBattGroupNames.clear(); } mBattGroupNames.add(bg_name); mReadBattGroupNameindex += 1; msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD) + mReadBattGroupNameindex + "/" + mBattGroupNameCnt; setCommCmdAck(m_NTM_Cmd.CMD); if(mReadBattGroupNameindex < mBattGroupNameCnt) { ByteBuffer bf_cmd = ByteBuffer.allocate(2); bf_cmd.put((byte) (mReadBattGroupNameindex/256)); bf_cmd.put((byte) (mReadBattGroupNameindex%256)); bf_cmd.position(0); setCommCmd(NTM_Cmd.CMD_GetBattGroupNames, bf_cmd, null); } else { mReadFileStat = false; playSound(); } } break; case NTM_Cmd.CMD_GetTestFileNames: { byte[] bt = new byte[bf.limit()]; bf.get(bt); mTestFileCnt = m_NTM_Cmd.DB1*256 + m_NTM_Cmd.DB2; mReadTestFileindex = m_NTM_Cmd.DB3*256 + m_NTM_Cmd.DB4; String file_name = ""; try { file_name = new String(bt, "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(0 == mReadTestFileindex) { mTestFiles.clear(); } mTestFiles.add(file_name); mReadTestFileindex += 1; msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD) + mReadTestFileindex + "/" + mTestFileCnt; setCommCmdAck(m_NTM_Cmd.CMD); if(mReadTestFileindex < mTestFileCnt) { ByteBuffer bf_cmd = ByteBuffer.allocate(4); bf_cmd.put((byte) (mSetBattGroupNameindex/256)); bf_cmd.put((byte) (mSetBattGroupNameindex%256)); bf_cmd.put((byte) (mReadTestFileindex/256)); bf_cmd.put((byte) (mReadTestFileindex%256)); bf_cmd.position(0); setCommCmd(NTM_Cmd.CMD_GetTestFileNames, bf_cmd, null); } else { mReadFileStat = false; playSound(); } } break; case NTM_Cmd.CMD_GetTestFile: { byte[] bt = new byte[bf.limit()]; bf.get(bt); mDatablockCnt = m_NTM_Cmd.DB1*256 + m_NTM_Cmd.DB2; mDatablockIndex = m_NTM_Cmd.DB3*256 + m_NTM_Cmd.DB4; if(0 == mDatablockIndex) { File file = new File("ntm.fbo"); if(file.exists()) { file.delete(); } } RandomAccessFile randomFile; try { randomFile = new RandomAccessFile("ntm.fbo", "rw"); randomFile.seek(randomFile.length());//½«Ð´ÎļþÖ¸ÕëÒÆµ½Îļþβ randomFile.write(bt); randomFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDatablockIndex += 1; msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD) + mDatablockIndex + "/" + mDatablockCnt; setCommCmdAck(m_NTM_Cmd.CMD); if(mDatablockIndex < mDatablockCnt) { ByteBuffer bf_cmd = ByteBuffer.allocate(2); bf_cmd.put((byte) (mDatablockIndex/256)); bf_cmd.put((byte) (mDatablockIndex%256)); bf_cmd.position(0); ByteBuffer bf_inf = null; try { bf_inf = ByteBuffer.wrap(m_SelectedFilePathName.getBytes("utf-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } bf_inf.position(0); setCommCmd(NTM_Cmd.CMD_GetTestFile, bf_cmd, bf_inf); } else { mReadFileStat = false; playSound(); } } break; case NTM_Cmd.CMD_GetDischargeData: { m_TestData.putByteBuffer(bf); } break; case NTM_Cmd.CMD_GetMonData: { int mon_cnt = m_NTM_Cmd.DB1*256 + m_NTM_Cmd.DB2; m_MonData.putByteBuffer(bf, mon_cnt); } break; case NTM_Cmd.CMD_ChangeToParamPage: { playSound(); msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD); setCommCmdAck(m_NTM_Cmd.CMD); try { Thread.sleep(800); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setCommCmd(NTM_Cmd.CMD_SetDisParm, null, m_TestParam.getByteBuffer()); } break; case NTM_Cmd.CMD_GetDisParm: { m_TestParam.putByteBuffer(bf); playSound(); msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD); setCommCmdAck(m_NTM_Cmd.CMD); } break; case NTM_Cmd.CMD_SetDisParm: { playSound(); msg = NTM_Cmd.getAckCmdText(m_NTM_Cmd.CMD); setCommCmdAck(m_NTM_Cmd.CMD); try { Thread.sleep(800); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setCommCmd(NTM_Cmd.CMD_GetDisParm, null, null); } break; case NTM_Cmd.CMD_GetTaskInf: { bf.position(0); for(int n=0; n 0) { tf_opres_inf.setText(msg); } } if(++comm_rx_cnt > 1000000) { comm_rx_cnt = 0; } } return msg; } private void closeNTMCommPort() { m_SP_COMM.closeCommPort(); } public void run() { int run_cnt = 0; while(true == CommThreadRunning) { try { Thread.sleep(100); if(false == dt_update_en) { if((NTM_Cmd.CMD_NULL != dt_usr_cmd) || (0 == (run_cnt%5))) { tr_Msg(makeCommTxData(), CommRxBuffer, true); getDataFromCommBuf(CommRxBuffer); } } else { try { setCommCmd(NTM_Cmd.CMD_StartDFU, null, null); CommTxBuffer.clear(); CommTxBuffer.put(makeCommTxData()); CommTxBuffer.flip(); tr_Msg(CommTxBuffer, CommRxBuffer, true); Thread.sleep(100); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } String dfu_text_inf = ""; m_SPCommDFU = new SPCommMon_DFU(m_SP_COMM, SPCommMon.DEV_TYPE_NTM, dt_target_addr, true, dt_show_dfu_inf, dt_debug_inf); m_SPCommDFU.runDFU(dt_update_file, dfu_text_inf); dt_update_en = false; setCommCmd(NTM_Cmd.CMD_NULL, null, null); //-----------------------------------------------------------------------------// dfu_text_inf = dt_show_dfu_inf.getText(); dt_show_dfu_inf.setText(dfu_text_inf + "DFU Process Stopped!\n"); //-----------------------------------------------------------------------------// //-----------------------------------------------------------------------------// } run_cnt += 1; if(run_cnt > 999) { run_cnt = 0; } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); exitCommThread(); JOptionPane.showMessageDialog(null, "NTM CommPort ͨÐÅÒì³£: " + e.getMessage()); } } /******************************************/ setCommCmd(NTM_Cmd.CMD_LogOut, ByteBuffer.allocate(0), ByteBuffer.allocate(0)); tr_Msg(makeCommTxData(), CommRxBuffer, true); getDataFromCommBuf(CommRxBuffer); closeNTMCommPort(); /******************************************/ dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S) + " NTM CommPort is quit," + " COMM_PORT_TYPE: " + m_COMM_PORT_TYPE + "\n"); /******************************************/ } }