CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

[working with hold_regs, and modbus rs485] problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arthurlmiranda



Joined: 19 Oct 2012
Posts: 2

View user's profile Send private message

[working with hold_regs, and modbus rs485] problem
PostPosted: Fri Oct 19, 2012 8:55 am     Reply with quote

Hello guys, this is my first post here.
I'm doing a simple test with modbus rs485. I have a button in a slave that I press it and the slave counts and the master should read this number...
My problem is the master get the right data from slave, I don't know if i have to use holding registers or input regs.
The sample ex_modbus_slave.c use some holding regs but I can't compile with them for example if I use 0x8800 i get this error (n#45) subscript out of range.
My ccs version IDE 4.078.
I'm simulating this on proteus.
My simulation with the slave address is working fine, the slave undertands that the message is for it.
Probably I won't have access to internet until monday morning.
Sorry for bad English, I'm from brazil.


Masters Code

Code:

#include <18F4550.h>
 

#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20M)   

#define MODBUS_TYPE MODBUS_TYPE_MASTER                    //  MODBUS_TYPE_MASTER or MODBUS_TYPE_SLAVE     
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT          //interrupcao externa mas nao estamos usando os pinos que tem interrupcao                     
#define MODBUS_SERIAL_BAUD 9600                         //Valid baud rate for serial                 
#define MODBUS_SERIAL_RX_PIN PIN_c6                    //Valid pin for serial receive               
#define MODBUS_SERIAL_TX_PIN PIN_c7                   //Valid pin for serial transmit     
                       
#define MODBUS_SERIAL_ENABLE_PIN PIN_c4              //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5              //Valid pin for serial rcv enable, rs485 only   

                                               
#define MODBUS_SERAIL_RX_BUFFER_SIZE 64            //Size of the receive buffer

#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA  //Tipo de interrupção(assincrono) pro rs
                                           
#include "modbus.c"
                                                                       

#define MODBUS_SLAVE_01_ADDRESS 0xF5
#define MODBUS_SLAVE_02_ADDRESS 0xF6                                                 
                                             
   
                                       
   
void read_all_holding()
{
   if(!(modbus_read_holding_registers(0xF5,0x0001,8)))  // should I use 0 or 8?
   {   
   int i; 
      printf("Data: ");
      /*Started at 1 since 0 is quantity of coils*/
      for(i=1; i < (modbus_rx.len); ++i)
         printf("%X ", modbus_rx.data[i]);
      printf("\r\n\r\n");
   }                                                                 
   else
   {
      printf("<-**Exception %X**->\r\n\r\n", modbus_rx.error);
   }   
 
}   
   
void read_all_input_reg()
{                                                                                         
   printf("Input Registers:\r\n");
   if(!(modbus_read_input_registers(0xF5,0x0003,8))) // should I use 0 or 8?
   {   
   int i; 
      printf("Data: ");
      /*Started at 1 since 0 is quantity of coils*/
      for(i=1; i < (modbus_rx.len); ++i)
        printf("%X ", modbus_rx.data[i]);                               
      printf("\r\n\r\n");
   }
   else
   {                                                                   
      printf("<-**Exception %X**->\r\n\r\n", modbus_rx.error);
   }
}   
                                           

void main(void)                                                 
{                                               

int i; 
   
while(1)
{
//  2 test to receive the data. Using the sample from ex_modbus_master
printf("\rreading holding");
 read_all_holding();
 delay_ms(1800);   
read_all_input_reg();
printf("\rreading input reg"); 
delay_ms(1800);   
// 2 test to receive the data. Using the samples from modbus.c
 modbus_read_holding_registers(0xF5,0x0001,8);// should I use 0 or 8? 
 printf("\rreading holding");                 
 delay_ms(1800);
 modbus_read_input_registers(0xF5,0x0003,8); // should I use 0 or 8? 
 printf("\rreading input reg"); 
 delay_ms(1800);   
   
   
                           
} //whie
}//main

                                               


Slave
Code:

#include <18F4550.h>


#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20M)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, stream=PIC, errors)

#define MODBUS_TYPE MODBUS_TYPE_SLAVE                     //  MODBUS_TYPE_MASTER or MODBUS_TYPE_SLAVE     
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT          //Source of interrupts    (MODBUS_INT_EXT,MODBUS_INT_RDA,MODBUS_INT_RDA2)                         
#define MODBUS_SERIAL_BAUD 9600                         //Valid baud rate for serial
#define MODBUS_SERIAL_RX_PIN PIN_c6                    //Valid pin for serial receive               
#define MODBUS_SERIAL_TX_PIN PIN_c7                   //Valid pin for serial transmit     

