I'm having a few issues with the ModbusFB Library. I'm trying to create some reusable function blocks for devices I use quite often. The below is just a simple Modbus Relay board with 2x Inputs and 2x Relay Outputs.
My Function blocks works with one SerialClient connection, as soon as I link the second ClientSerial to the second FB I get error 56 which is reply timeout. Both FBs work if I connect each one independantly, just can't have both connected together.
I have also tested using the original ModbusFB.ClientSerial Function Block and I get the exact same results. I have tried with 2x ClientSerial for each function block and still get the same error. Seems like I can only use 1x ClientSerial with 1x Function Block
You cannot execute 2 functions at the same time on the same port. You have to sequence each request.
I would call the read/write inside the MODBUS_CLIENT_SERIAL and then provide a queue for the MODBUS_RELAY FBs. Then just place a request with its parameters in the queue with MODBUS_RELAY, process them in MODBUS_CLIENT_SERIAL, using pointers for in/out data, and also results. This way the asking RELAY FB could be aware his request have been processed.
For less modifications of your code, you can also add a flag that the line is currently busy, but you will have to manage RELAY FBs concurrency.
Last edit: dFx 2022-02-18
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks, that makes perfect sense and I should have picked up on that one. Would you have an example of setting up a queue with the pointers? I haven't used pointers or anything like that before so wouldn't know where to start. I'm trying todo it dynamically so I can easily add more of the same devices or different devices in the future without much modification to the project.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Pointers are not a big deal. See help
For instance, you may have a struct, for the queue elements.
Lets say you need to know the type of work (read/write), the data to be written or read.
STRUCT ModbusQueueItem TypeOfWork : INT; pData : Pointer TO BOOL; // Data pointer for boolean data szData : INT; // Size of dataEND_STRUCT
So In case of Read, just provide the pData to pData of ClientRequestReadDiscreteInputs, given that your RELAY block did correctly set the item using ADR(YourData).
This is only a draft, not real code, so there maybe adjustements to do on types.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can you not pass the whole struct as an In/Out and every FB have a connection to that struct?
What I'm thinking is every slave already has an unique ID/Address and in the Modbus_Client_Serial FB having a FOR loop looking for these IDs may have 8 devices all with different addresses and store them in an array and then use another FOR loop based on the size of that array to trigger the slave devices in sequence based on there address and when that device FB is done it goes on to the next device FB? I haven't dealt with comms stuff in the past and the correct way todo this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I'm having a few issues with the ModbusFB Library. I'm trying to create some reusable function blocks for devices I use quite often. The below is just a simple Modbus Relay board with 2x Inputs and 2x Relay Outputs.
My Function blocks works with one SerialClient connection, as soon as I link the second ClientSerial to the second FB I get error 56 which is reply timeout. Both FBs work if I connect each one independantly, just can't have both connected together.
[-img src=CodeSys ModbusFB Issue.png width=50%: missing =-]
Here is the ClientSerial ModbusFB
Here is the code
Here is the Relay Board Code
Here is the Code
I have also tested using the original ModbusFB.ClientSerial Function Block and I get the exact same results. I have tried with 2x ClientSerial for each function block and still get the same error. Seems like I can only use 1x ClientSerial with 1x Function Block
You cannot execute 2 functions at the same time on the same port. You have to sequence each request.
I would call the read/write inside the MODBUS_CLIENT_SERIAL and then provide a queue for the MODBUS_RELAY FBs. Then just place a request with its parameters in the queue with MODBUS_RELAY, process them in MODBUS_CLIENT_SERIAL, using pointers for in/out data, and also results. This way the asking RELAY FB could be aware his request have been processed.
For less modifications of your code, you can also add a flag that the line is currently busy, but you will have to manage RELAY FBs concurrency.
Last edit: dFx 2022-02-18
Hi dFx,
Thanks, that makes perfect sense and I should have picked up on that one. Would you have an example of setting up a queue with the pointers? I haven't used pointers or anything like that before so wouldn't know where to start. I'm trying todo it dynamically so I can easily add more of the same devices or different devices in the future without much modification to the project.
Pointers are not a big deal. See help
For instance, you may have a struct, for the queue elements.
Lets say you need to know the type of work (read/write), the data to be written or read.
So In case of Read, just provide the pData to pData of ClientRequestReadDiscreteInputs, given that your RELAY block did correctly set the item using ADR(YourData).
This is only a draft, not real code, so there maybe adjustements to do on types.
Hi dFx,
Can you not pass the whole struct as an In/Out and every FB have a connection to that struct?
What I'm thinking is every slave already has an unique ID/Address and in the Modbus_Client_Serial FB having a FOR loop looking for these IDs may have 8 devices all with different addresses and store them in an array and then use another FOR loop based on the size of that array to trigger the slave devices in sequence based on there address and when that device FB is done it goes on to the next device FB? I haven't dealt with comms stuff in the past and the correct way todo this.