Author Topic: Looking for Example code for using ModbusRTU serial  (Read 4029 times)

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Looking for Example code for using ModbusRTU serial
« on: December 09, 2014, 06:24:22 PM »
I have used the AdvancedHMI for one application and now I have another the needs ModbusRTU serial communications.  I will be interfacing through a USB/serial to RS-485 converter.  HMI will be a Windows 7 64bit machine.   Is there any sample code or setup documentation to guide me through this?

Thanks in advance!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Looking for Example code for using ModbusRTU serial
« Reply #1 on: December 09, 2014, 06:30:06 PM »
You will need to make sure your USB-RS-485 adapter maps to a COM port. You can check this through the Windows Device Manager.

In AdvancedHMI, after you add the ModbusRTU driver, set the ComPort, BaudRate, Parity, etc

Add controls to your form and use the common Modbus addressing scheme such as 40001. If you need to use 32 bit integers or floating points, then precede the address with an L or F (e.g L40001)

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Looking for Example code for using ModbusRTU serial
« Reply #2 on: December 10, 2014, 06:04:10 PM »
Thanks for quick response , it was very easy to set up and worked first try.

I placed a digitalpanelmeter on the form and it read the address, looked like it was polling at about 4 times a second.  However when a placed 5 digitalpanelmeters on the form with 5 different addresses, the update rate became erratic, typically about  3 seconds per update.   So what should I be doing to help the performance?
 

fohdeesha

  • Newbie
  • *
  • Posts: 46
    • View Profile
Re: Looking for Example code for using ModbusRTU serial
« Reply #3 on: December 10, 2014, 06:42:17 PM »
Keep the modbusrtu driver baud rate at 38400, I had the same weird lagging of updates when I tried to increase it. at 38400, you can drop the polloverride down to 50 (50ms, so 20 updates a second) and it's enough bandwidth to poll a few hundred meters and stuff. At 38400 and a polloverride of 50 I have a ton of meters and buttons and stuff and it's working great

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Looking for Example code for using ModbusRTU serial
« Reply #4 on: December 10, 2014, 07:01:49 PM »
As of version 3.88, the updating algorithm was changed to attempt to optimize and smooth the updates. After the first read, the driver will attempt to group values into fewer reads. For example, if you have 5 panel meters with addresses 40001, 40002, 40003, 40004, and 40005, after 5 updates the drivers will group those reads into a single packet. After minimizing the packets, the driver will then divide the PollRateOverride into the number of grouped packets and use that time as a delay in between each packet read. So let's say you now add an meter with an address of 40100, the driver will use 2 packets to get all of the data and delay 1/2 of the PollRateOverride in between each packet read. If the PollRateOverride is less than the total time to read all packets, then it will read as fast as the PLC responds.

If you are seeing erratic reads, then it is a sign of errors in the communications. As fohdeesha points out, trying to get too high of a baud rate can work against you. 38400 is a very responsive baud rate to use and does not seem to generate any errors in the tests I have done. A PollRateOverride of 50ms should get you very close to 20 updates per second.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Looking for Example code for using ModbusRTU serial
« Reply #5 on: December 10, 2014, 07:52:48 PM »
In this particular application the baud rate must be 9600 for the modbusRTU device.  I would be happy with a polling rate of 2 times per second.  I did try to change the PollRateOverride from 0 to some different values, but it didn't seem to have any effect.  I will work with it again tomorrow.
Thanks

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Looking for Example code for using ModbusRTU serial
« Reply #6 on: December 10, 2014, 08:20:58 PM »
What version are you using ?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Looking for Example code for using ModbusRTU serial
« Reply #7 on: December 10, 2014, 11:32:56 PM »
Version 3.91 is now available. Try that one to see if it resolves the issues.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Looking for Example code for using ModbusRTU serial
« Reply #8 on: December 11, 2014, 04:43:07 PM »
I have been using version 3.83

Experimenting with 3.83 and 3.91, the results are similar

.
Version 3.83
If I have 3 digitalpanelmeters polling address, 42817, 42818 and 42819, the digitalpanelmeters update OK.  If I had 3 more with addresses 42820, 42821 and 42822, then I get a "Com Error-20.No response from PLC" on each of the panelmeters.  I can see the indicators showing that its polling about once a second(PollRateOverride=0)

Version 3.91
"Com Error 2. Illegal data address"

With the six digitalpanelmeters, when the form opens, all 6 update with data, then alternate showing that same data and "Com Error 2. Illegal data address" in the digitalpanelmeter label.

With three digitalpanelmeters, it works OK.

I am using Visual Studio Express 2013 on a 64bit windows machine.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Looking for Example code for using ModbusRTU serial
« Reply #9 on: December 11, 2014, 07:59:05 PM »
I ran this exact scenario on the version 3.92 that I am testing with and I get no errors. I even added some additional reading via code. I will get this version posted in a few hours.

scott.clark

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Looking for Example code for using ModbusRTU serial
« Reply #10 on: December 16, 2014, 02:50:53 PM »
I tried my application now with version 3.93 and was still getting "Com Error-20.No response from PLC".  What I found is that the ModbusRTU device I am dealing with has a maximum 5 element limitation on reading and writing registers.  I now have have a form with 9 digitalpanelmeters, and as long as there are now more than 5 addresses in a continuous space, there are no errors.

This is my first ModbusRTU project.  What is typical limitation on number of elements that can be read or written in one packet?