📄 modbusmonitor.java
字号:
//******************************************************************************
// ModbusMonitor.java: Applet
//
//******************************************************************************
package FreeModbus.AppletTest;
import java.applet.*;
import java.awt.*;
import FreeModbus.Util.*; //Modbus Class
import FreeModbus.AppletTest.ModbusMonitorDlg;
//==============================================================================
// Main Class for applet ModbusMonitor
//
//==============================================================================
public class ModbusMonitor extends Applet implements Runnable
{
long m_lNumErrors = 0;
private long m_lNumPolls = 0;
private boolean m_bPollActive = false;
// THREAD SUPPORT:
// m_ModbusMonitor is the Thread object for the applet
//--------------------------------------------------------------------------
Thread m_ModbusMonitor = null;
private Modbus m_Modbus; //modbus Object
private ModbusMonitorDlg m_dlgMonitor; //dialog Window
//constants
private final static int FORCE_SINGLE_COIL=1;
private final static int PRESET_SINGLE_REGISTER=0;
//Static
private final static int LOOPBACK_TEST=0;
private final static int READ_OUTPUT_REGISTERS=1;
private final static int READ_INPUT_REGISTERS=2;
private final static int READ_OUTPUT_STATUS=3;
private final static int READ_INPUT_STATUS=4;
// ModbusMonitor Class Constructor
//--------------------------------------------------------------------------
public ModbusMonitor()
{
// TODO: Add constructor code here
}
// APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
//--------------------------------------------------------------------------
public String getAppletInfo()
{
return "Name: ModbusMonitor\r\n" +
"Author: Ricardo Saat \r\n" +
"Created with Microsoft Visual J++ Version 1.0";
}
// The init() method is called by the AWT when an applet is first loaded or
// reloaded. Override this method to perform whatever initialization your
// applet needs, such as initializing data structures, loading images or
// fonts, creating frame windows, setting the layout manager, or adding UI
// components.
//--------------------------------------------------------------------------
public void init()
{
// If you use a ResourceWizard-generated "control creator" class to
// arrange controls in your applet, you may want to call its
// CreateControls() method from within this method. Remove the following
// call to resize() before adding the call to CreateControls();
// CreateControls() does its own resizing.
//----------------------------------------------------------------------
//resize(500, 300);
//create control dialog
m_dlgMonitor= new ModbusMonitorDlg (this);
m_dlgMonitor.CreateControls();
//Modbus Initialization
m_Modbus=new Modbus();
m_Modbus.ReConnectOnEveryMessage(true);
// TODO: Place additional initialization code here
Initializecontrols();
m_bPollActive=false;
//Connect();
}
// Place additional applet clean up code here. destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
public void destroy()
{
// TODO: Place applet cleanup code here
}
// ModbusMonitor Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
// TODO: Place applet paint code here
//g.drawString("Running: " + Math.random(), 10, 20);
}
// The start() method is called when the page containing the applet
// first appears on the screen. The AppletWizard's initial implementation
// of this method starts execution of the applet's thread.
//--------------------------------------------------------------------------
public void start()
{
if (m_ModbusMonitor == null)
{
m_ModbusMonitor = new Thread(this);
m_ModbusMonitor.start();
}
// TODO: Place additional applet start code here
}
// The stop() method is called when the page containing the applet is
// no longer on the screen. The AppletWizard's initial implementation of
// this method stops execution of the applet's thread.
//--------------------------------------------------------------------------
public void stop()
{
if (m_ModbusMonitor != null)
{
m_ModbusMonitor.stop();
m_ModbusMonitor = null;
}
// TODO: Place additional applet stop code here
//Stop Modbus
if (m_Modbus.IsConnected()){
m_Modbus.closeConnection();
}
//m_Modbus=null;
}
// THREAD SUPPORT
// The run() method is called when the applet's thread is started. If
// your applet performs any ongoing activities without waiting for user
// input, the code for implementing that behavior typically goes here. For
// example, for an applet that performs animation, the run() method controls
// the display of images.
//--------------------------------------------------------------------------
public void run()
{
int iDelay;
while (true)
{
try
{
//repaint();
//SetStatusCom(" " + System.currentTimeMillis());
// TODO: Add additional thread-specific code here
if (m_bPollActive) {
iDelay=atoi(m_dlgMonitor.txtDelay.getText());
if ((iDelay>0)&&(iDelay<=5000)) {
Thread.sleep(iDelay);
}
else {
Thread.sleep(1000);
}
ReadFuncion(m_dlgMonitor.cboReadFunction.getSelectedIndex());
}
else {
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
// TODO: Place exception-handling code here in case an
// InterruptedException is thrown by Thread.sleep(),
// meaning that another thread has interrupted this one
stop();
}
}
}
// TODO: Place additional applet code here
private void Initializecontrols()
{
//m_dlgMonitor.txtValues.disable();
//initialize combo read
m_dlgMonitor.cboReadFunction.addItem("LoopBackTest");
m_dlgMonitor.cboReadFunction.addItem("Read Output Registers");
m_dlgMonitor.cboReadFunction.addItem("Read Input Registers");
m_dlgMonitor.cboReadFunction.addItem("Read Output Status");
m_dlgMonitor.cboReadFunction.addItem("Read Input Status");
m_dlgMonitor.cboReadFunction.select(1);
//initialize combo write
m_dlgMonitor.cboWriteFunction.addItem("Preset Single Register");
m_dlgMonitor.cboWriteFunction.addItem("Force Single Coil");
m_dlgMonitor.txtServerAddr.setText(getCodeBase().getHost());
m_Modbus.Host(getCodeBase().getHost());
//m_dlgMonitor.txtServerAddr.disable();
m_dlgMonitor.txtPort.setText("502");
m_Modbus.Port(502);
m_dlgMonitor.txtDevice.setText("1");
m_dlgMonitor.txtValue.setText("0");
m_dlgMonitor.txtWriteAddr.setText("0");
m_dlgMonitor.txtQuantity.setText("10");
m_dlgMonitor.txtAddress.setText("0");
m_dlgMonitor.txtDelay.setText("1000");
m_dlgMonitor.cmdDisconnect.disable();
//old Version Controls
//m_dlgMonitor.cmdDisconnect.hide();
//m_dlgMonitor.cmdConnect.hide();
m_dlgMonitor.lstValues.hide();
EnableCancelPollButtons(false);
}
private void SetStatusCom(String sText) {
m_dlgMonitor.lblComErrors.setText(sText);
}
private void SetStatusStat(String sText) {
m_dlgMonitor.lblStat.setText(sText);
}
public boolean action (Event evt,Object obj){
boolean bEvent;
bEvent=false;
if (evt.target.equals(m_dlgMonitor.cmdConnect)) {
//connect
Connect();
}
else if (evt.target.equals(m_dlgMonitor.cmdDisconnect)) {
//Stop Modbus
if (m_Modbus.IsConnected()){
m_Modbus.closeConnection();
}
StopPoll();
m_dlgMonitor.cmdDisconnect.disable();
m_dlgMonitor.cmdConnect.enable();
}
else if (evt.target.equals(m_dlgMonitor.cmdStart)) {
StartPoll();
}
else if (evt.target.equals(m_dlgMonitor.cmdCancel)) {
StopPoll();
}
else if (evt.target.equals(m_dlgMonitor.cmdWrite)) {
WriteFunction();
}
else {
return (super.action(evt,obj));
}
return (true);
}
private void Connect() {
String sServerAddr;
int iPort=0;
sServerAddr=m_dlgMonitor.txtServerAddr.getText(); //getCodeBase().getHost();
try {
iPort=(Integer.valueOf(m_dlgMonitor.txtPort.getText())).intValue();
}
catch (NumberFormatException e){
SetStatusCom("Invalid Port Number");
}
if (!m_Modbus.openConnection(sServerAddr,iPort,5000)){
SetStatusCom("openConnection Failed");
}
else {
SetStatusCom("");
m_dlgMonitor.cmdDisconnect.enable();
m_dlgMonitor.cmdConnect.disable();
}
}
//process write event
private void WriteFunction() {
short nAddress;
short nValue;
short nError=0;
short nDevice;
// if (!m_Modbus.IsConnected()){
// SetStatusCom("Not Connected");
// return;
// }
nAddress=(short)atoi(m_dlgMonitor.txtWriteAddr.getText());
nValue=(short)atoi(m_dlgMonitor.txtValue.getText());
nDevice=(short)atoi(m_dlgMonitor.txtDevice.getText());
switch (m_dlgMonitor.cboWriteFunction.getSelectedIndex()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -