View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
Modbus interfacing with PC |
Posted: Mon Mar 26, 2018 9:56 pm |
|
|
Hi,
I am using CCS C Compiler V4.114. PIC18F2520. I want to send a data to HMI through modbus. Since it is my first project with Modbus, I want to test it with PC before interfacing with the HMI. Any sample code is available? Please share. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Mar 27, 2018 12:32 am |
|
|
There are Modbus examples with the compiler.
First thing you need to know is what actual interface you are going to use?
Modbus is a 'protocol' standard (actually about nine different protocols under a 'wrapper' name). It then has to be sent over a physical transport medium (normally serial or Ethernet).
Now the examples support Modbus ASCII or Modbus RTU over normal serial links, which is probably the commonest form. There is also a basic Modbus TCP driver included. So assuming that you are probably going to be working with Modbus ASCII as a starting point at least, go and look at ex_modbus_master.c and ex_modbus_slave.c which are the core examples. You can use these with a PC first and a basic modbus driver there to start to understand the abilities. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Mar 27, 2018 6:28 am |
|
|
Mr T is 100%. Get the CCS supplied examples working BEFORE you try any other code! Once you've got familiar with it, THEN copy the drivers renaming them to say 'yourname_xxxyyyzzz' and modify the COPIES as required.When you make several changes, save as a new version.
That way when the copies don't work, you can go back to the original versions and try again.
Since harddrives have terrabyte of storage these days, it's OK to have 30-40-50 versions of code. |
|
|
Dutch Guy
Joined: 20 Mar 2018 Posts: 21 Location: University of Antwerp
|
|
Posted: Wed Mar 28, 2018 6:27 am |
|
|
I just wrestled through the example modbus slave and got my pic modbus PLC working with 32 inputs and outputs.
A great tool for checking data while debugging is Realterm. Once your RS485 cables are correctly connected the ex_slave_modbus communicates easily.
I'm sending commands with a Weintek HMI while debugging and checking modbus.rx struct in the watch window.
Once this reads out correct you can step easily trough the modbus.FUNC switch statement.
I made 4 element arrays of inputs and coils defines.
In the function switch statement you could alter the error checker for accepting a wider bit range and make an array of data fields for the bit functions 1,2,5.
But first make sure data comes in directly in the modbus.rx struct then i`ll try to get you as far as I am.
Also I hope it won't take long before i can upload my code but I guess when I share in the stitch and patch way as it is now, I'll get some dirty looks from some purists. ;)
Keep me informed _________________ I just can`t get it all in my head.... But wait, there is a new hole opening up.... |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Tue Apr 24, 2018 2:47 am |
|
|
Thanks for the valuable comments.
below is the example program from ccs.
Code: | /////////////////////////////////////////////////////////////////////////
//// ex_modbus_slave.c ////
//// ////
//// This is a simple test program for a modbus slave device. ////
/////////////////////////////////////////////////////////////////////////
|
On the PC side, Can i Use docklight scripting to view the data? what is the output for the program. Please help.
++++++++++++++++++++++++++++
Code deleted.
Reason: Forum Rule #10
10. Don't post the CCS example code or drivers, or ask for such code and drivers.
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
- Forum Moderator
++++++++++++++++++++++++++++ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Apr 24, 2018 3:00 am |
|
|
What you have posted, is a _slave_ not a master. It expects to receive commands from the master to write into the holding registers or input registers. It doesn't send anything (except the acknowledgements for what it is receiving). |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Tue Apr 24, 2018 4:22 am |
|
|
Thanks for the clarification.
What command should I send from my master(Docklight scripting in PC). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Apr 24, 2018 6:31 am |
|
|
If you send a 'write holding register', with a particular value to a particular register, you can then send a 'read holding register' to the same register number, and should get the value you have written 'back'. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Apr 24, 2018 2:04 pm |
|
|
isnt it "illegal" to post CCS supplied drivers here?
++++++++++++++++++++++++
Code is now deleted. Thanks.
- Forum Moderator.
++++++++++++++++++++++++ _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Wed May 02, 2018 3:39 am |
|
|
Code: | void main()
{
setup_adc_ports(NO_ANALOGS);
printf("test\n\r");
delay_ms(500);
modbus_init();
printf("Initializing\n\r");
delay_ms(500);
while(TRUE)
{
while(!modbus_kbhit());
delay_us(50);
//check address against our address, 0 is broadcast
if((modbus_rx.address == MODBUS_ADDRESS) || modbus_rx.address == 0)
{
printf("loop\n\r");
delay_ms(500);
// how to send data to PC
}
}
} |
How shall i send write the data to a register and send it to PC. Please help |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
How to test the program |
Posted: Wed May 02, 2018 9:43 pm |
|
|
My hardware has 18F2520, max232 and DB9 connector to PC. Can I test the program without MAX485 IC? Please help. |
|
|
Dutch Guy
Joined: 20 Mar 2018 Posts: 21 Location: University of Antwerp
|
|
Posted: Thu May 03, 2018 3:31 am |
|
|
Hemnath
You must not be using the driver like that.
When data comes in the serial buffer the rx.struct (received data) will be analyzed by the driver and this will decide to do something accordingly the modbus commands in the rx.function. (look at the function switches).
Every case will do something in the driver.
If you want to send text, actually you are not using MODBUS anymore but plain old RS232 on RS485 levels.
Modbus is meant to set bits and bytes of outputs. The driver has already fixed actions built-in. But you CAN invent a new function code in that driver switch.
Carefully analyze the driver flow and try to understand it. The "empty" function case definitions are in fact an OR statement with the next non empty function definitions.
Later on the function variable is checked again for executing.
Use pen and paper for a helping flowchart.
It is essential to do this before adding more code.
If you come from a microcontroller you probably have a 0-5V clock line and a 0-5V data line. It is called RS-TTL.
The MAX232 will drive 0-5 V to a -5-+5V level and uses timing to eliminate your serial clock line data and clock combined (stating a baudrate and protocol like 9600,8,N,1).
A 485 chip will drive also to a symmetrical higher potential and wraps the transmission lines in a straight/inverted pair for noise elimination.
Your PC has a RS232 -+5V DB9 so you need conversion to TTL for your PIC.
If you are sure you need modbus learn to understand the driver and functions...FIRST (step a valid case through). If you can't then you probably should stay out of the driver.
Otherwise USE RS232 will do for ASCII (RS485 is just a hardware change). _________________ I just can`t get it all in my head.... But wait, there is a new hole opening up.... |
|
|
|