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.
Modbus RS232 Setholdingregister
-
- Posts: 37
- http://meble-kuchenne.info.pl
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times
Modbus RS232 Setholdingregister
- 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.
-
- Matrix Staff
- Posts: 1739
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 440 times
- Been thanked: 603 times
Re: Modbus RS232 Setholdingregister
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
-
- Posts: 37
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times
Re: Modbus RS232 Setholdingregister
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 wrote: ↑Wed Mar 08, 2023 11:34 amHello,
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
-
- Matrix Staff
- Posts: 1739
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 440 times
- Been thanked: 603 times
Re: Modbus RS232 Setholdingregister
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
-
- Posts: 37
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times
Re: Modbus RS232 Setholdingregister
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.
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.
- Attachments
-
- poll definition
- MicrosoftTeams-image (1).png (23.72 KiB) Viewed 1309 times
-
- Modbus poll
- MicrosoftTeams-image (2).png (41.79 KiB) Viewed 1309 times
-
- After power cycling
- MicrosoftTeams-image (3).png (50.89 KiB) Viewed 1309 times
-
- Matrix Staff
- Posts: 1739
- Joined: Mon Dec 07, 2020 10:06 am
- Has thanked: 440 times
- Been thanked: 603 times
Re: Modbus RS232 Setholdingregister
Hello,
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.I tried moving it to the decision component, but I would get errors on the modbuss poll progran.
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.Why is it when I power cycle I get random values?
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.The getLastIncoming. I am unsure of how it is supposed to work. Does it serve as a diagnostic tool?
Regards Ben Rowland - MatrixTSL
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
Flowcode Online Code Viewer (Beta) - Flowcode Product Page - Flowcode Help Wiki - My YouTube Channel
-
- Posts: 37
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times
Re: Modbus RS232 Setholdingregister
Hi Ben,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.
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
-
- Posts: 37
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times
Re: Modbus RS232 Setholdingregister
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 (28.36 KiB) Viewed 1254 times
-
- Posts: 37
- Joined: Thu Feb 02, 2023 9:32 pm
- Has thanked: 19 times
- Been thanked: 3 times