Modbus RS232 Setholdingregister

For general Flowcode discussion that does not belong in the other sections.
Post Reply
flowcode-developer
Posts: 37
http://meble-kuchenne.info.pl
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Flowcode v10 Modbus RS232 Setholdingregister

Post by flowcode-developer »

Hello,
I am having trouble with Modbus rs232 setholding register. I have a larger program where I take the value from one device using UART. Next step I want to put the value into a holding register. I proved that the devices can talk to each other using UART but when I try with the holding register, I get issues. I have the value going out to Modbus poll program using baud 9600, 1 stop, 1 start, no parity. The Modbus slave is slave id 2, baud 9600, address starts at 100 with a length of 10. The issues I get are timeout error.

I broke down the program to what I believe would be the minimum to recreate my issue. I get the timeout error and I can't figure out why. I posted my code below. Thank you for your time and help.
Attachments
modbusTESTER.fcfx
(12.14 KiB) Downloaded 37 times
Last edited by flowcode-developer on Thu Mar 09, 2023 6:45 pm, edited 1 time in total.

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: Modbus RS232 Setholdingregister

Post by BenR »

Hello,

As you have a 5 second delay in your loop the master is going to have a hard time polling the slave at just the right moment.

Here is a slightly reworked version of your program that starts up comms and then repeatedly waits for incoming commands from the master.

modbusTESTER.fcfx
(13.72 KiB) Downloaded 43 times

flowcode-developer
Posts: 37
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Re: Modbus RS232 Setholdingregister

Post by flowcode-developer »

BenR wrote:
Wed Mar 08, 2023 11:34 am
Hello,

As you have a 5 second delay in your loop the master is going to have a hard time polling the slave at just the right moment.

Here is a slightly reworked version of your program that starts up comms and then repeatedly waits for incoming commands from the master.


modbusTESTER.fcfx
Thank you for that! I thought I would need the delay so that the UART can have time to setup. (my other program) I was recommended to use an interrupt from a previous post I made. viewtopic.php?f=5&t=1788 If I switch over to using an interrupt would that affect the modbus polling? Is it to me benefit to use a circular buffer? Sorry for all the questions these concepts are new to me.

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: Modbus RS232 Setholdingregister

Post by BenR »

Hello

The modbus slave component already uses the interrupt behind the scenes to fill a circular buffer but needs to process the buffer and respond to the master request in a timely manner. As long as your while loop can loop and call the slave check macro within the master timeout period then you should get reliable comms between the two.

flowcode-developer
Posts: 37
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Re: Modbus RS232 Setholdingregister

Post by flowcode-developer »

I tried and played with your version of the modbus_Tester. I am having issues with it. It shows all zeros in the modbus poll program. It does connect as I do not get a connection error. Can I set the defaults as decisions? For example if rx does not equal one then set register 105 to value 113? I tried moving it to the decision component, but I would get errors on the modbuss poll progran.

Why is it when I power cycle I get random values?

The getLastIncoming. I am unsure of how it is supposed to work. Does it serve as a diagnostic tool?

Sorry for all the questions. I really want to become more familiar with using modbus with Flowcode. I really appreciate your help! I have attached images below.

Image
Attachments
poll definition
poll definition
MicrosoftTeams-image (1).png (23.72 KiB) Viewed 1308 times
Modbus poll
Modbus poll
MicrosoftTeams-image (2).png (41.79 KiB) Viewed 1308 times
After power cycling
After power cycling
MicrosoftTeams-image (3).png (50.89 KiB) Viewed 1308 times

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: Modbus RS232 Setholdingregister

Post by BenR »

Hello,
I tried moving it to the decision component, but I would get errors on the modbuss poll progran.
Attach yur program that does this and maybe we can help to get you what you're after or advise why you're getting errors.
Why is it when I power cycle I get random values?
If you read a variable before you write it then it will contain random uninitialised data. Always a good idea to write before you read. Before the main loop you could set all the initial register values so they are in a known state.
The getLastIncoming. I am unsure of how it is supposed to work. Does it serve as a diagnostic tool?
Yes it's just getting the details of the last master operation should you need to know what has been accessed and potentially updated. Saves having to poll through all of your local register values to find the one value changed by the master.

flowcode-developer
Posts: 37
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Re: Modbus RS232 Setholdingregister

Post by flowcode-developer »

Attach yur program that does this and maybe we can help to get you what you're after or advise why you're getting errors.
Hi Ben,
I have attached my code below. I have another question about my program do I need to include (rx=1) from CircularBuffer::GetNumberBytes() in the decision component or is it sufficient that I have (numBytes>0) from ModbusSlave::CheckForIncoming()? Every time I have used rx=1 my program does not work. I haven't had any issues with getNumberBytes as I proved that it worked with UART before I started to integrate Modbus.

Thank you
Attachments
inspectra2.fcfx
program that receives command from device and returns model number to reg on modbus.
(22.56 KiB) Downloaded 37 times

flowcode-developer
Posts: 37
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Re: Modbus RS232 Setholdingregister

Post by flowcode-developer »

I made a macro that has while loop that initialized the Modbus holding reg. (see below) I then put this macro right after all of the initialize components. This worked for me. I do not understand why? The examples shown on the Flowcode Wiki don't set their modbusslave:setholdingregister registers to 0. Hope this helps someone. Any other tips or tricks with modbus slave holding reg would be appreciated.

Code: Select all

addr = 0
Attachments
initModbusReg.PNG
initModbusReg.PNG (28.36 KiB) Viewed 1253 times

flowcode-developer
Posts: 37
Joined: Thu Feb 02, 2023 9:32 pm
Has thanked: 19 times
Been thanked: 3 times

Flowcode v10 Re: Modbus RS232 Setholdingregister

Post by flowcode-developer »

bump

Post Reply