Modbus Client Request Not Processed

corriibme
2022-08-04
2024-05-30
  • corriibme - 2022-08-04

    It's my very first time with Modbus but I have worked with some other networking protocols before and programmed PLCs using all of Structured Text, FBD, and LAD.

    I have this ModbusTCP code to read the input registers of a connected device. The problem is that the request messages are not being sent (Wireshark does not record any traffic) even though the xExecute input of the ClientRequestReadInputRegisters function block is changing as I desire when the clientTCP object is connected. I have also adjusted the frequency of the requests to ensure that the Modbus server can handle them that fast. However, the value of ClientTCP.udiNumReqNotProcessed keeps rising with each rising edge on ClientRequestReadInputRegisters.xExecute, although ClientTCP.udiNumMsgSent stays at 0. Is there something I need to do to have the requests processed?

    It may also be important to know that the ClientRequestReadInputRegisters function block returns no errors or exception codes. I believe that this means I have the correct values for all necessary fields for a ModbusTCP request.

    Please help if you have some Modbus experience with Codesys.

     

    Last edit: corriibme 2022-08-04
  • tuomas - 2022-08-10

    I have exactly the same problem and I urgently need help.

    When I login and run the program codesyscontrol.log and Wireshark shows the same messages which refer to opening TCP connection. So Modbus TCP/IP connection is opened, but no messages are sent from the program.

    β€’ udiNumMsgSent is 0 and udiNumReqNotProcessed keeps rising
    β€’ eErrorID is OK
    β€’ And the eExecption:= RESPONSE_SUCCESS

     

    Last edit: tuomas 2022-08-10
  • wildcard - 2024-05-13

    Hi,

    does anyone has a solution for this issue. I've the same problem.
    I've implemented a very simple client based on the Modbus Examples and connected the soft PLC to a Modbus Simulator.

    PROGRAM ModbusClient
    VAR
        initDone : BOOL := FALSE;
        errorID : ModbusFB.Error;   
        client : ModbusFB.ClientTCP;
    
        timeout : UDINT := 500000;
        replyTimeout : UDINT := 200000;
    
        aUINT : ARRAY [0..8] OF UINT;
        clientRequestReadHoldingRegisters : ModbusFB.ClientRequestReadHoldingRegisters;
    
        clientRequestsCnt : UINT := 0;
        clientRequestsProcessCnt : UINT := 0;
    
        ipAddress : ARRAY[0..3] OF BYTE := [10,54,0,72];
    END_VAR
    
    IF NOT initDone THEN
        initDone := TRUE;
        client(aIPaddr:=ipAddress, udiLogOptions:=ModbusFB.LoggingOptions.All);
        client(xConnect:=TRUE, );
        clientRequestReadHoldingRegisters(rClient:=client, udiTimeOut:=timeout, uiUnitId:=1, uiStartItem:=0, uiQuantity:=4, pData:=ADR(aUINT[0]), udiReplyTimeout:=replyTimeout);
        clientRequestReadHoldingRegisters.xExecute := TRUE;
        clientRequestsCnt := 0;
    END_IF
    
    clientRequestReadHoldingRegisters(rClient:=client, udiTimeOut:=timeout, uiUnitId:=1, uiStartItem:=0, uiQuantity:=4, pData:=ADR(aUINT[0]), udiReplyTimeout:=replyTimeout, xExecute := TRUE);
    
    IF clientRequestReadHoldingRegisters.xError THEN
        clientRequestsCnt := clientRequestsCnt +1 ;
        errorID :=  clientRequestReadHoldingRegisters.eErrorID;
    END_IF
    
    clientRequestReadHoldingRegisters(rClient:=client, udiTimeOut:=timeout, uiUnitId:=1, uiStartItem:=0, uiQuantity:=4, pData:=ADR(aUINT[0]), udiReplyTimeout:=replyTimeout, xExecute := NOT clientRequestReadHoldingRegisters.xExecute);
    

    When the system is running I do get the following on the logs:

    2024-05-13T10:18:07.443Z: Cmp=MODBUS lib, Class=1, Error=0, Info=0, pszInfo= Client.RequestProcessed ClientRequest,16#0164ADC561A0 unitId=1 fc=ReadHoldingRegisters id=2070 state=Error
    2024-05-13T10:18:07.443Z: Cmp=MODBUS lib, Class=1, Error=0, Info=0, pszInfo= ClientRequest,16#0164ADC561A0 unitId=1 fc=ReadHoldingRegisters id=2070 change state Error -> None timestamp=63843421226
    2024-05-13T10:18:08.444Z: Cmp=MODBUS lib, Class=1, Error=0, Info=0, pszInfo= ClientRequest,16#0164ADC561A0 unitId=1 fc=ReadHoldingRegisters id=2071 change state None -> Init timestamp=63844421420
    2024-05-13T10:18:09.444Z: Cmp=MODBUS lib, Class=1, Error=0, Info=0, pszInfo= ClientRequest,16#0164ADC561A0 unitId=1 fc=ReadHoldingRegisters id=2071 change state Init -> Error timestamp=63845421675

    But the errorID is jumping between OK and RequestNotProcessed.
    Any help is very appreciated which gives me a hint what I'm doing wrong.

    Thanks

     
  • zer0g - 2024-05-29

    Hi,

    I have the same problem as the guys above. Any solution or hint in the right direction is appreciated.
    Codesys version 3.5.19.2, Lib version: 4.4.0.0.

    Thank you!

     
  • paro - 2024-05-29

    Hi, Problem in your code is that you are not calling the ClientFb cyclic. Please look at the Example from CODESYS: https://forge.codesys.com/prj/codesys-example/modbus/home/Home/

    Your code works if the client fb is called cyclic!

    IF NOT initDone THEN
        initDone := TRUE;
    
        // configure clientTcp
        clientTcp(aIPaddr:=Ethernet.IPAddress, uiPort:=502);
    
        // configure clientSerial
        clientSerial(iPort:=SysCom.SYS_COMPORT2, dwBaudRate:=SysCom.SYS_BR_115200, byDataBits:=8, eParity:=SysCom.SYS_EVENPARITY, eStopBits:=SysCom.SYS_ONESTOPBIT,
                    eRtuAscii:=ModbusFB.RtuAscii.RTU);
    
        // configure clientRequestReadCoils_1               
        clientRequestReadCoils_1(rClient:=clientTcp, uiUnitId:=42, udiTimeout:=TO_UDINT(T#10MS));
    
        // configure clientRequestWriteSingleRegister
        clientRequestWriteSingleRegister(rClient:=clientTcp, uiUnitId:=43, udiTimeout:=TO_UDINT(T#10MS));
    
        // configure clientRequestReadCoils_2               
        clientRequestReadCoils_2(rClient:=clientTcp, uiUnitId:=44, udiTimeout:=TO_UDINT(T#10MS));
    
        // configure clientRequestReadInputRegisters
        clientRequestReadInputRegisters(rClient:=clientTcp, uiUnitId:=44, udiTimeout:=TO_UDINT(T#10MS));
    
    END_IF
    
    // call the client FB's
    clientTcp();
    clientSerial();
    
    // call client request FB's
    clientRequestReadCoils_1(rClient:=clientTcp, xExecute:=TRUE, uiStartItem:=2, uiQuantity:=3, pData:=ADR(aDataCoils_1[0]));
    // for more details see Example_TCP, especially ClientRequest control (xExecute, xDone, xError).
    IF clientRequestReadCoils_1.xDone THEN
        // get data from aDataCoils_1 ...
    END_IF
    
    clientRequestWriteSingleRegister(rClient:=clientTcp, xExecute:=TRUE, uiItem:=3, uiValue:=123);
    clientRequestReadCoils_2(rClient:=clientTcp, xExecute:=TRUE, uiStartItem:=2, uiQuantity:=3, pData:=ADR(aDataCoils_2[0]));
    clientRequestReadInputRegisters(rClient:=clientTcp, xExecute:=TRUE, uiStartItem:=16, uiQuantity:=3, pData:=ADR(aDataInputRegisters[0]));
    
     

    Last edit: paro 2024-05-29
  • zer0g - 2024-05-30

    I'm using the code bellow which is based on the Codesys example:

    FUNCTION_BLOCK MODBUS_master_example_ST
    VAR
        initDone : BOOL := FALSE;
        aIPAddress : ARRAY [0..3] OF BYTE := [127,0,0,1];
        clientTcp: ModbusFB.ClientTcp;
    
        // buffer to read input registers
        aDataInputRegisters : ARRAY[0..9] OF UINT;
    
        // some client requests
        clientRequestReadInputRegisters: ModbusFB.ClientRequestReadInputRegisters;
        xExecute: BOOL;
    END_VAR
    
    IF NOT initDone THEN
        initDone := TRUE;
    
        // configure clientTcp
        clientTcp(aIPaddr:=aIPAddress, uiPort:=502, udiLogOptions := ModbusFB.LoggingOptions.All);
    
        // configure clientRequestReadInputRegisters
        clientRequestReadInputRegisters(rClient:=clientTcp, uiUnitId:=1, udiTimeout:=TO_UDINT(T#1000MS));
    
    END_IF
    
    // call the client FB's
    clientTcp();
    clientRequestReadInputRegisters(rClient:=clientTcp,xExecute := xExecute AND NOT clientRequestReadInputRegisters.xBusy  ,uiStartItem:=2, uiQuantity:=3, pData:=ADR(aDataInputRegisters[0]));
    

    As you can see the clientTCP is called cyclically with the same result.

     
  • paro - 2024-05-30

    Hi,
    works in my case if I increase the timeout!

    to_udint(t#100ms) -> 100 -> 100us..

    FUNCTION_BLOCK MODBUS_master_example_ST
    VAR
        initDone : BOOL := FALSE;
        aIPAddress : ARRAY [0..3] OF BYTE := [127,0,0,1];
        clientTcp: ModbusFB.ClientTcp;
    
        // buffer to read input registers
        aDataInputRegisters : ARRAY[0..9] OF UINT;
    
        // some client requests
        clientRequestReadInputRegisters: ModbusFB.ClientRequestReadInputRegisters;
        xExecute: BOOL;
        uistart: UINT := 100;
        udiTimeout1: UDINT;
    END_VAR
    
    IF NOT initDone THEN
        initDone := TRUE;
    
        // configure clientTcp
        clientTcp(aIPaddr:=aIPAddress, uiPort:=502, udiLogOptions := ModbusFB.LoggingOptions.All);
    
        // configure clientRequestReadInputRegisters
        clientRequestReadInputRegisters(rClient:=clientTcp, uiUnitId:=1, udiTimeout:=1000000); // 1sec
    
    END_IF
    
    // call the client FB's
    clientTcp();
    clientRequestReadInputRegisters(rClient:=clientTcp,xExecute := xExecute AND NOT clientRequestReadInputRegisters.xBusy  ,uiStartItem:=uistart, uiQuantity:=3, pData:=ADR(aDataInputRegisters[0]));
    
     
  • zer0g - 2024-05-30

    Hi,
    Thank you for your suggestion but the problem is fixed after I disabled and enabled the network adapter from Windows settings.

     

Log in to post a comment.