Modbus Slave

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Modbus Slave

Post by Lord Grezington »

Hello

I have a test program running Modbus slave, and it seems to be working ok reading registers. However its a little odd and perhaps it just needs a little explanation.

I have set holding registers with addresses 1 through to 5 with some values. When I read the registers I get the correct responce but always the register address above the one I have requested. Ie, if I request the data from register 1, I get the data stored in register 2. This carries on for all regesters up to 5. This means I am missing register 1.

I get this if I read jsut one register at a time all all 5 resisters, see below:
Modbus issue.jpg
Modbus issue.jpg (49.27 KiB) Viewed 4715 times
I have also attached hte slave code.

Thanks
Attachments
Modbus test.fcfx
(10.98 KiB) Downloaded 244 times

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: Modbus Slave

Post by Lord Grezington »

Hello

Also found another potential issue...

When I select the RS485 bus and select the data directional pin I get compiler errors (and select the pin A10). This is the only change I make before I get any compile errors.

http://www.matrixtsl.com
Launching the compiler...
C:\Program Files (x86)\Flowcode\Common\Compilers\pic16\batchfiles\pic16_C30_comp.bat "C:\Users\pc\ZIKODR~1\ZIKODR~1\QUALIT~1\SPECIF~1\ALLSPE~1\PROGRA~1\VERSIO~2\ZDBL45LS3.11" "C:\Users\pc\ZIKODR~1\ZIKODR~1\QUALIT~1\SPECIF~1\ALLSPE~1\PROGRA~1\VERSIO~2\" "33EP32MC504"

C:\Users\pc\QUALIT~1\SPECIF~1\ALLSPE~1\PROGRA~1\VERSIO~2>xc16-gcc -c -mcpu="33EP32MC504" -omf=coff -funsigned-char -fno-short-double -Os -I"C:\PROGRA~2\Flowcode\Common\COMPIL~1\pic16\BATCHF~1\..\support\h" -I"C:\PROGRA~2\Flowcode\Common\COMPIL~1\pic16\BATCHF~1\" -std=gnu99 "C:\Users\pc\QUALIT~1\SPECIF~1\ALLSPE~1\PROGRA~1\VERSIO~2\ZDBL45LS3.11".c -o "C:\Users\pc\ZIKODR~1\ZIKODR~1\QUALIT~1\SPECIF~1\ALLSPE~1\PROGRA~1\VERSIO~2\LS3.11".o
Options have been disabled due to restricted license
Visit http://www.microchip.com/ to purchase a new key.
C:\Users\pc\L45LS3.11.c: In function 'FCD_04c01_ModbusSlave1__CheckForIncoming':
C:\Users\pc\LS3.11.c:2912:12: error: 'U3STAbits' undeclared (first use in this function)
C:\Users\pc\LS3.11.c:2912:12: note: each undeclared identifier is reported only once for each function it appears in
C:\Users\pc\LS3.11.c:2926:12: error: 'U4STAbits' undeclared (first use in this function)

Error returned from [xc16-gcc.exe]

C:\Program Files (x86)\Flowcode\Common\Compilers\pic16\batchfiles\pic16_C30_comp.bat reported error code 1



FINISHED


Thanks

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: Modbus Slave

Post by Lord Grezington »

Hello Again...

I apologise for the multiple questions, however I am still learning MODbus and need to know if what I am seeing should be correct.

Here we have the master (PC software) writing a 0 to to the coil in address 1, and the PIC replies OK with a duplicate of the of the transmitted message.

But then, when the master sends a 1 to the coil in address 1, the PIC still responds with the saying the coil is empty (see below).
Modbus writw single coil.jpg
Modbus writw single coil.jpg (39.83 KiB) Viewed 4696 times
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: Modbus Slave

Post by Benj »

Hello,
I have set holding registers with addresses 1 through to 5 with some values. When I read the registers I get the correct responce but always the register address above the one I have requested. Ie, if I request the data from register 1, I get the data stored in register 2. This carries on for all regesters up to 5. This means I am missing register 1.
Modbus addresses are indexed from 0. i.e. address 0 is register 1. Hope this helps to explain things.

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: Modbus Slave

Post by Lord Grezington »

Hi Ben

Yeah, I knew this. I was just hoping to keep the 0x00 registers blank and start from 1. The problem I am having is that if I request the data from register 1, I get the data from register 2. And if I request the data from register 2 I get the data from register 3.

The data I have stored in the slave holding regisers on the PIC are:

Register 1 = 12
Register 2 = 23
Register 3 = 34
Register 4 = 45
Register 5 = 56

For the Modbus issue.jpg below, on the first line we have:

Sent from PC

1C = Slave address
03 = Function code - Read holding registers
00 01 = Read start register
00 01 = Number of read registers
D6 47 = CRC

I then get a responce on line 2 from the PIC

1C = Slave Address
02 = Byte Count
00 17 = Register Values
14 48 = CRC
Modbus issue.jpg
Modbus issue.jpg (49.27 KiB) Viewed 4689 times
So, when I read register 1 I ahve getting the 0x17 (23) which I had stored in Register 2. This continues when I attempt to read all the registers.

Thanks

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: Modbus Slave

Post by Lord Grezington »

Hello

I'm the second post above I have compiling issues when I use the RS485 bus and set the data direction pin.

I am using the dsPIC33EP32MC504, does anyone have any advice on getting this working? I have filled in all the component proporties.

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: Modbus Slave

Post by Benj »

Hello,

Thanks for letting us know of the problem.

I have now managed to track down and fix the issue and the latest fixed files are now available via the Help -> check for updates menu.

Let us know how you get on.

Lord Grezington
Flowcode V4 User
Posts: 288
Joined: Wed Nov 02, 2011 11:15 pm
Has thanked: 29 times
Been thanked: 30 times
Contact:

Re: Modbus Slave

Post by Lord Grezington »

Great, thanks Ben

Worked first time!!

Many thanks

Post Reply