⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modbusmonitor.java

📁 工控modbus协议通讯
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

		case PRESET_SINGLE_REGISTER:

			nError=m_Modbus.PresetSingleRegister(nDevice,(int)nAddress,nValue);
			break;
		case FORCE_SINGLE_COIL:

            nError=m_Modbus.ForceSingleCoil(nDevice,nAddress,(nValue!=0));
			break;
			
			
		}
		
	
		if (Modbus.ERR_OK !=nError) {
			SetStatusCom("Write Failed");
			  
		}
		else {
		    SetStatusCom("Ok"); 
		}
	
	}

	private int atoi(String sNumber) {
		int iNumber;
		
        try {
			iNumber=(Integer.valueOf(sNumber)).intValue();
		}
		catch (NumberFormatException e){
			
			iNumber=0;
		}

		return iNumber;
	}

    private void StartPoll() 
	{
		if ((!m_Modbus.IsConnected())&&(!m_Modbus.ReConnectOnEveryMessage())){
			SetStatusCom("Not Connected");
			return;
		}
		m_lNumPolls=0;
		m_lNumErrors=0;
        m_bPollActive=true;
		EnableCancelPollButtons(true);
	}

	private void StopPoll() 
	{
		EnableCancelPollButtons(false);
		m_bPollActive=false;

	}

	private void EnableCancelPollButtons(boolean bEnable) {
	    
		if (bEnable) {
			m_dlgMonitor.cmdCancel.enable();  
			m_dlgMonitor.cmdStart.disable();
		}
		else {
		   m_dlgMonitor.cmdCancel.disable();  
		   m_dlgMonitor.cmdStart.enable();
		}


	}

	private void ReadFuncionOld(int iFunction) {
		
		short nError=0;
		short nDevice;
		short nQuantity;
		int nAddress;
		short anRegValues[];
		boolean abCoilValues[];
		String sStat;
		String sValues="";
		String sText="";
		int i;
        long  lInitialTime;
		long  lDuration=0; 
		char cValue;
		int iListCount; 
		int iSelectedItem=-1;

		nAddress=atoi(m_dlgMonitor.txtAddress.getText());
		nQuantity=(short)atoi(m_dlgMonitor.txtQuantity.getText());
        nDevice=(short)atoi(m_dlgMonitor.txtDevice.getText()); 
        anRegValues=new short[0];
		abCoilValues=new boolean[0];

		iListCount=m_dlgMonitor.lstValues.countItems();
	
		if (nQuantity>iListCount) {
			for(i=1;i<=(nQuantity-iListCount);i++){
				m_dlgMonitor.lstValues.addItem(" ");
			}
		}

		if (nQuantity<iListCount) {
			m_dlgMonitor.lstValues.delItems(nQuantity,iListCount-1);
		} 

		m_Modbus.ThrowCommException(false);

		switch (iFunction) {
				
		case LOOPBACK_TEST:
            lInitialTime=System.currentTimeMillis();
			try{
				nError=m_Modbus.LoopbackTest(nDevice);
			}
			catch (ModbusException mbExcetion) {
				nError=(short)mbExcetion.ErrorNumber();
				
			}
			lDuration=System.currentTimeMillis()-lInitialTime;
			anRegValues=new short[0];
			abCoilValues=new boolean[0];
            break;
		case READ_OUTPUT_REGISTERS:
			lInitialTime=System.currentTimeMillis();
			anRegValues=new short[nQuantity];
			abCoilValues=new boolean[0];
			nError=m_Modbus.ReadOutputRegisters(nDevice,  (short)nAddress , nQuantity , anRegValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_INPUT_REGISTERS:
			lInitialTime=System.currentTimeMillis();
			anRegValues=new short[nQuantity];
			abCoilValues=new boolean[0];
			nError=m_Modbus.ReadInputRegisters(nDevice,  (short)nAddress , nQuantity , anRegValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_OUTPUT_STATUS:
			lInitialTime=System.currentTimeMillis();
			abCoilValues=new boolean[nQuantity];
            anRegValues=new short[0];;
			nError=m_Modbus.ReadOutputStatus(nDevice,  (short)nAddress , nQuantity , abCoilValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_INPUT_STATUS:
			lInitialTime=System.currentTimeMillis();
			abCoilValues=new boolean[nQuantity];
            anRegValues=new short[0];
			nError=m_Modbus.ReadInputStatus(nDevice,  (short)nAddress , nQuantity , abCoilValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
			
			
		}

		m_lNumPolls++;
		if (Modbus.ERR_OK!=nError) {
			m_lNumErrors++;
		}

		if (anRegValues.length!=0) {
			
			iSelectedItem=m_dlgMonitor.lstValues.getSelectedIndex();
			
			for (i=0;i<anRegValues.length;i++){
				sValues="["+(nAddress+i)+"]="
						  + anRegValues[i]  //short 
					      + " " + (int)(char)anRegValues[i]  //unsigned short  
						  + " 0x"+Integer.toHexString((char)anRegValues[i]); //hex 
			
				cValue=(char)anRegValues[i];
				if (cValue!=0) {
					sValues+= " " + cValue;  //unsigned short 
				}
				m_dlgMonitor.lstValues.replaceItem(sValues,i);
				
			}

			if (iSelectedItem>=0) {
				m_dlgMonitor.lstValues.select(iSelectedItem);
			}
			
		
		
		}
	
		if (abCoilValues.length!=0) {
			
			iSelectedItem=m_dlgMonitor.lstValues.getSelectedIndex();

			for (i=0;i<abCoilValues.length;i++){
				sValues="["+(nAddress+i)+"]="+abCoilValues[i];
				m_dlgMonitor.lstValues.replaceItem(sValues,i);
			}

			if (iSelectedItem>=0) {
				m_dlgMonitor.lstValues.select(iSelectedItem);
			}
			
		
		}


		SetStatusStat(m_lNumPolls+" Errors="+m_lNumErrors + " "+lDuration + "(ms)");
	
	}

	private void ReadFuncion(int iFunction) {
		
		short nError=0;
		short nDevice;
		short nQuantity;
		int nAddress;
		short anRegValues[];
		boolean abCoilValues[];
		String sStat;
		String sValues="";
		String sText="";
		int i;
        long  lInitialTime;
		long  lDuration=0; 
		char cValue;
		

		nAddress=atoi(m_dlgMonitor.txtAddress.getText());
		nQuantity=(short)atoi(m_dlgMonitor.txtQuantity.getText());
        nDevice=(short)atoi(m_dlgMonitor.txtDevice.getText()); 
        anRegValues=new short[0];
		abCoilValues=new boolean[0];

	
		m_Modbus.ThrowCommException(false);

		switch (iFunction) {
				
		case LOOPBACK_TEST:
            lInitialTime=System.currentTimeMillis();
			try{
				nError=m_Modbus.LoopbackTest(nDevice);
			}
			catch (ModbusException mbExcetion) {
				nError=(short)mbExcetion.ErrorNumber();
				
			}
			lDuration=System.currentTimeMillis()-lInitialTime;
			anRegValues=new short[0];
			abCoilValues=new boolean[0];
            break;
		case READ_OUTPUT_REGISTERS:
			lInitialTime=System.currentTimeMillis();
			anRegValues=new short[nQuantity];
			abCoilValues=new boolean[0];
			nError=m_Modbus.ReadOutputRegisters(nDevice,  (short)nAddress , nQuantity , anRegValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_INPUT_REGISTERS:
			lInitialTime=System.currentTimeMillis();
			anRegValues=new short[nQuantity];
			abCoilValues=new boolean[0];
			nError=m_Modbus.ReadInputRegisters(nDevice,  (short)nAddress , nQuantity , anRegValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_OUTPUT_STATUS:
			lInitialTime=System.currentTimeMillis();
			abCoilValues=new boolean[nQuantity];
            anRegValues=new short[0];;
			nError=m_Modbus.ReadOutputStatus(nDevice,  (short)nAddress , nQuantity , abCoilValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
		case READ_INPUT_STATUS:
			lInitialTime=System.currentTimeMillis();
			abCoilValues=new boolean[nQuantity];
            anRegValues=new short[0];
			nError=m_Modbus.ReadInputStatus(nDevice,  (short)nAddress , nQuantity , abCoilValues);
			lDuration=System.currentTimeMillis()-lInitialTime;
			break;
			
			
		}

		m_lNumPolls++;
		if (Modbus.ERR_OK!=nError) {
			m_lNumErrors++;
		}

		if (anRegValues.length!=0) {
			
			for (i=0;i<anRegValues.length;i++){
				sValues="["+(nAddress+i)+"]="
						  + anRegValues[i]  //short 
					      + " " + (int)(char)anRegValues[i]  //unsigned short  
						  + " 0x"+Integer.toHexString((char)anRegValues[i]); //hex 
			
				cValue=(char)anRegValues[i];
				if (cValue!=0) {
					sValues+= " " + cValue;  //unsigned short 
				}
				sText+="\n"+sValues;
				
			}

			
		
		
		}
	
		if (abCoilValues.length!=0) {
			
			for (i=0;i<abCoilValues.length;i++){
				sValues="["+(nAddress+i)+"]="+abCoilValues[i];
				sText+="\n"+sValues;
			}

					
		}

        
		m_dlgMonitor.txtValues. setText(sText);

		SetStatusStat(m_lNumPolls+" Errors="+m_lNumErrors + " "+lDuration + "(ms)");
	
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -