PIC as MODBUS slave

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

Moderator: Benj

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!
Continuing to make tests for I/O , I've some problems mainly at modbus command 15 ( Write Multiple Coils ). I add a demo code to control 8 coils at PORTA of PIC16F1939 with PLC ( I post the code below ). First I try to send and write a byte from PLC to PIC register . As it seems at PLC programming snap, I send the E5 (hex) expecting to have at PORTA that I set as 8bit coils
the output 11100101 (although PORTA has only 6 bit -A.0 to A.5- and one of that used for RS485 data direction input pin at PIC, so I use for now the first 5 bits ). I connect an array of 8 leds to PORTA of PIC to observe the data. I run the PLC program ,but there is no info at PORTA. As it seems at PLC snap there is an error (254) that is response error.
After that, I use the Master emulator to test my code. As it seems at snaps, I set 2 register sizes to check the send/receive data. Below are the output at PORTA:
1. When I set register size = 16bit registers ----------> PORTA data = 00000001
2. When I set register size = 1bit coils ----------------> PORTA data = 00000011
I'm a little confused for what is the correct set for above register size at master emulator and why, because the data is send to slave as 2 hex byte ,while the PIC registers are 16bit. Also I noticed that there is an error at response code when I use Modbus command 15 , and sometimes at command 1 (Read Coils). Would you like to check if there is a mistake at my code or at component code?
Many thanks in advance .
Attachments
Ashampoo_Snap_2017.04.08_20h46m54s_001_.png
Send a byte E5 (hex) to slave . No data at slave- CommError 254 (response error).
(220.98 KiB) Downloaded 13709 times
Ashampoo_Snap_2017.04.08_23h18m01s_002_.png
Send the same data E5 (hex) -229 (dec) ,with master emulator. At right side (Write Data) , 2 different settings at register size ( 1bit coils and 16bit registers).Error at response code.
(647.64 KiB) Downloaded 13709 times
ModBus_Slave_LCD_4-DEMO-FT1.fcfx
(13.18 KiB) Downloaded 400 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,

Please can you have a quick go with this and let us know how you get on.

I have changed the error function so it should now flash pin A0 three times if an error occurs this way you can tell the difference between an error and valid data.

I have also moved the binary value from outside the LCD data loop and provided a bit by bit conversion so we can compare if it is the multi bit functionality that is failing.
ModBus_Slave_LCD_4-DEMO-FT1.fcfx
(18.25 KiB) Downloaded 443 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 »

Dear Benj,
I made a quick test with the new code.
First I use the PLC demo code to send the byte E5(hex) . No data (leds off). At execute table I see CommError=254 . I think that there is error at response code.
With master emulator I write a value 255 ( FF ) to set on the first 5 leds . When I set register size at 16 bit registers only the first led (A.0) is on . When I set register size at 1bit coils all 5 leds are on. I noticed that at response code there is no data at coils quantity : 01 0F 00 00 (00 00) 55 CB .Data in brackets must be the quantity of coils .
In both tests there is no flashing at pin A.0 .The code partially works with master emulator but noway with PLC.

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 »

Dear Benj,
I made a test with another master emulator to confirm the response error at ModbusCommand 15. As it seems at response code, there is no data at quantity of coils . It must be 000d 005d . Above the request/response data , the test results shows 0 valid response instead of 1 , but also 0 error responses.The same response error I get at ModbusCommand 01 (Read Coils). Is it possible to check the component code to confirm that the response code is send correct ?
Attachments
Ashampoo_Snap_2017.04.11_00h27m14s_002_.png
Testing ModbusCommand 15 (WriteMultipleCoils) with ComTestPro.
(371.84 KiB) Downloaded 13697 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 »

Hi Benj,
Would you like to check the ModbusSlave component for some problems, as it seems at my previous posts ? I'm not sure if the response codes for commands 1 and 15 are correct, or there is a mistake in my FC7 code. For last tests I use the modified code (ModBus_Slave_LCD_4-DEMO-FT1.fcfx) that you send me with no results. Thanks.

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,

Sorry for the delay just having a look at this now for you.

The command byte 0x15 seems wrong, or my interpretation could be wrong.

I currently only support the following commands in the Modbus slave component.

0x01 - Read Coil Status
0x02 - Read Input Status
0x03 - Read Holding Registers
0x04 - Read Analogue Registers
0x05 - Force Single Coil
0x06 - Preset Single Register
0x0F - Force Multiple Coils
0x10 - Preset Multiple Registers

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 »

