Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Modbus slave number

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Fri Nov 17, 2017 1:04 pm    Post subject: Modbus slave number Reply with quote

Hi,

I would like to send some modbus frame from advantech panel to ATMega8 in modbus, rs 485. Panel is a master, avr is a slave. First problem i encountered is that i don't know how to set up a slave number for avr? Let say, on a panel i have a button and by pressing it i would like to send a some number, i.e "1" to avr. On a panel i've configured already connection chanel and slave number should be = "11". So how to do it on ATMega8 side?

Thank you,
Martin

[b][color=red](BASCOM-AVR version : 2.0.7.6 , Latest : 2.0.7.8 )[/b][/color]
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Nov 18, 2017 9:04 am    Post subject: Reply with quote

Hi,

The modbus data packet is just an Array of Bytes.

In the main Loop I have

Code:
 
  If Modbus_have_data > 0 Then                             'Have we got a modbus packet
      Call Modbus_task()                                    'Handle Modbus packet
      Modbus_have_data = 0                                  'Clear flag
   End if
 


Note Modbus_have_data is set to 1 (By a timer interrupt) when data arrives at the Serial port then nothing for a short day (approx 1ms)

Code:

Sub Modbus_task()
'Function    : Check modbus packets calling the correct decoder (read/write) .
'Author      : Ian Dobson
'Inputs      : None
'Outputs     : None
'Limitations :       :
'Date        : 13.2.2016
   If Modbus_buffer(1) = _modbus_slave_address Then         'Slave address correct
      Modbus_temp1 = Modbus_have_data - 2                   'Low Byte
      Modbus_temp2 = Modbus_have_data - 1                   'High Byte
      Modbus_checksum_serial = Makeint(modbus_buffer(modbus_temp1) , Modbus_buffer(modbus_temp2))
      Modbus_temp1 = Modbus_have_data - 3
      Modbus_checksum_user = Crcmb(modbus_buffer(1) , Modbus_temp1 )       'checksum from data
      If Modbus_checksum_user = Modbus_checksum_serial Then 'checksum OK?
         Call Debug_modbus(modbus_have_data -1 , "I")
         Register_start = Makeint(modbus_buffer(4) , Modbus_buffer(3))       'get start register
         Incr Register_start
         Modbus_register_count = Makeint(modbus_buffer(6) , Modbus_buffer(5))       'get register count
         Select Case Modbus_buffer(2)
            Case Is = 3                                     'Read Holding Registers
               Call Modbus_fc3()
            Case Is = 16                                    'Preset Multiple Registers
               Call Modbus_fc16()
         End Select
         If Modbus_watchdog_counter < 3 Then                'no comms for ca 3 seconds
            Toggle Led_status
         End If
      End If
   End If
End Sub
 


Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Sat Nov 18, 2017 6:55 pm    Post subject: Reply with quote

Thank you for an idea hot to respond when modbus frame arrives. I was a little way-out in this subject because i thought that slave number should be written somewhere... Now i understand.
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Nov 18, 2017 7:03 pm    Post subject: Reply with quote

Hi,

Here are my comments for my modbus slave. Note the slave address is the first Byte in the Array.

'The following functions are supported
'FC3 read multiple registers
'FC16 write mutliple registers
'
'Master to slave data packet- read registers
'11 - Slave Address
'03 - Function code
'006B - The Data Address of the first register requested
'0003 - The total number of registers requested
'7687 - Checksum
'Response
'11 - Slave Address
'03 - Function code
'06 - The number of data bytes to follow
'AE41 - Value of register 1
'5652 - Value of register 2
'4340 - Value of register 3
'49AD - Checksum
'
'
'Master to slave data packet- write register
'11 - Slave Address
'06 - Function code
'006B - The Data Address of the register
'0003 - The value to write
'7687 - Checksum
'Response
'11 - Slave Address
'06 - Function code
'006B - The Data Address of the register
'0003 - The value to written
'7687 - Checksum

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Sat Nov 25, 2017 7:32 pm    Post subject: Reply with quote

Finally i've done. Now i know how to set an address of device (AVR as a slave) an the rest is to take apart the frame. Piece of cake Wink
Thank you.
Thread to close.
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Tue Nov 28, 2017 8:23 pm    Post subject: Reply with quote

Unfortunatelly I was a bit to fast saying OK. Wink Problem I have concerns the respond that slave should send to master. I catch request in URxC interrupt. That works so far. But i send response in such way:

Code:

do
 if is_request=1 then
  waitms 20
  Printbin A(1);8      'A is an array of bytes. Array is Ok (with modbus crc) i've checked in rs485 sniffer
 end if
loop
 


and the panel master says "no response". If in above code is no "waitms 20", then my atmega8 works fine, but for other devices panel master raises an timeout error. So i came to an conclusion that response in such way is bad, but how it should be done?
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Tue Nov 28, 2017 9:15 pm    Post subject: Reply with quote

Hi,

When do you reset is_request?

Maybe try

Code:

do
 if is_request=1 then
  is_request=0
  Printbin A(1);8      'A is an array of bytes. Array is Ok (with modbus crc) i've checked in rs485 sniffer
 end if
Loop
 


Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Tue Nov 28, 2017 10:42 pm    Post subject: Reply with quote

Hi,

is_request is reset in URxC after receiving all bytes of the master's frame, but i've also tested your solution. Eventually came for an idea to switch off communication error in panel master (Advantech product). So now on the lcd of atmega8 i have what i want and on panel's side there is no communication errors but all buttons, edits, etc are functioning. I will do short movie on youtube tomorrow.

Regards,

Martin
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Wed Nov 29, 2017 5:58 pm    Post subject: Reply with quote

Hi
thre is short movie:
https://www.youtube.com/watch?v=COIERydC7ys&feature=youtu.be

Regards,

Martin
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1570
Location: Basel, Switzerland

switzerland.gif
PostPosted: Thu Nov 30, 2017 7:12 pm    Post subject: Reply with quote

Hi,

modbus works in there the master sends a packet to a slave and the slave answers with a packet. The slave should only "talk" when they're spoken too by the master.

With your solution I have the Feeling that the slave (your Atmel) is just sending packets to the master even with being asked to. Adding that short delay (20ms) gives the master/other slaves time to communicate.

Regards
Ian Dobson

_________________
Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile
krolikbest

Bascom Member



Joined: 02 Jan 2017
Posts: 112

poland.gif
PostPosted: Thu Nov 30, 2017 7:37 pm    Post subject: Reply with quote

Hi,

right, 20ms is time to work for other devices. even i gave later ~100ms and it is sill working well. In this little example are working 3 slave devices + panel master.

Regrads,

Martin
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum