PIC as MODBUS slave

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

I'm afraid the Modbus slave component will only work with Flowcode v7. If you already have a v6 license then you get a significant discount on the price of the v7 license.

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Hello to all!
I made some tests of code that I post below, and works fine as modbus slave. With a modbus master emulator I send characters one by one to an LCD 4X20 , using modbus command 6 (Write Single Register). The problem is , when I try to send a string of characters , 20 or more , there is a delay (about 1 sec. per character) to display. That is not practical especially if I want to use whole capacity of LCD (80 characters). The modbus command to send a long string of characters is 16 (Write Multiple Registers).When I use modbus command 16 to send a string with the modbus master emulator I receive an error. I noticed that at FLOWCODE7 modbus slave component there is no a parameter for multiple registers . Would you like to inform me how many characters can I send as string , and how can I do that, to use whole area of LCD . Thanks.
Attachments
ModBus_Slave_LCD_4-DEMO.fcfx
(11.44 KiB) Downloaded 454 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

Right I have now added support for the WriteMultipleRegisters command. Previously it was not supported in the slave code.

The command should now support up to 20 registers to be set in a single transaction.

Let me know how you get on.
ModbusSlave.fcpx
(89.97 KiB) Downloaded 440 times

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Many thanks Benj,
I made a copy of ModbusSlave.fcpx to components directory of FLOWCODE7, but there is no WriteMultipleRegisters command in the ModbusSlave component. Is there something wrong with the update ? How can I check that, and what modifications must be done in ModBus_Slave_LCD_4-DEMO.fcfx to work with WriteMultipleRegisters command from the modbus master?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

The WriteMultipleRegisters command is now supported as part of the CheckForIncoming macro. So hopefully no modifications are required for your program other then to re-compile and program.

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks Benj ,
I'll make some tests to confirm .

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Hello to all!
After last modification of the code to support Modbus command 16 (WriteMultipleRegisters), I made some tests with an LCD 4x20 as slave. I send 2 requests to the slave for 2 and 20 characters. As master I use a PLC TM221C24R (SCHNEIDER) and a master emulator ComTestPro .The results are below:
1. Test with PLC
Send 2 characters( 2,3 ) at Slave 01 with start address 07
DATA OUT [hex]
[01] [10] [00 07] [00 02] [04] [00 32] [00 33] [53 93]
DATA IN [hex]
[01] [10] [00 09] [00 02] [91 CA]
No data on lcd . PLC shows communication error. Maybe there is a response error . As it seems at DATA IN, start address is wrong [00 09], so the CRC is no valid.

Send a string of 20 characters at Slave 01 with start address 07
DATA OUT [hex]
[01] [10] [00 07] [00 14] [ 20 ch. data......... ] [3C 86]
DATA IN [hex]
[01] [10] [00 1B] [00 14] [80 01].
No data on LCD. PLC shows communication error . The same problem with start address at DATA IN ( [00 14] instead of [00 07] ) .

2. Test with master emulator ComTestPro
At that case, with the same requests as with PLC above , the LCD shows some characters , but the emulator panel shows no valid response error , and the DATA IN, DATA OUT are the same as at test with PLC .
I think that there is a problem with response data, maybe at code for modbus command 16. Is there a way to solve this?
Below I post the latest code.
Thanks!
Attachments
ModBus_Slave_LCD_4-DEMO.fcfx
(11.45 KiB) Downloaded 387 times

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by EtsDriver »

Is the slave answering right ?
Attachments
Screenshot_20170228-072529.png
(414.15 KiB) Downloaded 824 times
Ill just keep the good work up!

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Steve001 »

Hi

as ets driver stated is the slave answering correctly ?

I would test first with your emulator and get it working with that as PLC's are notorious for having problems with MODBUS interfaces and not being able to communicate with the required number of devices on the loop or the distance or both !

steve
Success always occurs in private and failure in full view.

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by EtsDriver »

Hi!

Sorry, did not notice you had allready shown that the response is wrong!

