package com.dev.fbs9600_groupvol;
|
import gnu.io.SerialPort;
|
import gnu.io.UnsupportedCommOperationException;
|
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.Date;
|
|
import javax.swing.JOptionPane;
|
import javax.swing.JTextField;
|
|
import org.apache.logging.log4j.Logger;
|
|
import sp_comm.CommSerialPort;
|
import sp_comm.Comm_Socket;
|
import main.page_debug_inf;
|
|
import com.Com;
|
import com.Crc16;
|
|
public class SPCommGroupVol implements Runnable/*, SerialPortEventListener*/ {
|
public static final int COMM_PORT_TYPE_Serial = 0;
|
public static final int COMM_PORT_TYPE_Socket = 1;
|
|
public static final byte CMD_TYPE_READ = 0x03;
|
public static final byte CMD_TYPE_WRITE_Multy = 0x10;
|
public static final short UART_MonomerSetAddr_RegAddr = 0001;
|
public static final short UART_MonomerAdjOffSet_RegAddr = 0002; //Æ«ÒÆÁ¿
|
public static final short UART_MonomerAdjSlope_RegAddr = 0003; //бÂÊ
|
|
public static final short UART_MonomerVer_RegAddr = 1001;
|
|
public static final int DEV_TYPE_MON_SAMD09 = 0;
|
public static final int DEV_TYPE_MON_MSP430 = 1;
|
public static final int DEV_TYPE_MON_MSP430_RF = 2;
|
public static final int DEV_TYPE_MON_SWM = 3; //»ªÐ¾ÍþÌØmcu
|
public static final int DEV_TYPE_Curr = 100;
|
public static final int DEV_TYPE_GroupVol = 110;
|
public static final int DEV_TYPE_FCF651GroupVol = 111;
|
public static final int DEV_TYPE_KW8 = 120;
|
public static final int DEV_TYPE_BYBB = 130;
|
|
public int m_DEV_TYPE = DEV_TYPE_FCF651GroupVol;
|
|
private CommSerialPort m_SP_Comm = null;
|
private Comm_Socket m_SocketComm = null;
|
// ÊäÈëÊä³öÁ÷
|
public InputStream inputStream;
|
public OutputStream outputStream;
|
// RS-232µÄ´®ÐпÚ
|
public SerialPort serialPort;
|
|
public boolean CommThreadRunning = false;
|
private ByteBuffer CommRxBuffer = ByteBuffer.allocate(512);
|
private ByteBuffer CommTxBuffer = ByteBuffer.allocate(512);
|
|
public short comm_tx_cnt = 0;
|
public short comm_rx_cnt = 0;
|
|
public short comm_OK_cnt = 0;
|
public short comm_Err_cnt = 0;
|
public boolean dt_new_tag = false;
|
|
public int dt_target_addr = 0;
|
public short dt_addr = 0;
|
public int dt_vol = 0;
|
public int dt_version = 0;
|
public int dt_volcal_offset = 0;
|
public int dt_volcal_slope = 0;
|
//----------------------------------------------------------//
|
//----------------------------------------------------------//
|
|
private short dt_cmd = CMD_TYPE_READ;
|
private short dt_cmd_regaddr = 0;
|
private short dt_cmd_regcnt = 0;
|
private short dt_cmd_data = 0;
|
private short dt_cmd_data1 = 0;
|
int dt_multy_comm = CommSerialPort.mutycomm_Type_Samd09;
|
private short dt_auto_read_regaddr = UART_MonomerVer_RegAddr;
|
|
private JTextField addr_TextField = null;
|
|
|
private page_debug_inf dt_debug_inf;
|
private int m_COMM_PORT_TYPE = 0;
|
|
private Logger m_Log = null;
|
|
public SPCommGroupVol(int addr_t, int bitrate, int dev_type,
|
int multy_com_type, page_debug_inf debug, Logger log) {
|
dt_target_addr = addr_t & 0xFFFF;
|
dt_multy_comm = multy_com_type;
|
dt_debug_inf = debug;
|
//-------------------------------------------------------------//
|
m_DEV_TYPE = dev_type;
|
//-------------------------------------------------------------//
|
m_Log = log;
|
}
|
|
public SPCommGroupVol(JTextField addr_t, int bitrate, int dev_type,
|
int multy_com_type, page_debug_inf debug) {
|
//dt_target_addr = addr_t & 0xFFFF;
|
addr_TextField = addr_t;
|
dt_multy_comm = multy_com_type;
|
dt_debug_inf = debug;
|
//-------------------------------------------------------------//
|
m_DEV_TYPE = dev_type;
|
//-------------------------------------------------------------//
|
}
|
|
public void setCommAddr(int addr_t) {
|
dt_target_addr = addr_t & 0xFFFF;
|
}
|
|
public void setCommBautrate(int bt_rate) {
|
if(COMM_PORT_TYPE_Serial != m_COMM_PORT_TYPE) {
|
return;
|
}
|
|
try {
|
m_SP_Comm.serialPort.setSerialPortParams(bt_rate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
|
} catch (UnsupportedCommOperationException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
|
//³õʼ»¯´®¿Ú
|
public boolean OpenCommPort(int comm_port_type, String comm_name_or_ip, int bitrate_or_comm_port) {
|
boolean res = false;
|
m_COMM_PORT_TYPE = comm_port_type;
|
if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
|
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;
|
}
|
|
if(true == res) {
|
CommTxBuffer.order(ByteOrder.BIG_ENDIAN);
|
CommRxBuffer.order(ByteOrder.BIG_ENDIAN);
|
comm_tx_cnt = 0;
|
comm_rx_cnt = 0;
|
CommThreadRunning = true;
|
}
|
|
return res;
|
}
|
|
private void makeVolModule_CommTxData() {
|
CommTxBuffer.clear();
|
|
if(null != addr_TextField) {
|
dt_target_addr = (short) Integer.parseInt(addr_TextField.getText()) & 0xFFFF;
|
}
|
|
if(CMD_TYPE_READ == dt_cmd) {
|
if(UART_MonomerVer_RegAddr == dt_auto_read_regaddr) {
|
dt_auto_read_regaddr = UART_MonomerSetAddr_RegAddr;
|
} else {
|
dt_auto_read_regaddr = UART_MonomerVer_RegAddr;
|
}
|
|
CommTxBuffer.put((byte) dt_target_addr);
|
CommTxBuffer.put((byte) CMD_TYPE_READ);
|
CommTxBuffer.put((byte) (dt_auto_read_regaddr>>8));
|
CommTxBuffer.put((byte) dt_auto_read_regaddr);
|
CommTxBuffer.put((byte) 0x00);
|
CommTxBuffer.put((byte) 0x03);
|
} else {
|
byte datlen = (byte) (dt_cmd_regcnt*2);
|
CommTxBuffer.put((byte) dt_target_addr);
|
CommTxBuffer.put((byte) CMD_TYPE_WRITE_Multy);
|
CommTxBuffer.put((byte) (dt_cmd_regaddr>>8));
|
CommTxBuffer.put((byte) dt_cmd_regaddr);
|
CommTxBuffer.put((byte) (dt_cmd_regcnt>>8));
|
CommTxBuffer.put((byte) dt_cmd_regcnt);
|
CommTxBuffer.put((byte) datlen);
|
CommTxBuffer.put((byte) (dt_cmd_data>>8));
|
CommTxBuffer.put((byte) dt_cmd_data);
|
if(dt_cmd_regcnt > 1) {
|
CommTxBuffer.put((byte) (dt_cmd_data1>>8));
|
CommTxBuffer.put((byte) dt_cmd_data1);
|
}
|
dt_cmd_regcnt = 0;
|
dt_cmd = CMD_TYPE_READ;
|
}
|
|
int crc = Crc16.CalCRC16(CommTxBuffer, CommTxBuffer.position()) & 0xFFFF;
|
CommTxBuffer.putShort((short) crc);
|
CommTxBuffer.flip();
|
}
|
|
private boolean checkCommBuf(ByteBuffer buf_t) {
|
boolean res = false;
|
if(buf_t.limit() >= 5) {
|
buf_t.position(buf_t.limit()-2);
|
int crc = buf_t.getShort() & 0xFFFF;
|
//crc = ((crc>>8) | (crc<<8)) & 0xFFFF;
|
int crc_cal = Crc16.CalCRC16(buf_t, buf_t.limit()-2) & 0xFFFF;
|
/*************************************************/
|
if(crc_cal == crc) {
|
res = true;
|
}
|
/*************************************************/
|
} else {
|
res = false;
|
}
|
|
return res;
|
}
|
|
public void tr_Msg(ByteBuffer bbf_tx, ByteBuffer bbf_rx, boolean showdat) {
|
if(++comm_tx_cnt > 32500) {
|
comm_tx_cnt = 0;
|
}
|
if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
|
m_SP_Comm.SP_Comm(bbf_tx, bbf_rx, dt_multy_comm, dt_debug_inf, showdat);
|
} else {
|
m_SocketComm.SocketComm(bbf_tx, bbf_rx, dt_debug_inf, showdat);
|
}
|
}
|
|
private void closeCommPort() {
|
if(COMM_PORT_TYPE_Serial == m_COMM_PORT_TYPE) {
|
m_SP_Comm.serialPort.close();
|
} else {
|
m_SocketComm.socketClose();
|
}
|
}
|
|
public void exitCommPortThread() {
|
CommThreadRunning = false;
|
}
|
|
public void setCommCmd(short cmd, short cmd_reg_addr, short cmd_reg_cnt,
|
short cmd_data, short cmd_data1) {
|
dt_cmd = cmd;
|
dt_cmd_regaddr = cmd_reg_addr;
|
dt_cmd_regcnt = cmd_reg_cnt;
|
dt_cmd_data = cmd_data;
|
dt_cmd_data1 = cmd_data1;
|
}
|
|
private void processVolModule_RxData(ByteBuffer bbf_rx, short addr_reg) {
|
if(false == checkCommBuf(bbf_rx)) {
|
return;
|
}
|
|
if(bbf_rx.limit() >= 7) {
|
bbf_rx.position(0);
|
dt_addr = (short) (bbf_rx.get() & 0xFF);
|
byte cmd_t = bbf_rx.get();//cmd
|
bbf_rx.get();//bytes
|
if(CMD_TYPE_READ == cmd_t) {
|
if(UART_MonomerVer_RegAddr == addr_reg) {
|
dt_version = bbf_rx.getShort() & 0xFFFF;
|
dt_vol = bbf_rx.getShort() & 0xFFFF;
|
dt_vol = (dt_vol) | (((int)(bbf_rx.getShort() & 0xFFFF))<<16);
|
} else {
|
bbf_rx.getShort();
|
dt_volcal_offset = bbf_rx.getShort();
|
dt_volcal_slope = bbf_rx.getShort() & 0xFFFF;
|
}
|
}
|
|
if(++comm_rx_cnt > 32500) {
|
comm_rx_cnt = 0;
|
}
|
if(comm_OK_cnt < 1000) {
|
comm_OK_cnt += 1;
|
}
|
}
|
}
|
|
public void run() {
|
while(true == CommThreadRunning) {
|
try {
|
//---------------------- make tx data --------------------------//
|
makeVolModule_CommTxData();
|
//----------------------- tx rx data ---------------------------//
|
tr_Msg(CommTxBuffer, CommRxBuffer, true);
|
//----------------------- process data -------------------------//
|
processVolModule_RxData(CommRxBuffer, dt_auto_read_regaddr);
|
//-------------------------------------------------------------//
|
|
Thread.sleep(500);
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
exitCommPortThread();
|
JOptionPane.showMessageDialog(null, "FCF651 GroupVol Module CommPort ´®¿ÚÒì³£: " + e.getMessage());
|
}
|
}
|
/******************************************************************************/
|
closeCommPort();
|
/******************************************************************************/
|
dt_debug_inf.addDebugInf(Com.get_DTF(new Date(), Com.DTF_YMDhms_S)
|
+ " CommPort is quit, Dev Id: " + m_DEV_TYPE
|
+ ", COMM_PORT_TYPE: " + m_COMM_PORT_TYPE + "\n");
|
/******************************************************************************/
|
}
|
}
|