| | |
| | | import com.fgkj.action.ServiceModel;
|
| | | import com.google.gson.Gson;
|
| | | import com.serial.SerialPort;
|
| | | import com.util.ComFn;
|
| | |
|
| | | import java.io.File;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.nio.ByteBuffer;
|
| | | import java.nio.ByteOrder;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | |
|
| | | /**
|
| | | * 底层和汇集器通讯线程
|
| | |
| | | public Handler mHandler = null;
|
| | | private boolean thread_run_flag = true;
|
| | | private Gson gson = new Gson();
|
| | | private ServiceModel remodel;
|
| | | private ServiceModel remodel = new ServiceModel();
|
| | |
|
| | | private CommSockClientThread(){
|
| | | this.concentratorState = new Concentrator_State();
|
| | | this.concentratorParam = new Concentrator_Param();
|
| | | }
|
| | |
|
| | |
|
| | | /**
|
| | | * 构造当前类的单例对象
|
| | |
| | | public static CommSockClientThread createClientThread(){
|
| | | if(clientThread == null){
|
| | | clientThread = new CommSockClientThread();
|
| | | clientThread.start();
|
| | | }
|
| | | return clientThread;
|
| | | }
|
| | |
| | | public void handleMessage(Message msg)
|
| | | {
|
| | | ServiceModel model = (ServiceModel) (msg.obj);
|
| | | // int what = msg.what;
|
| | | // List<BattDataThread> battData = FBS9600S_DeviceService.allBattDatt;
|
| | | // if(model.battindex>0 && model.battindex<FBS9600S_DeviceService.MAXCONCENTRATORCOUNT){
|
| | | // if(!battData.get(model.battindex-1).isInstall){
|
| | | // return;
|
| | | // }
|
| | | // }
|
| | | try
|
| | | {
|
| | | if(null != model.mhandler)
|
| | |
| | | }
|
| | | //-----------------------------------------------
|
| | | }
|
| | | //Log.e(TAG, "battindex:"+remodel.battindex+"====handleMessage: cmd"+remodel.cmd+"\tcmd_type:"+remodel.cmd_type+"\t"+model.mhandler );
|
| | | Handler handler = remodel.mhandler;
|
| | | Message toMain = handler.obtainMessage();
|
| | | toMain.obj = remodel;
|
| | | handler.sendMessage(toMain);
|
| | | sleep(100);
|
| | | sleep(5);
|
| | | }
|
| | | }
|
| | | catch (InterruptedException e)
|
| | |
| | | * @throws InterruptedException
|
| | | */
|
| | |
|
| | | public boolean SocketComm(byte[] datasource,ServiceModel model) throws InterruptedException
|
| | | public boolean SocketComm(final byte[] datasource,ServiceModel model) throws InterruptedException
|
| | | {
|
| | | //Log.e(TAG, "SocketComm: "+model );
|
| | | remodel = model.clone();
|
| | | boolean res_t = false;
|
| | | try
|
| | |
| | | }
|
| | | //=====================================================================//
|
| | | */
|
| | | //Log.d(TAG, "SocketComm: ");
|
| | | Log.e(TAG, "发送数据:"+ComFn.bytesToHexString(plain_tx_t,plain_tx_t.length));
|
| | | int rx_read_time_out = 0;
|
| | | //int rx_len = 0;
|
| | | out.write(cipher_tx_t);
|
| | | out.write(plain_tx_t);
|
| | | out.flush();
|
| | | while(true) {
|
| | | if(in.available() > 0) {
|
| | |
| | | //bytebuffer_for_socket_RX.put((byte)in.read());
|
| | | } else {
|
| | | rx_read_time_out++;
|
| | | if((bytebuffer_for_socket_RX.position() >= 8) && (rx_read_time_out>10)){
|
| | | if((bytebuffer_for_socket_RX.position() >= 8) && (rx_read_time_out>20)){
|
| | | // res = true;
|
| | | break;
|
| | | }
|
| | | if(rx_read_time_out > 220) {
|
| | | if(rx_read_time_out > 30) {
|
| | | break;
|
| | | }
|
| | | }
|
| | |
| | |
|
| | | bytebuffer_for_socket_RX.flip();
|
| | |
|
| | | Date d2 = new Date();
|
| | | long comm_tms = (d2.getTime() - d1.getTime());
|
| | | if(comm_tms < 200) {
|
| | | Thread.sleep(200 - comm_tms);
|
| | | }
|
| | | // Date d2 = new Date();
|
| | | // long comm_tms = (d2.getTime() - d1.getTime());
|
| | | // if(comm_tms < 200) {
|
| | | // Thread.sleep(200 - comm_tms);
|
| | | // }
|
| | | /*
|
| | | System.out.println(this.getName()
|
| | | + " dev_id: " + m_StatAndParam.dev_id
|
| | |
| | | bytebuffer_for_socket_RX.get(cipher_buf);
|
| | |
|
| | |
|
| | | //Log.e(TAG, "SocketComm: "+ComFn.bytesToHexString(plain_buf, plain_buf.length));
|
| | | //Log.e(TAG, "接收数据: "+ ComFn.bytesToHexString(cipher_buf, cipher_buf.length));
|
| | | if(true == getDataFromCommBuf(cipher_buf,remodel)) {
|
| | | rx_errcount_for_live_of_thread = 0;
|
| | | res_t = true;
|
| | |
| | | return res_t;
|
| | | }
|
| | |
|
| | | public boolean getDataFromCommBuf(byte[] recebyte,ServiceModel model){
|
| | | public boolean getDataFromCommBuf(byte[] recebyte,final ServiceModel model){
|
| | | boolean isSuccess = false;
|
| | | ByteBuffer bf = ByteBuffer.allocate(recebyte.length);
|
| | | bf.order(ByteOrder.BIG_ENDIAN);
|
| | |
| | | if(Ccmd.ByteCount == Concentrator_State.BYTE_LEN){
|
| | | //读取系统状态
|
| | | if(concentratorState.putByteBuffer(bf)){
|
| | | //Log.e(TAG, "获取设备状态成功 " );
|
| | | isSuccess = true; //读取系统状态成功
|
| | | }
|
| | | }
|
| | |
| | | if(Ccmd.ByteCount == Concentrator_Param.REG_COUNT*2){
|
| | | //读取系统参数
|
| | | if(concentratorParam.putByteBuffer(bf)){
|
| | | Concentrator_State.monCount = concentratorParam.countMonCount();
|
| | | //Log.e(TAG, "获取设备参数成功 " );
|
| | | isSuccess = true;
|
| | | }
|
| | | }
|
| | |
|
| | | if(Ccmd.ByteCount == Concentrator_State.MON_REG_COUNT){
|
| | | if(Ccmd.ByteCount == Concentrator_State.MON_REG_COUNT*2){
|
| | | //读取单体电压/内阻/温度
|
| | | if(concentratorState.putMonDataBuffer(bf,model.cmd)) {
|
| | | isSuccess = true;
|
| | |
| | | this.socket.close();
|
| | | this.socket = null;
|
| | | }
|
| | | }else{
|
| | | Log.e(TAG, "initConnectDevice: 打开串口成功" );
|
| | | }
|
| | | }
|
| | | }
|
| | |
| | | Concentrator_ComBase.CMD_MODEBUS_SINGLEWRITECMD == model.cmd_type
|
| | | ){
|
| | | Concentrator_Cmd Ccmd = new Concentrator_Cmd();
|
| | | Ccmd.makeCmd(0xFF,model.cmd_type,model.cmd);
|
| | | Ccmd.makeCmd(model.battindex,model.cmd_type,model.cmd);
|
| | | ByteBuffer buffer = null;
|
| | | if(Concentrator_ComBase.CMD_MODEBUS_SYSTEMPARAM_INDEX == model.cmd){
|
| | | //读写汇集器参数
|
| | |
| | | //控制时传值(内阻测试[启动停止]/重启系统)
|
| | | buffer = Ccmd.getReadByteBuffer(Concentrator_ComBase.CMD_CONTROL_VALUE);
|
| | | }
|
| | |
|
| | | b = makeCommBuf(Ccmd,buffer);
|
| | | //Log.e(TAG, "****makeComData: "+ComFn.bytesToHexString(b,b.length) );
|
| | | }else{
|
| | | //Log.e(TAG, "makeComData: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
|
| | | b = new byte[0];
|
| | | }
|
| | | return b;
|