Hi Benj,
The command byte is 0F (WriteMultipleCoils or ForceMultipleCoils) and refers to Modbus Function 15. In some Modbus Master emulators you can select to see the data in decimal format, as it seems in my previous snaps .In the raw data you can see the real format, so the command is correct. Also there is no problem about command supporting .My problem is : when I send a byte from PLC with the above command to control the state of 8 leds at slave , I have no data at PIC I/O . Checking the communication data,. I noticed that there is wrong response data (Quantity of coils 00 00 instead of 00 08 ) .In your opinion where is the wrong , at component code or at my demo code? Is it possible to check at first the component code for commands 01 and 0F ? If that is correct , then we must take a look at demo code . Thanks.

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 »

Aha I was wondering what was with all the d's they mean decimal :D

Right I'll have another look and see if I can find anything. Obviously my head is not screwed on right today.

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 »

Right I think I have found the problem now.

Have a go with this updated component and let me know how you get on.

Simply copy the file into your "Flowcode 7/components" folder and restart Flowcode for the changes to be loaded.
ModbusSlave.fcpx
(91.53 KiB) Downloaded 426 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 for your help . I'll make some tests playing with Modbus Master code for PLC, and give you info about results.

cedlgy
Posts: 4
Joined: Mon May 01, 2017 7:59 pm
Contact:

Re: PIC AS MODBUS SLAVE

Post by cedlgy »

Hello i want to buy a new flowcode V7 but the MODBUS slave not working (i test before buy) with MODBUS POLL or another master plc i have no reponse for exemple from ATMEGA 328 P TQFP at 16MHz at the command read single holding register 40001 i download the lastest vs of component.

I want read and write holding register (8 write 8 read) at different adresse slave ID for eatch µc.

If your component MODBUS SLAVE working i buy the next vs of flowcode ( i am Professional licencied flowcode V6 ).

Thank you for your colaboration and your support

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 want to buy a new flowcode V7 but the MODBUS slave not working
It should be working fine, can you attach your program and can you give us a description of your hardware setup so we can try and determine what might be going wrong.

Have you tried the 1 second flasher test to confirm your clock speed is correct and micro is running as expected.

AVRs generally come with a /8 on the clock by default so the chip may be running 8 times slower then you think and maybe causing the problem.

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,
The response data is ok. The part ReadCoilStates of ModbusSlave component, works fine when I test it with a Modbus Master emulator. However not work when I send commands from PLC. Maybe the error is at PLC code (wrong register size or data type). To find where is the problem, I'll try to use another pic ( PIC16F887) , as master to make some tests. Is there some example code for Modbus Master with WriteMultipleCoils command to define some data details ? At help there is an example for Master but it refers to ForceSingleCoil.

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,
with modbus poll (i testing and validate the serial with the master modbus component its working correctly and µc it s ok )
You're not calling the CheckForIncoming macro in your program. This is the macro that listens for incoming messages from the master and automatically replies.

I would advise you to look at the example here to help get you started.
https://www.matrixtsl.com/wikiv7/index. ... s:_System)

dannyvanneijenhoff
Posts: 65
Joined: Thu Feb 07, 2008 7:59 am
Has thanked: 17 times
Been thanked: 10 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by dannyvanneijenhoff »

Hello Benj

This link is not working/correct:
I would advise you to look at the example here to help get you started.
https://www.matrixtsl.com/wikiv7/index. ... ms:_System
Attachments
Knipsel.JPG
Knipsel.JPG (36.4 KiB) Viewed 22738 times
Last edited by dannyvanneijenhoff on Sun May 07, 2017 4:27 pm, edited 2 times in total.

dannyvanneijenhoff
Posts: 65
Joined: Thu Feb 07, 2008 7:59 am
Has thanked: 17 times
Been thanked: 10 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by dannyvanneijenhoff »

I have the master working with an 16F887 with write single coils (not multiple coils). And the slave is responding correct.

But i have no working reading from the data which are received from the slave.

Perhaps we help each other. :mrgreen:
I have attached the program and added the links.

If i have this working with hardware, then i know that sending and receiving of the data is correct on my pcb. And can i move forward to test the slave component.

slave / master:
http://www.matrixtsl.com/mmforums/viewt ... 332#p81332

