Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Modbus Protocol

Status
Not open for further replies.

pakhi

New Member
Hi,

i want to communicate between PC & microcontroller through modbus protocol in RTU mode... i'm facing lots of problem in that...
1. Is it possible to communicate between PC & MC through RS232 or RS485 or their is some other driver to implement this(RTU mode)??
2. What does 3.5 character time means in this .....
3. i'm writing code in C using keil comipler in which we use variables to store data in place of registers.... so,what shoud i write while giving starting address for registers???

thanx :)
 
Hi,

I worked with modbus ASCII before so I assume that the RTU is very similar. I think that the main differences between the 2 are in the message framing.

Before writing your own code, have you tried:
**broken link removed**

Let me know if above website does what you need as I have only worked on modbus integration between PC and industrial controllers.


1. RS232 would be fine in communicating between PC and uC as long it is over a short distance. If you need to use RS485 then you can still use RS232/RS485 converters.

2. 3.5 character time. Correct me if I'm wrong but I think that 3.5 character means the start and end of the message should be each 28 bits long. So if you send multiple messages to RTU, then in between each message there will be a pause of 3.5 characters that will indicate the end of a previous message and the start of the current one.

3. For example, if a register is called 40105, then its address is 104 (ignore first 2 characters, and subtract one from last 3 characters). Anyway this 'subtracting' is important, as it gave me some headaches, but I am still not sure what you are asking. When you say "write" do you mean "you writing your source code" or "you writing the register location"?
 
Hi,

ya,u are right that this 3.5character time is the indication for the start &
end of the message format in modbus rtu mode.The entire msg. must be transmitted in a continous stream & their must not be a gap of more than 1.5 charater time before completion of the msg. if it does then this indicates that their must be an error in the msg..

this 3.5 character time is how much in millisecond or microsecond & how to calculate it ???

"write" here means writing a source code in C using keil compiler

thanx.
 
To calculate character time:

time_in_msec=3.5*8*1/baud rate*1000

where units are: 3.5 char
8 bits/char (remember 'char' and an 8 bit 'int' are same)
baud rate is in bps
1000 is amultipler to get bits per second.

But the abpove calculation isn't true because you must account for all overhead bits (stop/start, parity) as well as data bits. So to refer to Modbus protocol guide (PI_MBUS_300.pdf; page 13; Figure 6), you can see that RTU character framing has 11 bits.

So the actual calcualation is:

time_in_msec=3.5*11*1/baud rate*1000

Likewise you can also figure out how long 1.5 char is.


As for writing the source code, you could define the starting data memory address in the pic that you will use for register locations. Instead of variables, I would recommend using an array to store the register locations. That way you can use array arithmetic to easily access required registers as opposed to using variables. The first element of this array should originate at some specific PIC address. You could use PICs general purpose registers. Maybe somebody else on this forum might suggest a location where you can place your array.
 
hello,

can anybody tell me what are the advantage of this modbus....
except greater connectivity.....

if i'm communicating b/w PC & microcontroller,where PC is acting as a master & controller as a slave than i must have to write the routine for query & response both if i'm not wrong.....
while writing query the 1st field is slave address,2nd is function code,next is the data field where starting address of register is shown than last 2 byte is for CRC checking.So,i'm confused here that where i should write the data which i want to write or read in the register....
should i write this data while sending Response....

thanx,
 
You are right. Greater connectivity is definately an advantage of Modbus as almost any industrial device supports it. But I think that its main advantage is that it is old and thus well tested and documented. Plus its newer versions, Modbus Plus and TCP/IP, allow more flexibility in control systems. You can find plenty information about Modbus on the internet.


Now to your problem.


Master needs both query and response algotirthms. But slave needs only to answer. Are you using Modbus or are you creating your own protocol?

Your question is not completely clear to me, but here comes the answer anyway while refering to Modbus.:D

Suppose that a master sends a message to a slave. The message looks something like this:

Header (e.g. :01B1F5.... the header is a colon as you see it in a hex file)
Slave address (e.g. traditionally modbus supported 0-255 devices)
Function (e.g. read/write register, set/reset bits etc.)
Data:
Starting Register Address (High Byte)
Starting Register Address (Low Byte)
Number of Registers to access (high byte)
Number of Registers to access (Low byte)
CRC check
3.5 character time (e.g. the end of message indicating a new message can begin)

So suppose you want to read contents of 4 registers starting from register 0201 decimal (00C9 hex).
In Modbus Data field you would write:
Starting Register Address (High Byte)=00
Starting Register Address (Low Byte)=C9
Number of Registers to access (high byte)=00
Number of Registers to access (Low byte)=04

When a slave receives this message it will read contents of 4 registers from 0202 to 0205. Note that with Modbus convention when you specify register 0201, Modbus will read 0202. (It always increments register address once).

To answer your last question:

When the slave responds, it will send function code back, number of bytes and data (high and Low bytes) of each register back to the master.
So in this case, it will send 8 high and low data bytes from 4 registers.

My experience is based on Modbus and I would recommend you use it as a guideline or when you get stuck.
 
ya,i'm writing source code in Embedded C for modbus in RTU mode where i'm communicating between PC & microcontroller.where PC is acting as a master & controllers as a slave.although i'm now,well known about modbus but still one dout is creeping in my mind - that the starting address where my data contents are kept should be the address of internal RAM of microcontroller or some other address.According to me it should be Internal RAM of microcontroller,is i'm right??

thanx.
 
I am not sure about that kelley. I worked with industrial controllers which had registers within their ROM space.
If you can ensure that you can always allocate that amount of RAM at that RAM address then you'll be fine.

Why not just use external memory module?
 
Thanx Pop,

for helping me in solving my douts....
In my project i'm working on AT89C2051 IC & not using any external memory so i'm using internal RAM only & secondly my address is also not changing.I want to explain u wht i had understood & doing,if i'm wrong than do plz. correct me .....
i have written the prog for sending query in which after checking each character through crc method this 16-bit crc gets added in that query atlast of this msg structre.than after getting query i'm now constructing response in which firstly i'm checking each character of query & after calculating crc of this i'm comparing this calculated crc with that of query(crc).if all is fine than response is send to master thru slave othrwise error response is send.is this okay???
1. should i give error after chking whole query or whereever i found mismatch at tht point only i should send error response......
2. should there be any limitation for starting address???
3. code for reading a bit or register should be same??
4. code for writing a bit or register should be same??

thanx.
 
hi

i have made the code for modbus using function 01/02
but while executing it in modtest.exe(simulator) i'm facing some problem...
1. how to check tht whter slave is reciving the command given by master
2. at times it slave is sending some response but tht to in ascii format & also the response which is coming is also not the one expected.
But although its working fine in Hyper terminal so why its not working in this ... Plz help me to sort out this probl. & also tell me abt anyother free modbus simulator to test my code.

thanx,
pakhi
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top