Reading values from Holding Register on P1AM PLC and EA3-S3ML HMI. to print them on the Serial monitor of the PLC P1AM -100.

I am currently trying to read values from the holding register using the holdingRegisterRead function. I would like to be able to enter values into the numeric entry on the EA3-S3ML HMI then read them on the Serial Monitor on the P1AM side. So far all I have been able to see in the Serial Monitor is a -1 despite declaring the variable as either a "int" and uint16_t. The two addresses I am trying to read from are on 40002 and 40003 on the C-more side. I will post the code so you are able to see what I have done so far. Thank you


    1. #include <Ethernet.h>
    2. #include <P1AM.h>
    3. #include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
    4. #include <ArduinoModbus.h>
    5.  
    6.  
    7. //int userInput_TimeOn[onTime] = {0}; //for storing how long the output is on
    8. //int userInput_Time_Off[offTime] = {0}; //for storing how long the output is off
    9. //int userOutput_seLect[selectOuputs] = {0,0,0,0,0,0,0,0}; //for storing how many outputs does user want to use
    10. //int cycleAmount[howManyCycles] = {0}; //for storing how many cycles total does the user want
    11.  
    12.  
    13.  
    14.  
    15. int previous_Cycles = 0;
    16. unsigned long previousTime = 0;
    17. unsigned long eventInterval1 = 1500;
    18. unsigned long eventInterval2 = 2500;
    19. bool cmore_buttonState1 = false;
    20.  
    21.  
    22. byte mac[] = { //Use the �Reserved MAC� printed on the right-side label of your P1AM-ETH.
    23. 0x60, 0x52, 0xD0, 0x07, 0x42, 0XC7
    24. };
    25. IPAddress ip(192, 168, 0, 177); //IP Address of the P1AM-ETH module.
    26.  
    27. EthernetServer server(502);
    28. EthernetClient clients[8];
    29. ModbusTCPServer modbusTCPServer;
    30.  
    31. int client_cnt;
    32. bool cmore_button = false; // state of cmore button
    33. bool cmore_buttonState = false;
    34. uint16_t CYCLE_COUNTER = 0;
    35. int on_Time = 0;
    36. int off_Time = 0;
    37.  
    38. void setup() {
    39. Serial.begin(115200);
    40. Serial.println("Modbus TCP Server and Module I/O Example");
    41. while (!P1.init()) {} ; //Wait for P1 Modules to Sign on
    42. Ethernet.init(5);
    43. Ethernet.begin(mac, ip);
    44. server.begin();
    45.  
    46. if (!modbusTCPServer.begin()) {
    47. Serial.println("Failed to start Modbus TCP Server!");
    48. while(1);
    49. }
    50.  
    51. modbusTCPServer.configureCoils(0x00, 16); //setup 16 coils starting at address 0
    52. modbusTCPServer.configureHoldingRegisters(0x00, 16);
    53. modbusTCPServer.configureInputRegisters(0x00, 16);
    54. }
    55.  
    56. void loop() {
    57.  
    58. EthernetClient client = server.available(); //listen for incoming clients
    59. if (client) {
    60.  
    61. if (client.available()) {
    62. //Serial.println("Got Client");
    63. modbusTCPServer.accept(client); //accept connection.
    64. modbusTCPServer.poll();// service any Modbus TCP requests, while client connected.
    65. cmore_button = modbusTCPServer.coilRead(0X00); // Read coil 1, coilRead adds the function code.
    66. modbusTCPServer.holdingRegisterWrite(0X00, CYCLE_COUNTER);
    67. int on_Time = modbusTCPServer.holdingRegisterRead(0X20);
    68. int off_Time = modbusTCPServer.holdingRegisterRead(0X30);
    69. static unsigned long cycle_Counter = 0;
    70. unsigned long currentTime = millis();
    71.  
    72.  
    73.  
    74. if (cmore_button == true) {
    75. if (currentTime - previousTime >= eventInterval1) {
    76. previousTime = currentTime;
    77. if (cmore_buttonState1 == false) cmore_buttonState1 = true; else cmore_buttonState1 = false;
    78. P1.writeDiscrete(cmore_buttonState1, 2, 1);
    79.  
    80. if (cmore_buttonState1 == 0) {
    81. for (int i = 0; i < 1; i++) {
    82. cycle_Counter++;
    83. CYCLE_COUNTER = cycle_Counter;
    84. Serial.print("Cycles:");
    85. Serial.println(CYCLE_COUNTER);
    86. }
    87.  
    88. Serial.print("On Time:");
    89. Serial.println(on_Time);
    90. Serial.print("Off Time:");
    91. Serial.println(off_Time);
    92.  
    93. }
    94. }
    95.  
    96. }
    97.  
    98. }
    99.  
    100.  
    101. if (cmore_button == false) {
    102. P1.writeDiscrete(0, 2, 1);
    103.  
    104. }
    105. if (client && !client.connected()) {
    106. client.stop();
    107. }
    108.  
    109. }
    110. }

     

    Expand Post
  • FACTS_MikeSc (AutomationDirect)

    Lines 68 + 69

    The holding register addresses you are trying to read do not appear to be correct to what your c-more is setup and what you have configured on line 52. I think you accidentally put a 0 at the end is my guess.

     

    You're getting a -1 on your prints.

    If you look at the modbus library.

    https://www.arduino.cc/reference/en/libraries/arduinomodbus/modbusserver.holdingregisterread/

    -1 represents a failure for the return value.

    Expand Post
      1. Thank you for your response. I am not sure I understand what you meant by I accidently added a 0 at the end. Are you saying it should be 0x0 instead of 0x00?
      1. modbusTCPServer.configureHoldingRegisters(0x00, 16);
      2. modbusTCPServer.configureInputRegisters(0x00, 16);
      3. }

       

      Expand Post
      • FACTS_MikeSc (AutomationDirect)

        Sorry that is bad wording on my part.

         

        The address you passed in is 0x20 and 0x30. I think you meant to put in 0x2 and 0x3 if those are the correct modbus addresses on the c-more panel.

         

        I referenced the configuration because you only assigned 16 addresses starting at 0x0. So you were also trying to read a address out of the range of what you configured.

        Expand Post
      • FACTS_MikeSc (AutomationDirect)

        No, fix lines 68+69 addresses.

        Example:

        C-More --> ON TIME1: 40003

        P1AM-100 --> line 68 --> 0x2 NOT: 0x20

        1. Here is the updated code on the P1AM side
        2.  
        3.  
        4. #include <Ethernet.h>
        5. #include <P1AM.h>
        6. #include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
        7. #include <ArduinoModbus.h>
        8.  
        9.  
        10. //int userInput_TimeOn[onTime] = {0}; //for storing how long the output is on
        11. //int userInput_Time_Off[offTime] = {0}; //for storing how long the output is off
        12. //int userOutput_seLect[selectOuputs] = {0,0,0,0,0,0,0,0}; //for storing how many outputs does user want to use
        13. //int cycleAmount[howManyCycles] = {0}; //for storing how many cycles total does the user want
        14.  
        15.  
        16.  
        17.  
        18.  
        19.  
        20. int previous_Cycles = 0;
        21. unsigned long previousTime = 0;
        22. unsigned long eventInterval1 = 1000;
        23. unsigned long eventInterval2 = 2500;
        24. bool cmore_buttonState1 = false;
        25.  
        26.  
        27. byte mac[] = { //Use the �Reserved MAC� printed on the right-side label of your P1AM-ETH.
        28. 0x60, 0x52, 0xD0, 0x07, 0x42, 0XC7
        29. };
        30. IPAddress ip(192, 168, 0, 177); //IP Address of the P1AM-ETH module.
        31.  
        32. EthernetServer server(502);
        33. EthernetClient clients[5];
        34. ModbusTCPServer modbusTCPServer;
        35.  
        36. int client_cnt;
        37. bool cmore_button = false; // state of cmore button
        38. bool cmore_buttonState = false;
        39. uint16_t CYCLE_COUNTER = 0;
        40.  
        41. uint16_t on_Time; //Modbus Holding Registers
        42. uint16_t off_Time;
        43.  
        44.  
        45. void setup() {
        46. Serial.begin(115200);
        47. Serial.println("Modbus TCP Server and Module I/O Example");
        48. while (!P1.init()) {} ; //Wait for P1 Modules to Sign on
        49. Ethernet.init(5);
        50. Ethernet.begin(mac, ip);
        51. server.begin();
        52.  
        53. if (!modbusTCPServer.begin()) {
        54. Serial.println("Failed to start Modbus TCP Server!");
        55. while (1);
        56. }
        57.  
        58. modbusTCPServer.configureCoils(0x00, 16); //setup 16 coils starting at address 0
        59. modbusTCPServer.configureHoldingRegisters(0x00, 16);
        60. modbusTCPServer.configureInputRegisters(0x00, 16);
        61. }
        62.  
        63. void loop() {
        64.  
        65.  
        66. EthernetClient client = server.available(); //listen for incoming clients
        67. if (client) {
        68.  
        69. if (client.available()) {
        70. //Serial.println("Got Client");
        71. modbusTCPServer.accept(client); //accept connection.
        72. modbusTCPServer.poll();// service any Modbus TCP requests, while client connected.
        73. cmore_button = modbusTCPServer.coilRead(0X00); // Read coil 1, coilRead adds the function code.
        74. on_Time = modbusTCPServer.holdingRegisterRead(0x3);
        75. off_Time = modbusTCPServer.holdingRegisterRead(0X4);
        76. }
        77. }
        78.  
        79.  
        80.  
        81. static unsigned long cycle_Counter = 0;
        82. unsigned long currentTime = millis();
        83.  
        84.  
        85.  
        86.  
        87. if (cmore_button == true) {
        88.  
        89. if (currentTime - previousTime >= eventInterval1 ) {
        90. previousTime = currentTime;
        91. if (cmore_buttonState1 == false) cmore_buttonState1 = true; else cmore_buttonState1 = false;
        92. P1.writeDiscrete(cmore_buttonState1, 2, 1);
        93.  
        94. if (cmore_buttonState1 == 0) {
        95. for (int i = 0; i < 1; i++) {
        96. cycle_Counter++;
        97. CYCLE_COUNTER = cycle_Counter;
        98. Serial.print("Cycles:");
        99. Serial.println(CYCLE_COUNTER);
        100.  
        101.  
        102. }
        103.  
        104. Serial.print("on_Time:");
        105. Serial.println(on_Time);
        106. Serial.print("off_Time:");
        107. Serial.println(off_Time);
        108.  
        109.  
        110.  
        111. }
        112. }
        113.  
        114. }
        115.  
        116.  
        117.  
        118.  
        119. if (cmore_button == false) {
        120. P1.writeDiscrete(0, 2, 1);
        121.  
        122. }
        123.  
        124. if (client && !client.connected()) {
        125. client.stop();
        126. }
        127.  
        128. }

         

        Expand Post
  • ok I erased on of the 0s on the C-more side. and the address is consistent with that you recommended and I am getting 0s on the serial monitor when I enter a number.

     

10 of 12