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