Browse Source

Modbus tcp

master
pry 1 year ago
parent
commit
07ce5d4b0a
12 changed files with 160 additions and 33 deletions
  1. +0
    -17
      .idea/deploymentTargetDropDown.xml
  2. +2
    -0
      .idea/gradle.xml
  3. +3
    -4
      app/build.gradle
  4. BIN
     
  5. +3
    -0
      app/src/main/AndroidManifest.xml
  6. +2
    -2
      app/src/main/java/com/example/bpa/app/Main.java
  7. +73
    -0
      app/src/main/java/com/example/bpa/app/ModbusTcpHelper.java
  8. +3
    -3
      app/src/main/java/com/example/bpa/app/PLC.java
  9. +3
    -1
      app/src/main/java/com/example/bpa/helper/SdCart.java
  10. +53
    -1
      app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java
  11. +17
    -5
      app/src/main/res/layout/from/layout/fragment_system_set.xml
  12. +1
    -0
      build.gradle

+ 0
- 17
.idea/deploymentTargetDropDown.xml View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="127.0.0.1:7555" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-04-11T08:06:36.147915300Z" />
</component>
</project>

+ 2
- 0
.idea/gradle.xml View File

@@ -7,6 +7,8 @@
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="D:/gradle-8.0.2-all/gradle-8.0.2" />
<option name="gradleJvm" value="Android Studio default JDK" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />


+ 3
- 4
app/build.gradle View File

@@ -42,14 +42,12 @@ android {

}
}

}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
@@ -60,4 +58,5 @@ dependencies {
implementation files('libs\\sdkapi.jar')
compileOnly files('libs/sdkapi.jar')
implementation files('libs/modbus4J.jar')
implementation files('libs/modbus4Android-1.2.jar')
}

BIN
View File


+ 3
- 0
app/src/main/AndroidManifest.xml View File

@@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


<application
android:name=".app.ICSApp"
@@ -15,6 +17,7 @@
android:requestLegacyExternalStorage="true"
android:supportsRtl="false"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true"
tools:targetApi="31">
<activity
android:name=".view.from.add_pf_activity"


+ 2
- 2
app/src/main/java/com/example/bpa/app/Main.java View File

@@ -35,9 +35,9 @@ public class Main {
//1.加载配置
LoadingPZ();
//2.初始化PLC
PLC.getInstance().InitMobus();
// PLC.getInstance().InitMobus();
//3.初始化业务
//4.初始化日志上报
//4.初始化日志上报adad
}




+ 73
- 0
app/src/main/java/com/example/bpa/app/ModbusTcpHelper.java View File

@@ -0,0 +1,73 @@
package com.example.bpa.app;

import android.util.Log;

import com.example.bpa.modbus4j.ModbusParam;
import com.example.bpa.modbus4j.ModbusReq;
import com.example.bpa.modbus4j.OnRequestBack;

import com.zgkxzx.modbus4And.ModbusFactory;
import com.zgkxzx.modbus4And.ModbusMaster;
//import com.zgkxzx.modbus4And.requset.ModbusParam;
//import com.zgkxzx.modbus4And.requset.ModbusReq;
//import com.zgkxzx.modbus4And.requset.OnRequestBack;

//import com.serotonin.modbus4j.ModbusFactory;
//import com.serotonin.modbus4j.ModbusMaster;
//import com.serotonin.modbus4j.ip.IpParameters;

public class ModbusTcpHelper {
private static volatile ModbusTcpHelper mInstance;

// public synchronized void release() {
// super.release();
// minstance = null;
// }

public static ModbusTcpHelper getInstance() {
if (mInstance == null)
mInstance = new ModbusTcpHelper();
return mInstance;
}

private ModbusMaster master = null;
private ModbusFactory factory = new ModbusFactory();

public boolean Connect(String ip, int port) throws Exception {
// IpParameters params = new IpParameters();
// params.setPort(port);
// params.setHost(ip);
// params.setEncapsulated(false);
// master = factory.createTcpMaster(params, true);
try {
// master.setTimeout(1000);//超时时间
// master.setRetries(0);//重连次数
// master.init();

ModbusReq.getInstance().setParam(new ModbusParam()
.setHost("192.168.1.14")
.setPort(502)
.setEncapsulated(false)
.setKeepAlive(true)
.setTimeout(2000)
.setRetries(0))
.init(new OnRequestBack<String>() {
@Override
public void onSuccess(String s) {
Log.i("TAG", "onSuccess " + s);
}

@Override
public void onFailed(String msg) {
Log.i("TAG", "onFailed " + msg);
}
});


} catch (Exception e) {
Log.i("错误信息", e.getMessage());
return false;
}
return true;
}
}