It seems that the modbus slave is answering in wrong format to the master...

I built your system in eblocs friendly way to my desk and tested out with RS485 using modbus poll program:

I write 0x0032 to register 40007 and 0x0033 to register 40008 and my display shows number 23 on the first line.

The sent data is:
01 | 10 | 00 06 | 00 02 | 04 | 0032 | 0033 | 925F

SlaveID | Function | Start address | registers to write | databytes to follow| data1 | data2 | CRC |
(address given in plc base1 7 = 6)

Response is the 01 10 00 08 00 02 C0 0A, and the data changes on the display of mine, but modbus poll shows that it gets response error.

Response to this should be 01 10 00 06 00 02 A1 C9
Attachments
ModBus_Slave_LCD_4-DEMO_modified_for_eblocks_19MHZ_clock.fcfx
(11.48 KiB) Downloaded 349 times
Ill just keep the good work up!

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks to all for your help!
As I stated in my previous post, my target is to use the code to send some messages from a PLC to a number of LCD's . When I use an emulator there is no problem for displaying the characters, although there is a response error. That is only for communication test and checking data , but in reality, a PLC use more accurate diagnostics for communication protocols. So far I can't send any data from the PLC to LCD with modbus protocol. I think that the problem is at code , because when I use the same PLC to send characters to a MODBUS LCD 4X20 ( SC2004MBS-SILICON CRAFT ) , that LCD works well. I noticed that the PLC after sending the message to LCD, wait some seconds, make some diagnostics to serial line data and after that, enable the communication. This waiting time there is no at master emulator. Also , checking all the response data, I noticed that the wrong START ADDRESS is the sum of correct START ADDRESS and QUANTITY OF REGISTERS . I will continue the tests with different pairs PLC-LCD.

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by EtsDriver »

What kind of diagnostics the plc runs on serial line? Does it do modbus diagnostic command to your DIY modbus display or does it use some kind of REPORT-SLAVE-ID (command 17) as diagnostics??
Ill just keep the good work up!

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

I have a hardware guide only for that PLC . Maybe the PLC use some kind of diagnostic command, maybe not. I suppose that, observing the relatively long time to enable communication at serial line. However, I think that the problem is not at PLC that works well with any modbus display.
Attachments
MODICON-M221 PLC.pdf
(4.45 MiB) Downloaded 347 times

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by EtsDriver »

Oh it could be the timeout when no correct answer is received, on some systems those are very long... Hope Matrix gets the response fixed asap so you can get it working correctly!
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

Many thanks for letting us know of the problem. I have found and hopefully fixed the problem in the attached component.
ModbusSlave.fcpx
(89.99 KiB) Downloaded 302 times
Let me know how you get on.

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks Benj for your help.
Now the response code is correct, but I have no data on LCD when send a modbus command from the PLC. I continue the tests to find the reason.

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Hello to all and thanks in advance for any help!
I made some tests about to send characters from a PLC to an LCD 4X20. When I set Quantity (is the the total number of characters that I want to sent) up to 20 , I have the correct characters at LCD ,when the FirstObj is the start address for the string. But when I set Quantity more than 20 , I have no data at LCD . The communication is ok, because there is no error, but the busy output is on at execute window. Generally, I can display only 20 characters, while I try to send more than 20. What do you think about that , the problem is about timing , or parameter modification of pic code /plc code ?
Attachments
The data that transferred to LCD when run the test program.The above data is for Quantity=20 , FirstObj=10
The data that transferred to LCD when run the test program.The above data is for Quantity=20 , FirstObj=10
20170314_001621.jpg (59.29 KiB) Viewed 20955 times
Ashampoo_Snap_2017.03.14_01h06m42s_004_.png
This is the PLC configuration panel for communication (RS-485,9600,N,1)
(153.58 KiB) Downloaded 117 times
Ashampoo_Snap_2017.03.14_01h08m43s_005_.png
A small test program (LD) to send some characters to LCD.
(156.04 KiB) Downloaded 117 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