Another issue i have with master force multiple coils:
http://www.matrixtsl.com/mmforums/viewt ... 63&t=18640
Attachments
modbus master online 1 .fcfx
(21.66 KiB) Downloaded 244 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 dannyvanneijenhoff,
Try this link:
https://www.matrixtsl.com/wikiv7/index. ... dbus_Slave_(Comms:_System
There are many useful info for components to work with. About the tests for modbus code , if you want to see TX/RX data take a look at following master or slave emulators:
1. simply modbus master 8.0.5 , simply modbus slave 8.0.5 (http://www.simplymodbus.ca) - demo , no limits , needs only reset after a number of actions but you can do some work .
2. com test pro 2.0.5.1 (http://www.baseblock.com) ,only for modbus RTU , serial/ethernet ,diagnostics, multiple data format.
Observing data with DSO , is a good option , but it takes more time and hardware prepare than emulator . I prefer it when I must check waveform problems or data that I can't take with emulator.
About my problem , my demo code works fine with emulators , but no with a PLC that I've programmed as master . So , first I try to find errors at my demo code in a PIC16F1639 that works as slave . The think is that I don't know well PLC programming language.
When I finish the demo board hardware I'll post the schematic.For now the demo code.
Attachments
ModBus_Slave_LCD_4-DEMO-FT1.fcfx
(13.18 KiB) Downloaded 242 times

dannyvanneijenhoff
Posts: 65
Joined: Thu Feb 07, 2008 7:59 am
Has thanked: 17 times
Been thanked: 10 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by dannyvanneijenhoff »

Thanks, i will check this next week

I prefer when I check waveform problems or data with scope and hardware, programming is "new" for me :-)

Perhaps send your data with PC and USB/485 dongle, and a cheap 485 modbus slave (which i have enough in stock) for testing.
So that you have an working hardware master AND slave setup. And start from there.

You can send your email address in a pm.

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 dannyvanneijenhoff for info.
Send a pm to me with more details about modbs hardware testing, because I don't see a pm icon to your profile.

dannyvanneijenhoff
Posts: 65
Joined: Thu Feb 07, 2008 7:59 am
Has thanked: 17 times
Been thanked: 10 times
Contact:

Re: PIC AS MODBUS SLAVE

Post by dannyvanneijenhoff »

done, and also updated my profile (a little bit late :oops:

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 »

Dear Benj,
The demo code works fine so far.These days I make some tests to control I/O data. Working about transfer characters to LCD 4X20, I noticed that modbus command 16 sends data in 2 pairs (hi-low)hex number. As it seems in the snap I send 5 characters that use only low side of data. To consume memory , how can I use high side to send another character , so to store 2 characters in each memory byte and send them in pairs? In that case I think we can use 40 memory bytes instead of 80. Below I post the demo code.Many thanks in advance.
Attachments
ModBus_Slave_LCD_4-DEMO-FT1 (3).fcfx
(15.99 KiB) Downloaded 204 times
Ashampoo_Snap_2017.06.09_02h07m31s_001_.png
(43.88 KiB) Downloaded 11861 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,

Yes the data is 16-bits per location. To use all 16-bits you would have to take the two ASCII bytes and pack then into a single value by doing something like this.

Code: Select all

wordvalue = bytesval0 + ( byteval1 << 8 )
So you store one byte in the lower 8 bits and the other byte in the upper 8 bits. The << 8 takes the 8 bit byte and shifts it up 8 bits to the upper 8 bits of the 16-bit value.

To unpack at the other end you would have to do something like this.

Code: Select all

byte0 = wordvalue
byte1 = wordvalue >> 8
<< 8 is the same as multiplying by 256
>> 8 is the same as dividing by 256

However the bit shift is likely to be more efficient in terms of processor cycles then the multiplication or division functions.

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 »

Dear Benj,
Thanks for the info.I made some tests with no result. Especially for the master emulator that I use to test the slave code, there is no way to make a pack of 2 bytes as it seems in your post. The master emulator sends a word of 2x 8bit bytes (I checked), and I can send the pack in 2 forms, first the high order byte(8bit) or first the low order byte. So , I think that the problem is to read the word at slave code . Its only a thought , maybe I'm wrong, but I can't use C code to the master code . The PLC that will work as master sends a 16bit word ,I can't edit that code cause I have no knowledge about ladder(PLC programming language).For that, it's preferable to make changes in slave code .So, is it possible to send me a modified code of slave demo that I post below, and a example code for master , in case that there is no way to solve the problem with editing only the slave code.
Attachments
ModBus_Slave_LCD_4-DEMO-FT1 (1).fcfx
(25.98 KiB) Downloaded 215 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,
I made a modification to demo code , so now the slave code reads both of 2 bytes and displays at LCD 2 characters for 1x16bit memory location .Below is the code as modified so far. Thanks Benj for your very important help!
Attachments
ModBus_Slave_LCD_4-DEMO-FT1 (4).fcfx
(26.54 KiB) Downloaded 204 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,
I try to make some modifications on hardware of demo board with PIC16F1939. I change the cpu with PIC18F458, but the compiler gives me error 1 , and demo code ( Modbus_Slave_LCD_4-DEMO-FT1(4).fcfx ) not work. What modifications at code of configuration settings must be done to use PIC18F instead of PIC16F?

Post Reply