22 Replies Latest reply on May 13, 2016 1:02 PM by echo RSS
    hshintani Newbie

    Connect Thingworx and PLC

    Hi,

     

    I'm trying to connect PLC and Thingworx via EMS.

    In modbusExample.lua file, defining property for Thing is essential.

    I put this property like this,

    properties.Int16HoldRegExample = {value="300", dataChangeType="ALWAYS",pushType="ALWAYS", key ="holding_register/1/40001", handler="modbus_handler", basetype="NUMBER"}

    But I can't connect.

    Which URL does this "key" table mean?   PLC ,EMS or Thingworx Composer ?

     

    please let me know information...

      • Re: Connect Thingworx and PLC
        pchung Collaborator

        The Modbus example is specifically for connecting using Modbus protocol.

        You have a modbus protocol setup to your PLC?

        the Key refers to the Modbus register that is to be read.

          • Re: Connect Thingworx and PLC
            hshintani Newbie

            I use Modbus package and set up modbus protocol to my PLC.

            Is it ok that only modbus register is included in key contents,for example key = "40001" ?

              • Re: Connect Thingworx and PLC
                pchung Collaborator

                Unfortunately I'm not too familiar with how the modbus.lua is setup and how it processes the key that is put in.

                Hopefully someone else can give some input on that.

                • Re: Connect Thingworx and PLC
                  tcoufal Communicator

                  First of all you need to make sure that your Modbus communication is working properly.

                  That deppends on type of connecten. Are you using Modbus over serial - i.e. RTU/ASCII over RS485? or Modbus over TCP??

                  You can try program like Modbus Poll to verify your settings and readings. That's what I am using. (it's only a 10minutes demo though).

                  Then you need to change your config.lua file accordingly. Regarding reading the registers, I think that you need to define a type as well.

                  This is what I am using (just an example):

                   

                  properties.VoltageVan = {key="input_register/1/30002?format=Float", handler="modbus_handler1", basetype="NUMBER"}

                   

                  Its always good idea to use some network protocol analyzer (Wireshark is the best no doubt) when dealing with low-layer communication.

                  --> Tomas

                    • Re: Connect Thingworx and PLC
                      hshintani Newbie

                      Thanks for your information.

                      I'm using Modbus over TCP protocol. And I could read holding register data from  PLC with holding_register function in modbus_handler.lua.

                      But,I can't write data from ThingWorx to PLC via EMS.The cause is timeout like this:

                      [ERROR] 2016-02-01 17:17:39,410 modbus:executeFunction: Error: Timed out waiting for result.

                      [ERROR] 2016-02-01 17:17:39,410 luaInterscriptBinding::l_executeCallback: Error executing callback 'modbus_handler//scripts/modbus_handler/holding_register/1/40001'

                      Returned: 500 Timeout

                      [WARN ] 2016-02-01 17:17:39,411 MyPLC: Could not write property: Int16HoldRegExample, code: 500, msg: Timeout

                      Does the holding_register function include not only reading holging_register value but writing the same holding_register value?

                      Or should I set something else for writing property ? If you know,please teach me...

                        • Re: Connect Thingworx and PLC
                          pchung Collaborator

                          Holding Registers should be able to be read and written to.

                          What function are you using for the write?

                          • Re: Connect Thingworx and PLC
                            tcoufal Communicator

                            I would be interested in that function as well.

                             

                            What kind of PLC system are you using? Systems which I am familiar with needs a proper configuration in a project in the first place to allow MODBUS registers (coils and holding registers) to be written into. It is a safety issue. Use Wireshark to capture Modbus packets sent and recieved from/by your lua script. That could give you some clue.   

                            • Re: Connect Thingworx and PLC
                              aanjan Heavyweight Champ

                              Can you post the property string you're using in your modbus template.lua file?

                                • Re: Connect Thingworx and PLC
                                  hshintani Newbie

                                  This line is for setting property.

                                  properties.Int16HoldRegExample = {value="300", dataChangeType="ALWAYS",pushType="ALWAYS", key ="holding_register/1/40001?format=Int16", handler="modbus_handler", basetype="NUMBER"}

                                   

                                  And,these line are for setting services to get property value.

                                  services.getAllProperties = function(me, headers, query, data)

                                    log.debug(me.name, "Called getProperties")

                                    local postdata = {content='{"slave": 1 ,"address": 40001 , "count": 1, "data": "string"}'}

                                    return tw_script.executeCallback("modbus_handler", "POST", "holding_register", headers, query, postdata)

                                    • Re: Connect Thingworx and PLC
                                      jcanosa Explorer

                                      You can try adjusting the timeout value in config.lua

                                      You can do so in the section

                                      scripts.modbus_handler = {

                                          file = "modbus_handler.lua" ,

                                          response_timeout = 10000,

                                      }

                                      Not that the number is in milliseconds.  The default is 5000.

                                       

                                      You can also debug your connection by talking directly to the modbus_handler script itself.  On the machine that your EMS is running, point a browser with your favorite REST debugging tool, like Postman, to http://127.0.0.1:<port>/scripts/modbus_handler/holding_register/<slave_id>/<register>

                                      where port is the port your Lua Script resource is listening on - defaults to 8001, slave_id would be 1, and holding_holding register would be 4001 in your case.

                                       

                                      For writing you should be should be doing a PUT to /holding_register/<slave_id>/<register>

                                       

                                      Take a look at the documentation in /etc/thingworx/scripts/modbus_handler.lua for some more details on how to interact directly with that script.

                                        • Re: Connect Thingworx and PLC
                                          pkeckes Explorer

                                          Hi John, how exactly work this service.getAllProperties in modbusExample.lua? I can't see this service in remote thing for binding (how I can call it?). Also I having issue to read coils and discrete inputs - as bit values and using workaround with Uint16 word value - It's working, but it's not right. Could you please advice? Thanks.

                                            • Re: Connect Thingworx and PLC
                                              jcanosa Explorer

                                              Peter - In looking at the modbusExample.lua file that you posted (I assume this is the one you are referring to), I don't see a service definition in that template.  That is why you don't see it when you browse from Composer.  If I remember right read_multiple_registers returns a string with a comma separated list of formatted values.  You can double check this by directly POSTing to the modbus_handler script to see what comes back.

                                               

                                              A coil property definition should look like the following:

                                              properties.CoilTest1 = {key="coil/<slave_id>/<coil_number>", handler="modbus_handler", basetype="BOOLEAN"}

                                              One thing to be careful of is offset in modbus addressing.  Depending on your modbus slave the first coil might be coil #1 at address 0 which is typical or they might call it by its address (i.e. coil 1 is at address 1 and coil 0 is at address 0).

                              • Re: Connect Thingworx and PLC
                                echo Explorer

                                Hi,

                                 

                                I also facing problem on connecting IDEC to ThingWorx.

                                below is the result after I run ./luascriptresources

                                 

                                [ERROR] 2016-04-09 11:34:20,113 luaScriptProxy::execute: [1094014096] Error executing script modbus_handler, Error = Error: ...t/thingworx/MicroServer/etc/thingworx/lua/modbus.lua:104: module 'socket' not found:

                                        no field package.preload['socket']

                                        no file '/opt/thingworx/MicroServer/etc/socket.lua'

                                        no file '/opt/thingworx/MicroServer/etc/thingworx/socket.lua'

                                        no file '/opt/thingworx/MicroServer/etc/thingworx/lua/socket.lua'

                                        no file '/opt/thingworx/MicroServer/etc/custom/socket.lua'

                                        no file '/opt/thingworx/MicroServer/etc/custom/lua/socket.lua'

                                        no file '/opt/thingworx/MicroServer/etc/community/lua/socket.lua'

                                        no file './socket.lua'

                                        no file '/usr/local/share/lua/5.1/socket.lua'

                                        no file '/usr/local/share/lua/5.1/socket/init.lua'

                                        no file '/usr/local/lib/lua/5.1/socket.lua'

                                        no file '/usr/local/lib/lua/5.1/socket/init.lua'

                                        no file '/opt/thingworx/MicroServer/etc/thingworx/clibs/socket.so'

                                        no file '/opt/thingworx/MicroServer/etc/custom/clibs/socket.so'

                                        no file '/opt/thingworx/MicroServer/etc/community/clibs/socket.so'

                                        no file './socket.so'

                                        no file '/usr/local/lib/lua/5.1/socket.so'

                                        no file '/usr/local/lib/lua/5.1/loadall.so'

                                [ERROR] 2016-04-09 11:34:20,118 luaScriptProxy::app_unlock: [1094014096] Attempt to unlock app_mutex while not holding it.L= 0x1141f0

                                 

                                hope someone can help.

                                 

                                thanks in advance

                                  • Re: Connect Thingworx and PLC
                                    aanjan Heavyweight Champ

                                    Eric, what version of the MicroServer are you using? Do you have the Modbus package with it as well?

                                    • Re: Connect Thingworx and PLC
                                      pkeckes Explorer

                                      Hi Eric, looks like you using latest 5.1 version - you need Modbus complete Modbus package - is missing - If you would like to download my package and search or socket.lua (and other files if needed) and add them to your microserver / or reuse my package with your config files (config.lua and config.json).

                                        • Re: Connect Thingworx and PLC
                                          pkeckes Explorer

                                          Have in mind - I'm using version 5.3.1.1113 with some changes to get it work

                                            • Re: Connect Thingworx and PLC
                                              echo Explorer

                                              Thanks everyone.

                                               

                                              Question to Peter, may I know where to get the complete Modbus package?

                                               

                                              Can I use this package?

                                               

                                              Content

                                                • Re: Connect Thingworx and PLC
                                                  pkeckes Explorer

                                                  Click on my name and that will lead you to my profile page - there is attached zip file Microserver 5.3.1 Modbus example. The package you pointing is TWEMS is the example package I originally used, but it need a bit of configuration to get it work. There are also some issues with coils and discrete inputs as it was designed for TWEMS and not current WSEMS Microserver. I have some workaround in my Modbus example package. Compare them and use them as source examples for your project.

                                                    • Re: Connect Thingworx and PLC
                                                      echo Explorer

                                                      Thank you Peter,

                                                       

                                                      I had re-download the EMS with Modbus package to my device, and got this error.

                                                       

                                                      root@SL3000-1191D6:/opt/thingworx/MicroServer# ./luaScriptResource
                                                      [INFO ] 2016-04-08 12:52:59,244 ./luaScriptResource: Using config file: /opt/thingworx/MicroServer/etc/config.lua
                                                      [FORCE] 2016-04-08 12:52:59,253 ./luaScriptResource: Starting up ....

                                                      [TRACE] 2016-04-08 12:52:59,282 luaConfigurator::doGetItem: Global property or script log_level not found
                                                      [TRACE] 2016-04-08 12:52:59,284 luaConfigurator::doGetItem: Global property or script rap_host not found
                                                      [TRACE] 2016-04-08 12:52:59,287 luaConfigurator::doGetItem: Global property or script rap_host not found
                                                      [TRACE] 2016-04-08 12:52:59,291 luaConfigurator::doGetItem: Global property or script rap_host not found
                                                      [TRACE] 2016-04-08 12:52:59,293 luaConfigurator::doGetItem: Global property or script rap_host not found
                                                      [TRACE] 2016-04-08 12:52:59,294 luaConfigurator::doGetItem: Global property or script rap_port not found
                                                      [TRACE] 2016-04-08 12:52:59,295 luaConfigurator::doGetItem: Global property or script rap_port not found
                                                      [TRACE] 2016-04-08 12:52:59,296 luaConfigurator::doGetItem: Global property or script rap_port not found
                                                      [TRACE] 2016-04-08 12:52:59,297 luaConfigurator::doGetItem: Global property or script rap_port not found
                                                      [TRACE] 2016-04-08 12:52:59,299 luaConfigurator::doGetItem: Global property or script rap_ssl not found
                                                      [TRACE] 2016-04-08 12:52:59,299 luaConfigurator::doGetItem: Global property or script rap_ssl not found
                                                      ....

                                                      [DEBUG] 2016-04-08 12:52:59,375 ./luaScriptResource: Appending /opt/thingworx/MicroServer/etc/thingworx/clibs;/opt/thingworx/MicroServer/etc/custom/clibs;/opt/thingworx/MicroServer/etc/community/clibs to current path /usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
                                                      [INFO ] 2016-04-08 12:52:59,376 ./luaScriptResource: New path is /opt/thingworx/MicroServer/etc/thingworx/clibs:/opt/thingworx/MicroServer/etc/custom/clibs:/opt/thingworx/MicroServer/etc/community/clibs:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
                                                      ....
                                                      [DEBUG] 2016-04-08 12:52:59,515 luaScriptProxy::initialize: Name = modbus_handler

                                                      File = /opt/thingworx/MicroServer/etc/thingworx/scripts/modbus_handler.lua

                                                      LibraryPath = /opt/thingworx/MicroServer/etc/?.lua;/opt/thingworx/MicroServer/etc/thingworx/?.lua;/opt/thingworx/MicroServer/etc/thingworx/lua/?.lua;/opt/thingworx/MicroServer/etc/custom/?.lua;/opt/thingworx/MicroServer/etc/custom/lua/?.lua;/opt/thingworx/MicroServer/etc/community/lua/?.lua
                                                      InitData Ptr = 0

                                                      Persisted Props:
                                                      file = modbus_handler.lua
                                                      host = 128.100.20.49
                                                      name = modbus_handler

                                                      GlobalProps:
                                                      config_file_path = /opt/thingworx/MicroServer/etc
                                                      keepAliveRate = 6000
                                                      log_level = TRACE
                                                      rap_host = localhost
                                                      rap_port = 8000
                                                      script_clib_path = /opt/thingworx/MicroServer/etc/thingworx/clibs/?.so;/opt/thingworx/MicroServer/etc/custom/clibs/?.so;/opt/thingworx/MicroServer/etc/community/clibs/?.so
                                                      script_directory = /opt/thingworx/MicroServer/etc/custom/scripts
                                                      script_library_path = /opt/thingworx/MicroServer/etc/?.lua;/opt/thingworx/MicroServer/etc/thingworx/?.lua;/opt/thingworx/MicroServer/etc/thingworx/lua/?.lua;/opt/thingworx/MicroServer/etc/custom/?.lua;/opt/thingworx/MicroServer/etc/custom/lua/?.lua;/opt/thingworx/MicroServer/etc/community/lua/?.lua
                                                      script_resource_host = 127.0.0.1
                                                      script_resource_port = 80

                                                      [DEBUG] 2016-04-08 12:52:59,607 luaScriptProxy::runScript: Starting Script
                                                      [TRACE] 2016-04-08 12:52:59,613 luaConfigurator::doGetItem: Global property or script modbus_handler not found

                                                       

                                                       

                                                      hope you can help. thank you