In your Flowcode Modbus Slave program what is the Receive Buffer Size property set to? This sets how much data can be received in a single message and therefore how much memory to reserve. The default is 20 bytes.
MbSlave.jpg
MbSlave.jpg (11.17 KiB) Viewed 20958 times

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks Benj ,
The Receive Buffer Size it was 20 , I set to 80 . I try to send 10 charachters per row with FirstObj = 0 and Quantity=80 from PLC. No data at display . no comm. errors , the busy is on . When I try to send data with master emulator there is no problem. The strange is that there is no communication error to help me to found the reason ,at the help-error diagnostics of PLC. Is it wrong timing parameter and for what code ? When I connect the PLC with a modbus LCD that I have ( SC200MBS 4X20 ) that LCD works fine.
Attachments
Ashampoo_Snap_2017.03.14_16h53m16s_001_.png
This is the last set up of modbus slave component properties
(127.24 KiB) Downloaded 78 times

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Hello Benj,
in your previous post you write :

" Right I have now added support for the WriteMultipleRegisters command. Previously it was not supported in the slave code.

The command should now support up to 20 registers to be set in a single transaction. "

So far , I can display up to 20 characters on LCD , while I send more than 20 and up to 80. I have response error when I send more than 20. Is that about the limit of 20 registers that support the WriteMultipleRegisters command ? If yes , is it possible to extend that limit? If that is not possible what modification can I make to FC7 code to display up to 80 characters in one transaction? Is it possible to receive and display in 4 packets of 20 , because I can't sent more than 1 request from PLC?
Thanks in advance for your help and support.
Attachments
ModBus_Slave_LCD_4-DEMO-F.fcfx
(11.45 KiB) Downloaded 251 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by Benj »

Hello,

I believe I have found and fixed the issue. The data was being read out the comms circular buffer into a local buffer which was fixed at 40 bytes or 20 registers. This variable is now dynamically sized based on your max receive buffer property setting. Ideally I would like to keep the data inside the existing circular buffer but then I can't ensure that no further communications bytes will be received while processing the command so for now I am using a new unique RAM buffer.

I have also fixed a problem where the simulation version of the CheckForIncoming had fallen behind the embedded version in terms of what was supported.
ModbusSlave.fcpx
(91.47 KiB) Downloaded 250 times
Let me know how you get on.

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks Benj for your help.
Below I post the code that so far works fine. I continue the tests adding some I/O at the PIC that can be controlled from the master, and make some modifications to hardware .
Attachments
ModBus_Slave_LCD_4-DEMO-F.fcfx
(11.45 KiB) Downloaded 222 times

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Hello Benj,
Before add some I/O to PIC , I was checked with help of a master emulator (Simply Modbus Master) some modbus command that I will use . I noticed that some commands as 01 (Read Coil States ) , 02 (Read Input States) , 04(Read Analog Input) and 05(Set Coil State) shows response code error as it seems at the snap that I post. The error at response code is the same at all commands. Would you like to check if that is error at my code as slave , or at FLOWCODE7 ?
Attachments
Ashampoo_Snap_2017.03.27_01h50m20s_005_.png
The response error as it seems at send/receive data at the bottom, is the same at commands 01,02,04,05 .
(139.41 KiB) Downloaded 11868 times

EtsDriver
Posts: 444
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by EtsDriver »

Have you set up the register counts for coils and analog?
FC 7 is smart and the code will answer "Illegal data adress" if it has no corresponding registers set up on components properties for that function code.
4x registers are Holding registers.
Ill just keep the good work up!

SILVESTROS9
Posts: 115
Joined: Wed Aug 03, 2016 10:45 pm
Has thanked: 24 times
Been thanked: 11 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by SILVESTROS9 »

Thanks EtsDriver, I forgot to setup component properties. After that, I have no errors in command's responses. Now I'll continue to make some tests with I/O .

Post Reply