Announcement

Collapse
No announcement yet.

ModbusTCP Client Simple Example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ModbusTCP Client Simple Example

    The information provided in the AutomationDirect knowledge base is provided "as is" without warranty of any kind. AutomationDirect disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. In no event shall AutomationDirect or its suppliers be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages, even if AutomationDirect or its suppliers have been advised of the possibility of such damages. Some states do not allow the exclusion of limitation of liability for consequential or incidental damages so the foregoing limitation may not apply.

    Purpose:Modbus TCP Client Simple for P1AM-100/P1AM-ETH

    This sketch toggles the coil of a Modbus TCP server connected
    on and off every second.

    This example uses Serial.print() to display status information.
    The Serial Monitor must be running for the sketch to start.


    Dependencies:
    https://github.com/arduino-libraries/ArduinoModbus
    https://github.com/arduino-libraries/ArduinoRS485

    Check out the code here:
    https://github.com/AutomationDirect/..._Client_Simple
    Last edited by ADC_CommTeam02; 03-02-2020, 10:59 AM.

  • #2

    I am getting the following error when I attempt to verify/compile this sketch:

    sketch_ModbusTCPclientSimpleExample:37:17: error: 'class ModbusTCPClient' has no member named 'setTimeout'

    modbusTCPClient.setTimeout(500);//Adjust Response Timeout from 30 seconds to 500 ms.

    Arduino: 1.8.11 (Windows 8.1), Board: "Arduino Uno"
    Using library SPI at version 1.0
    Using library Ethernet at version 2.0.0
    Using library ArduinoRS485 at version 1.0.0
    Using library ArduinoModbus at version 1.0.1
    exit status 1

    Comment


    • #3
      I was able to compile and download the example code with no issue.

      A couple of items to try:

      Noticed that Arduino Uno is selected as your board. This should say P1AM-100. Have you installed the P1AM-100 library?
      Files and directions at the below link:
      https://github.com/facts-engineering/P1AM

      If you go to Tools -> board info is should display:
      Click image for larger version  Name:	board.png Views:	0 Size:	6.4 KB ID:	128331

      Did you install the libraries from the dependencies link in the example? If you go to the link for ArduinoModbus -> select clone or download -> download .zip.
      Click image for larger version  Name:	zip.png Views:	0 Size:	26.3 KB ID:	128330

      Recommend to not unzip the file. Leave it zipped and once you get the file -> select sketch -> include library -> add .zip library. Choose the .zip file you just downloaded.

      Click image for larger version  Name:	library.png Views:	0 Size:	25.9 KB ID:	128329

      Also try closing the IDE and reopening after installing all the libraries to see if that clears up your issue.

      If you are still having issues just let me know.
      Last edited by ADC_CommTeam02; 02-07-2020, 02:07 PM.

      Comment


      • #4
        intmech

        You shouldn't need the P1AM library for this as mentioned above. It should also build fine as an Arduino Uno.

        It looks like the setTimeout function was added since the newest release, so if you installed through the library manager it wouldn't be in your code.
        I would: delete your current ArduinoModbus folder (path is something like C:\Users\adamc\Documents\Arduino\libraries). After that go to the ArduinoModbus Repo and follow the steps from ADC_CommTeam02 starting at "Did you install the libraries from..."

        Thanks
        Adam

        Comment


        • #5
          Thanks for the quick responses, I've made some progress. Now with the latest version of the ArduinoModbus library from github installed I can compile without error and upload to the P1AM. I can see in the serial monitor that the Modbus TCP Client is connected but whenever the P1AM goes to write to the coil I get "Failed to write coil! Connection timed out"

          Board Info:

          BN: P1AM-100
          VID: 0x1354
          PID: 0x4000
          SN: 1C661B6550515157382E3120FF163010

          serial monitor:

          Modbus TCP Client Simple Example
          P1AM-ETH at IP:192.168.1.91
          Attempting to connect to Modbus TCP server at IP:192.168.1.110
          Modbus TCP Client connected
          Failed to write coil! Connection timed out

          I can confirm that the modbus tcp server is responding at ip 192.168.1.110 and using another app I can use function 05 to write a single coil to slave 1 address 1 at the modbus tcp server on port 502 and toggle the coil from 0 to 1 and back but the arduino serial monitor continually throws the "Connection timed out" error and I have been unable to get the P1AM to be able to write to the modbus tcp server as yet.
          Attached Files

          Comment


          • #6
            Glad you got a connection going!

            It looks like your code is trying to write to Slave 0 Address 0. If you take a look at the coilWrite() functions the first argument is your address.

            Check here for a reference on the API and changing the slave ID as well: https://www.arduino.cc/en/ArduinoMod...ervercoilWrite

            Comment


            • #7
              Acording to the modbus wiki, write coil ON with value of 0xff00. This example uses 0x01. Does it make a difference?
              It has been a while- I don't remember.

              Comment


              • #8
                As mentioned above, you should try changing the node number. Your slave/server maybe using Node 0 in a special way. 255 is also typical for a ModbusTCP default node number.

                [edit]
                modbusTCPClient.coilWrite(0x00, 0x00) //Address, Value (0 or 1)
                should be this....
                modbusTCPClient.coilWrite(0xFF, 0x00, 0x00) //Node Number, Address, Value (0 or 1)
                Last edited by OpZed; 02-19-2020, 07:54 AM.

                Comment


                • #9
                  Thanks FACTS_ENG_TEAM1, I got it working now, for anyone else working through it, the modbusTCPClient.coilWrite takes a third argument for the slave and if you don't call it out it defaults to slave 0.

                  modbusTCPClient.coilWrite(slaveID, coilAddress, value)

                  Comment


                  • #10
                    intmech We have updated the code example. Appreciate the feedback!

                    Comment


                    • #11
                      Originally posted by BLANKOTADI

                      Noticed that Arduino Uno is selected as your board. This should say P1AM-100. Have you installed the P1AM-100 library?
                      I have not done anything with the P1AM stuff -- except read a few docs.
                      I'm not sure where you got the Uno board info from.

                      [EDIT] and you edit my post that you quoted to make it look like I posted some spam crap! oh well, your posts have been flagged
                      Last edited by kewakl; 03-02-2020, 05:31 PM.

                      Comment

                      Working...
                      X