+ 3
- 3
app/src/main/java/com/example/bpa/app/PLC.java View File

@@ -20,7 +20,7 @@ public class PLC {
/**
* Ip地址
*/
public String Host = "192.168.0.104";
public String Host = "192.168.1.14";
/**
* 端口
*/
@@ -51,7 +51,7 @@ public class PLC {
* 初始化Modbus
*/
public void InitMobus() {
/**
ModbusReq.getInstance().setParam(new ModbusParam()
.setHost(Host)//TCP PLC_IP地址
.setPort(Post)//端口默认502
@@ -70,7 +70,7 @@ public class PLC {
T.show(ConfigName.getInstance().dishesCon, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":PLC连接失败!" + msg);
}
});
**/
}

/**


+ 3
- 1
app/src/main/java/com/example/bpa/helper/SdCart.java View File

@@ -27,6 +27,8 @@ public class SdCart {
}
return mInstance;
}


//endregion
/**
* 初始化数据库到SD卡
@@ -44,7 +46,7 @@ public class SdCart {
ConfigName.getInstance().dbPath = ConfigName.getInstance().appResRoot + "/hbl.db";
File rootFile = new File(ConfigName.getInstance().appResRoot);
if (!rootFile.exists()) //创建目录
{
{ Log.i("日志",ConfigName.getInstance().sdCardPath);
rootFile.mkdirs();
}
File file = new File(ConfigName.getInstance().dbPath);


+ 53
- 1
app/src/main/java/com/example/bpa/view/fragment/SystemSetFragment.java View File

@@ -6,20 +6,72 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.bpa.R;
import com.example.bpa.app.ModbusTcpHelper;
import com.example.bpa.app.PLC;

/**
* 系统设置界面
*/
public class SystemSetFragment extends Fragment {

private Button ConBut;
private Button ReadBut;
private Button WriteBut;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_system_set, container,false);
View view = inflater.inflate(R.layout.fragment_system_set, container, false);
Init(view);
return view;
}

private void Init(View tempView) {
ConBut = tempView.findViewById(R.id.ConnectDevice);
ReadBut = tempView.findViewById(R.id.ReadData);
WriteBut = tempView.findViewById(R.id.WriteData);

ConBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ModbusTcpHelper.getInstance().Connect("192.168.1.14", 502);
// PLC.getInstance().InitMobus();
} catch (Exception e) {
Log.i("错误日志", e.toString());
}

}
}).start();
Log.i("日志", "开始连接设备");
}
});

ReadBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PLC.getInstance().ReadPlcData("400001");
}
});

WriteBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PLC.getInstance().WritePlcData("400001", 10);
}
});

}


}

+ 17
- 5
app/src/main/res/layout/from/layout/fragment_system_set.xml View File

@@ -6,11 +6,23 @@
android:layout_height="match_parent"
android:tag="系统设置"
tools:context=".view.fragment.SystemSetFragment">
<TextView

<Button
android:id="@+id/ConnectDevice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统设置"
android:textSize="50dp"
android:textColor="@color/color_purl">
</TextView>
android:text="连接设备"/>

<Button
android:id="@+id/ReadData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据"/>

<Button
android:id="@+id/WriteData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入数据"/>

</LinearLayout>

+ 1
- 0
build.gradle View File

@@ -4,3 +4,4 @@ plugins {
id 'com.android.application' version '7.0.0' apply false
id 'com.android.library' version '7.0.0' apply false
}


Loading…
Cancel
Save