Modbus is a serial communications protocol originally published by Modicon (now Schneider Electric) in 1979 for use with its programmable logic controllers (PLCs). Simple and robust, it has since become a de facto standard communication protocol, and it is now a commonly available means of connecting industrial electronic devices.(From Wekipedia)
## <a name="address"></a> Address Mapping
Modbus has four types of address: Coil, Discrete Input, Input Register and Holding Register.
Modbus has two address description method: standard and extend.
The following table could show the full address discription in Modbus.
Type | Standard | Extend |
---------------- | ----------- | ------------- |
Coil | 00001-09999 | 000001-065536 |
Discrete Input | 10001-19999 | 100001-165536 |
Input Register | 30001-39999 | 300001-365536 |
Holding Register | 40001-49999 | 400001-465536 |
Standard and Extend address description are all supported in Modbus.Net.Modbus. The only difference is don't write too large number in address.
The following table shows how to write address in Modbus.Net.Modbus
Standard Modbus Address | Modbus.Net.Modbus String Address |
@@ -129,26 +143,19 @@ More important, you can extend and implement your own field in UnitExtend in eve
3. Add a machine to TaskManager.
3. Add a machine to TaskManager.
Add a machine like siemens machine to the task manager.
Add a machine like siemens machine to the task manager.
4. Implement ReturnValues event.
4. Add a TaskItem for one machine or all Machines.
The argument return values is a key value pair. The architechture is:
Modbus.Net implement TaskItemGetDatas and TaskItemSetDatas as the default.
* Key : the link address of machine (in sample is the second parameter).
* Value : Dictionary.
* Key : CommunicationTag/Address(string) in AddressUnit.
* Value : ReturnUnit.
* PlcValue : The return data, all in double type.
* UnitExtend : UnitExtend in AddressUnit. You should cast this class to your own class extends by UnitExtend.
##<a name="tutorial"></a> Tutorial
##<a name="tutorial"></a> Tutorial
This platform has three level APIs that you could use: Low level API called "BaseUtility"; Middle level API called "BaseMachine"; High level API called "TaskManager".
This platform has three level APIs that you could use: Low level API called "BaseUtility"; Middle level API called "BaseMachine"; High level API called "TaskManager".
###BaseUtility
###Utility
BaseUtility is a low level api, in this level you can get or set data only by byte array or object array. Here is an example.
IUtilityProperty is a low level api, in this level you can get or set data only by byte array or object array. Here is an example.
```C#
```C#
string ip = "192.168.0.10";
string ip = "192.168.0.10";
BaseUtility utility = new ModbusUtility(ModbusType.Tcp, ip, 0x02, 0x00);
IUtilityProperty utility = new ModbusUtility(ModbusType.Tcp, ip, 0x02, 0x00);
object[] getNum = utility.GetDatas("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
object[] getNum = utility.InvokeUtilityMethod<IUtilityMethodData>?.GetDatas("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
```
```
BaseUtility is an abstract class. You can check all apis in BaseUtility.cs in Modbus.Net project.
BaseUtility is an abstract class. You can check all apis in BaseUtility.cs in Modbus.Net project.
@@ -157,24 +164,25 @@ To use BaseUtility, follow these steps.
1.New a BaseUtility instance, but remember BaseUtility is an abstract class, you should new class inherit from it.
1.New a BaseUtility instance, but remember BaseUtility is an abstract class, you should new class inherit from it.
```C#
```C#
BaseUtility utility = new ModbusUtility(ModbusType.Tcp, ip, 0x02, 0x00);
IUtilityPropety utility = new ModbusUtility(ModbusType.Tcp, ip, 0x02, 0x00);
```
```
2.Use GetData and SetData Api in BaseUtility, like
2.Use GetData and SetData Api in IUtilityMethodData, like
```C#
```C#
object[] getNum = utility.GetDatas("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
object[] getNum = utility.InvokeUtilityMethod<IUtilityMethodData>?.GetDatas("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
utility.SetDatas("4X 1", new object[] { (ushort)1, (ushort)2, (ushort)3 });
utility.InvokeUtilityMethod<IUtilityMethodData>?.SetDatas("4X 1", new object[] { (ushort)1, (ushort)2, (ushort)3 });
```
```
Remember force set type of numbers because GetData and SetData Apis are type sensitive.
Remember force set type of numbers because GetData and SetData Apis are type sensitive.
You can also use async functions like
You can also use async functions like
```C#
```C#
object[] getNum = await utility.GetDatasAsync("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
object[] getNum = await utility.InvokeUtilityMethod<IUtilityMethodData>?.GetDatasAsync("4X 1", new KeyValuePair<Type, int>(typeof(ushort), 4));
```
```
###BaseMachine
###Machine
BaseMachine is a middle level api, in this level you could get and set datas in a managable data structure for a single machine.
IMachineProperty is a middle level api, in this level you could get and set datas in a managable data structure for a single machine.
To understand this class, you have to see the AddressUnit first.
To understand this class, you have to see the AddressUnit first.
```C#
```C#
public class AddressUnit
public class AddressUnit
@@ -205,9 +213,9 @@ For some reasons, AddressUnit has two keys: Id and CommunicationTag, one is inte
* Unit : Unit of the Address. For example "¡æ".
* Unit : Unit of the Address. For example "¡æ".
* UnitExtend : If you want to get something else when value returns, extend the class and give it to here.
* UnitExtend : If you want to get something else when value returns, extend the class and give it to here.
Then using BaseMachine like this.
Then using IMachineProperty like this.
```C#
```C#
BaseMachine machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", new List<AddressUnit>()
IMachineProperty machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", new List<AddressUnit>()
{
{
machine = new ModbusMachine(ModbusType.Rtu, "COM3", new List<AddressUnit>()
machine = new ModbusMachine(ModbusType.Rtu, "COM3", new List<AddressUnit>()
{
{
@@ -219,18 +227,18 @@ machine = new ModbusMachine(ModbusType.Rtu, "COM3", new List<AddressUnit>()
machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator);
machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator);
machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator);
machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator);
machine.AddressCombiner = new AddressCombinerPercentageJump(20.0);
machine.AddressCombiner = new AddressCombinerPercentageJump(20.0);
var result = machine.GetDatas(MachineGetDataType.CommunicationTag);
var result = machine.InvokeMachineMethods<IMachineMethodData>?.GetDatas(MachineGetDataType.CommunicationTag);
var add1 = result["Add1"].PlcValue;
var add1 = result["Add1"].PlcValue;
var resultFormat = result.MapGetValuesToSetValues();
var resultFormat = result.MapGetValuesToSetValues();
machine.SetDatas has two types. It is referenced as the first parameter.
machine.SetDatas has four types. It is referenced as the first parameter.
1. MachineSetDataType.Address: the key of the dictionary of the second parameter is address.
1. MachineSetDataType.Address: the key of the dictionary of the second parameter is address.
2. MachineSetDataType.CommunicationTag: the key of the dictionary of the second parameter is communication tag.
2. MachineSetDataType.CommunicationTag: the key of the dictionary of the second parameter is communication tag.
3. MachineSetDataType.Id: the key of the dictionary of the second paramenter is ID.
4. MachineSetDataType.Name: the key of the dictionary of the second paramenter is name.
### TaskManager
### TaskManager
TaskManager is a high level api that you can manage and control many machines together. Remenber if you want to use this class, all communications must be asyncronized.
TaskManager is a high level api that you can manage and control many machines together. Remenber if you want to use this class, all communications must be asyncronized.
@@ -294,20 +304,33 @@ List<AddressUnit> addressUnits = new List<AddressUnit>
And don't remember set default AddressTranslator, slaveAddress, masterAddress and Protocal.
And don't remember set default AddressTranslator, slaveAddress, masterAddress and Protocal.
```C#
```C#
@@ -509,8 +537,8 @@ public class AddressFormaterModbus : AddressFormater
## <a name="addition"></a> Addition
## <a name="addition"></a> Addition
### For Subpos System
### For Subaddress System
Subpos system is implemented for reading and writing of bits.
Subaddress system is implemented for reading and writing of bits.
```C#
```C#
public class AddressUnit
public class AddressUnit
{
{
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.