(Modbus) How to assign coil address as ports in PIC?


Thread Starter


I am doing a project of controlling a relay that will control another motor using modbus. A computer will be the master and a PIC16F877 connected using RS232 as the slave. The circuit is a lot similar to that of modbus.pl by Andrei (memory, max232)

My question is, how to assign modbus address of coils to a specific port in Microchip PIC? I have no idea of doing it. I am using PIC16F877 with the compiler PIC C.

This is like say, when I used function 05 which is to force a single coil to close or open, it will actually affect the relay. Any ideas?

Thanks in advance guys.

curt wuollet

Look at how the reference code on Modbus.org works to receive a request. You will need to implement something like it to receive requests and sort out which coil needs actuation. Then you will figure out how that is done on the PIC and when the request comes in, wiggle the pin. You will probably need to fake just enough to do what you want and ignore other requests as the whole ball of wax might be kinda large for a PIC depending which you have. And yes, it's non-trivial.


if you still don’t have the answer yet...i can help you out. i'm developing a modbus communication between a DSpic and a PLC, the functions that i have implemented are, FC01, FC02, FC03, FC04, FC05, FC06, FC08, FC0B, FC0F, FC10 ...here is my mail where you can contact me... apusfigueroa [at] gmail.com

by the way my name is carlos and i'm from colombia...good luck!
Dear cwww,

thanks for the reply. It always bugs me when I see in other forums that they only touch the memory part. They send signal from and to the PIC and read whether they are correct or not. But actually, the pins is the one that will control any input that you want to capture or control any output. But little explanation is done on this part.

I guess I have just to do trial and error to assign specific address functions only to the specific pins?

To Carlos, I have sent an email to you. Hope to hear from you soon :)

Bruce Durdle

If you are writing the software for a Modbus slave, you can do what you like to allocate I/O pins to internal memory bit or byte addresses. You can also do what you like to allocate Modbus addresses to internal memory or I/O.

In my Modbus slave routine, I assign an array of internal memory addresses as "holding registers", "input registers" (loaded from A/D readings), and likewise for internal bit strings and input bit strings (derived from input ports). It is not always a good idea to allocate Modbus addresses directly to I/O addresses as some processing allows isolation. Analogue values read in from the A/D converter need to be buffered into memory anyway.

Dear Bruce,

is it memory from the PIC itself or external memory? Is it enough just by using the PIC's internal memory?

Bruce Durdle

Whether or not you can use memory from the PIC depends on what you want to do, and what model PIC you are using. But since you only have a few A/D channels available in a PIC, and each needs 2 bytes of memory, you can usually manage to get the Modbus buffers into the PIC. With the 16F/18F types I am using, you have only 5 I/O ports so that's 5 bytes for discrete inputs directly connected to the ports. I'm managing to run a dynamic simulation of a heat exchanger with reasonably heavy calculations and all data accessible via Modbus in a 16F877.

Wow, ok I see. I learned from several people including you and Carlos that we don't need the extra memory.

But how to eventually see that the PIC's memory is bottle necking?

Bruce Durdle

The easiest way to see how your memory is being used is to draw up a memory map. With the banked PICs this is useful anyway as it helps you keep control of the banks, and also the overlaps between SFRs.
sir i've been given task to generate code to for pic microcontroller to communicate with sensors to read write the values in it using modbus rtu. sir please help no. as you've already worked on it please help me sir. i couldnt understand the registers and also writing or reading to the regisers. sir please if you could help?