#define MODBUS_SERIAL_ENABLE_PIN PIN_c4              //Valid pin for serial enable, rs485 only
#define MODBUS_SERIAL_RX_ENABLE PIN_c5              //Valid pin for serial rcv enable, rs485 only   
 
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA  //Tipo de interrupção(assincrono) pro rs
#define MODBUS_SERAIL_RX_BUFFER_SIZE 64            //Size of the receive buffer
                                 

#include "modbus.c"                   
#define MODBUS_ADDRESS 0xF5                                             

void main(void)                       
{
                                                                 


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                   
   int8 coils = 0b00000101;
   int8 inputs = 0b00001001;
   int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x0001};
   int16 input_regs[] = {0x0003, 0x0004};
   int16 event_count = 0;
                                                                             
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                 
int8 *data;
int i=0;                 
modbus_init();                             
printf ("slave 0xF5") ;

while(TRUE)
{       
                                 

while(!modbus_kbhit())
{
   if((modbus_rx.address == MODBUS_ADDRESS))
   {
     
      }
      if(input(pin_D4)==1){     
                                   
      i++;
      data=i; 
      hold_regs[0x8800]=data;// test to use with read holding registers
      input_regs[0x0003]= data;// test to use with read input registers
      printf("\r button pressed %ld times ",data);   
      delay_ms(1000);
      if(modbus_rx.func == FUNC_READ_HOLDING_REGISTERS)
                     modbus_read_holding_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),hold_regs+modbus_rx.data[1]);
                  else
                     modbus_read_input_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),input_regs+modbus_rx.data[1]);
{                                                   
      }                                               
                                                     
                           
                                         
}                                 
}
}
}
                       






Thank You guys
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Oct 19, 2012 11:00 am     Reply with quote

Quote:
I'm simulating this on proteus.

This is a CCS forum not a proteus one.

Proteus is bad news, it's so full of bugs you're wasting your time with it.

Eventually you have to get real hardware to work in the real world.

Start with real hardware.

I can't (no refuse to) help with ISIS / Proteus.

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 19, 2012 11:39 am     Reply with quote

I have to 'second' Mike's response ! Please read PIC101 at the top of this forum.
As Mike said Proteus is FULL of bugs, errors and faulty DRCs. You can't possibly expect anyone to 'fix' your code when your 'running' on a real bad 'simulator'.
The only simulator I run is real hardware in the real world.NO simulator,that I know of, will create EMI,welding spikes, cell phone interferenc,power dips,noisey Vcc,bad solder joints, pins swapped,etc.
Buy a PIC, a white breadboard and some 22ga solid wire.For less than an hour's worth of total frustration you can buy all the real hardware you need and NOT be disappointed!
And... you'll get lots of help here as well.

hth
jay
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Oct 19, 2012 2:37 pm     Reply with quote

Thanks jay.

Nice to get some support.

Mike
arthurlmiranda



Joined: 19 Oct 2012
Posts: 2

View user's profile Send private message

PostPosted: Wed Oct 24, 2012 7:22 am     Reply with quote

ok but no one who knows how to use modbus can tell me if I'm doing something really stupid?
where I work here I can only simulate this, I don't know when my boss will buy PIC and etc...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 24, 2012 11:35 am     Reply with quote

Yes, there is something wrong. You should look closely at your
pin assignments below:
Quote:

#define MODBUS_SERIAL_RX_PIN PIN_c6
#define MODBUS_SERIAL_TX_PIN PIN_c7

#define MODBUS_SERIAL_ENABLE_PIN PIN_c4
#define MODBUS_SERIAL_RX_ENABLE PIN_c5

Look at the 18F4550 data sheet and see what the functions and
capabilities of those pins are, and then look what you are trying
to use them for. Also, look in all the CCS modbus files and
notice what pin they assign to those constants.

Also you have changed the case of the pin on each pin constant.
This is not the same as the All Caps case that's used in the CCS .h files.
CCS is a rare compiler that by default, it ignores the case. If you
change the case of compiler constants in other compilers, you will get
"undefined identifier" errors. It's a bad habit to change the case.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group