Modbus TCP Function Code 01 (Boolean)

120 views
Skip to first unread message

simon ong sk

unread,
Sep 28, 2023, 1:37:35 AM9/28/23
to libplctag
Hello:
          How to setup libplctag C# with Modbus TCP not on PLC but modbus devices that provide Digital Input and Output?

Any Example ?

Regards

Simon

Kyle

unread,
Sep 28, 2023, 12:09:17 PM9/28/23
to libplctag
Hi Simon,

Modbus devices are the same whether they are a sensor, an actuator, a PLC or something else.   As long as they have an IP address and you know the port that they use, you can connect to them.

Best,
Kyle

simon ong sk

unread,
Sep 28, 2023, 6:49:24 PM9/28/23
to libplctag
Hi Kyle:
                  Thank you for quick resposne. I tried before posting here. Here is my code:

           Tag<BoolPlcMapper, bool[]> modbusDevice;   // Declaration some where
            modbusDevice = new Tag<BoolPlcMapper, bool[]>();
            modbusDevice.Gateway = "192.168.127.254";    //Device IP
            modbusDevice.Path = "502"; // Famous Modbus Port
            modbusDevice.Name = "10"; //   ----------------------------> what to fill ?
            modbusDevice.PlcType = ""; ---------------> since it is not a PLC, what to fill
            modbusDevice.Protocol = Protocol.modbus_tcp;
            modbusDevice.Timeout = TimeSpan.FromSeconds(10);
            modbusDevice.ArrayDimensions = new int[] { 10 };
            modbusDevice.Aborted += ModbusDevice_Aborted;
            modbusDevice.ReadCompleted += ModbusDevice_ReadCompleted;

I know it is incorrect  , need some advice....

Thanks

Simon

Kyle

unread,
Sep 28, 2023, 10:45:25 PM9/28/23
to libplctag
The "Name" property should be set as follows in the wiki.  I.e. co42 is coil 42, di95 for discrete (boolean) input 95 etc.

Best,
Kyle

simon ong sk

unread,
Sep 29, 2023, 8:02:54 AM9/29/23
to libplctag
Hi Kyle:
            modbusDevice = new Tag<DintPlcMapper, int[]>();
            modbusDevice.Gateway = "192.168.127.254:502";
            modbusDevice.Path = "0"; //"18 for port 1 19 for port 2
            modbusDevice.Name = "ir50"; //Specify  30049
            //modbusDevice.PlcType = PlcType.Slc500;

            modbusDevice.Protocol = Protocol.modbus_tcp;
            modbusDevice.Timeout = TimeSpan.FromSeconds(10);
            modbusDevice.ArrayDimensions = new int[] {120};

            modbusDevice.Aborted += ModbusDevice_Aborted;
            modbusDevice.ReadCompleted += ModbusDevice_ReadCompleted;

         private  async void C_Click(object sender, RoutedEventArgs e)
        {
           await  modbusDevice.ReadAsync();  ///Start Read
 
        }

      private void ModbusDevice_ReadCompleted(object? sender, TagEventArgs e)
        {
           
            if (modbusDevice.Value!=null )
            {
                        Debug.WriteLine("Success");
                         
              }

        }

   I check all array return 0. whereas with modbus poll, Input Register 30049, value is 4 which is input bit 2 (start from 0); 
I try  modbusDevice.Name = "ir48" or ir49, nothing work 

 Is my setting correct?


Regards
simon

Kyle

unread,
Sep 29, 2023, 10:01:38 AM9/29/23
to libplctag
Without having access to your device, it is hard to tell if this is correct.

Questions/Comments:
  • Is that the correct unit/device ID?  Is it zero?
  • Does your device use the command type to determine the register class or does it need the full register number?   I.e. is is ir50 or ir30050?
  • Are you seeing any error returned?
  • Can you use the tag_rw2 tool to run the same query to see if it works?
  • You are using the DintPlcMapper class, but Modbus "native" registers are 16-bit not 32-bit. 
Best,
Kyle

simon ong sk

unread,
Sep 29, 2023, 11:38:54 AM9/29/23
to libplctag
Hi Kyle
           
  • Is that the correct unit/device ID?  Is it zero? yes.
  • Does your device use the command type to determine the register class or does it need the full register number?   I.e. is is ir50 or ir30050?
  • Are you seeing any error returned?  ----No error
  • Can you use the tag_rw2 tool to run the same query to see if it works?  --> where do I get this tool?
  • You are using the DintPlcMapper class, but Modbus "native" registers are 16-bit not 32-bit. Yes, I have tried with IntPLCMapper, but it has an error  "The type 'libplctag.DataTypes.IntPlcMapper' cannot be used as type parameter 'M' in the generic type or method 'Tag<M, T>'. There is no implicit reference conversion from 'libplctag.DataTypes.IntPlcMapper' to 'libplctag.DataTypes.IPlcMapper<int>'.

modbus.jpg

Regards

Simon

Kyle

unread,
Sep 29, 2023, 6:19:50 PM9/29/23
to libplctag
You can get tag_rw2 and other tools and examples from the core C library package.  Download the one that matches your OS and machine type.

That will make sure that the core library is doing the right thing here.   If it is, then the issue (if it is a bug) is in the C# wrapper or in your use of the wrapper code.   Using the tag_rw2 tool will help figure out where to dig deeper to solve the problem!

Best,
Kyle

simon ong sk

unread,
Sep 30, 2023, 5:28:01 AM9/30/23
to libplctag
Hi:
    tag_rw2'exe is unable to start, how to launch this application  ?


regards

Simon

Kyle

unread,
Sep 30, 2023, 6:20:06 PM9/30/23
to libplctag
What OS are you using?  If it is Windows, you'll need to have the DLL (plctag.dll) in the same directory as tag_rw2.exe.   You can run it without any arguments to get an idea of what arguments to pass it.

You probably want something like:

C:\foo\blah\tag_rw2 --type=sint16 "--tag=protocol=modbus-tcp&gateway=1.2.3.4&path=0&elem_count=1&name=ir50"

You may need to add more parts to the tag string.   If you are using the usual Windows command line, you will need to wrap the tag argument in double quotes as shown above.  Change the IP address 1.2.3.4 to whatever IP your device has.

If this is returning an error, you can add an additional debug argument with debug level 4:

C:\foo\blah\tag_rw2 --type=sint16 "--tag=protocol=modbus-tcp&gateway=1.2.3.4&path=0&elem_count=1&name=ir50" --debug=4 > log.txt

And capture the output into a log file.   That can help us diagnose any problems with the configuration, response from the device or request from the library.

Best,
Kyle
Reply all
Reply to author
Forward
0 new messages