Author Topic: How to use modbus tcp  (Read 5748 times)

sunny

  • Newbie
  • *
  • Posts: 22
    • View Profile
How to use modbus tcp
« on: June 02, 2015, 04:36:34 PM »
Hi All

I like to learn about communication with modbus tcp. I have zero experiance with that. I have a Siemens S7-1200 plc ad home. I have red here on te forum that some of you did some work with modbus tcp and a S7-1200. My question is does someone has an example program or somthint to get me going. Because I don't have a clou where to start.
thanks in advance

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to use modbus tcp
« Reply #1 on: June 02, 2015, 05:43:05 PM »
This is a good place to start to get familiar with Modbus:

http://advancedhmi.com/forum/index.php?topic=765.0

I can't help much with setting up the S7-1200, but I do remember this being discussed here:

http://www.plctalk.net/qanda/showthread.php?t=93134&highlight=s7-1200+modbus+function

sunny

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to use modbus tcp
« Reply #2 on: June 14, 2015, 04:17:05 AM »
The modbus comunication is now working. I can read inputs (1xxxx). I can read / write outputs (0xxxx). I can read write from the holding registers (4xxxxx). But if I want to read or write single bits from the holding register it does not do enything. In the plc I use a array of bool. Maybe that is wrong I can't figure it out. Can someone help me??

ragards

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to use modbus tcp
« Reply #3 on: June 14, 2015, 05:18:31 AM »
Writing to individual bits in a holding register requires support for function code 22 (masked bit write) which very few devices support. You can either write the bit value (1,2,4,8,16, etc) by using a BasicButton and changing OutputType to WriteValue or by putting code behind to first read the value, set the bit, then write it back.

Dim CurrentValue as string = ModbusTCPCom1.Read("40001")
Dim CurrentValueInt as Integer = Cint(CurrentValue)
CurrentValueInt=(CurrentValuInt or 8)
ModbusTCPCom1.Write("40001", Cstr(CurrentValueInt))


The risk to this is the value can change in between the read and write, therefore the potential of corrupting the other bits.

sunny

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: How to use modbus tcp
« Reply #4 on: June 14, 2015, 04:00:06 PM »
Hi Archie

Thanks for the quick reply. I was trying to set the setpoint of the temp controler. So I gues that is only possible with that bit of code that you wrote.
If I want to set a bit in the holding register yousing a basicbutton that will write a value that is fine with me.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: How to use modbus tcp
« Reply #5 on: June 14, 2015, 04:23:16 PM »
If you are trying to use the buttons on the temperature controller for increment/decrement, you can do the same thing with the code I posted above. However, it is probably better to use input addresses for the buttons such as 00001 for PLCAddressClick3 and 00002 for PLCAddressClick4