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 CCS Technical Support

Slave Modbus RS485

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



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

Slave Modbus RS485
PostPosted: Tue Jul 08, 2014 3:10 am     Reply with quote

Hello, first sorry for my English as it is not very good. I said I'm new to the forum and I have read the standard publication and hope I'm not breaking any're to perform post.

The help I need is that I'm trying to connect and comunicate through an RS485 with Modbus comunications protocol.

I got the RS485 comunication but through my own protocol already.

I am using the CCS PCWHD v4.130 and the example which I am testing is ex_modbus_slave_c.

The Schematics from this slave is the following picture.



and the code:

Code:

////////////////////////////////////////////////////////////////////////////////
////                         Slave                                                        ////
////                                                                                        ////
//////////////////////////////////////////////////////////////////////////////

#define USE_WITH_PC 1
#include <16F88.h>   

#fuses HS,NOWDT

#use delay(clock=8000000,RESTART_WDT)


#ZERO_RAM

#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_TYPE MODBUS_RTU     //use MODBUS_ASCII for ASCII mode
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600


#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA

#define MODBUS_SERIAL_TX_PIN PIN_B5   // Data transmit pin
#define MODBUS_SERIAL_RX_PIN PIN_B2   // Data receive pin
      //The following should be defined for RS485 communication
#define MODBUS_SERIAL_ENABLE_PIN   PIN_B1   // Controls DE pin for RS485
#define MODBUS_SERIAL_RX_ENABLE    PIN_B1  // Controls RE pin for RS485


#include <modbus.c>

#define MODBUS_ADDRESS 0x08   //0xF7


// DIRECCION DE LECTURA DEL SENSOR1 TEMPERATURA
#define HIGH_ADDRESS_READ_TEMP1      0x00 //TEMP1
#define LOW_ADDRESS_READ_TEMP1      0x0F



// DIRECCIONES DE ESCRITURA
#define HIGH_ADDRESS_WRITE_RELE1   0x00 //RELE1
#define LOW_ADDRESS_WRITE_RELE1      0x0A



int8 temp1=0;





/*This function may come in handy for you since MODBUS uses MSB first.*/

/*This function may come in handy for you since MODBUS uses MSB first.*/
int8 swap_bits(int8 c)
{
   return ((c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0)|((c&16)?8:0)
          |((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0);
}
void main()
{

   int8 coils = 0b00000101;
   int8 inputs = 0b00001001;
   int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x1100};
   int16 input_regs[] = {0x1100,0x2200,0x3300,0x4400,0x5500,0x6600,0x7700,0x8800};
   int16 event_count = 0;
   
   setup_adc_ports(NO_ANALOGS);


    modbus_init();
   
   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)
      {
         switch(modbus_rx.func)
         {
       
            case FUNC_READ_COILS:    //read coils
            case FUNC_READ_DISCRETE_INPUT:    //read inputs
               
   
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

               
             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
               break;
            case FUNC_READ_HOLDING_REGISTERS:
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
            case FUNC_READ_INPUT_REGISTERS:
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
               break;
               
            case FUNC_WRITE_SINGLE_COIL:      //write coil
               if(modbus_rx.data[0] || modbus_rx.data[3] || modbus_rx.data[1] > 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else if(modbus_rx.data[2] != 0xFF && modbus_rx.data[2] != 0x00)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_VALUE);
               else
               {
                  //coils are stored msb->lsb so we must use 7-address
                  if(modbus_rx.data[2] == 0xFF)
                     bit_set(coils,modbus_rx.data[1]);
                  else
                     bit_clear(coils,modbus_rx.data[1]);

                  modbus_write_single_coil_rsp(MODBUS_ADDRESS,modbus_rx.data[1],((int16)(modbus_rx.data[2]))<<8);
                 
                  event_count++;
               }
               break;
               
            case FUNC_WRITE_SINGLE_REGISTER:
               if(modbus_rx.data[0] || modbus_rx.data[1] >= 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  //the registers are stored in little endian format
                  hold_regs[modbus_rx.data[1]] = make16(modbus_rx.data[3],modbus_rx.data[2]);

                  modbus_write_single_register_rsp(MODBUS_ADDRESS,
                               make16(modbus_rx.data[0],modbus_rx.data[1]),
                               make16(modbus_rx.data[2],modbus_rx.data[3]));
               }
               break;
               
            case FUNC_WRITE_MULTIPLE_COILS:
               if(modbus_rx.data[0] || modbus_rx.data[2] ||
                  modbus_rx.data[1] >= 8 || modbus_rx.data[3]+modbus_rx.data[1] > 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  int i,j;

                  modbus_rx.data[5] = swap_bits(modbus_rx.data[5]);

                  for(i=modbus_rx.data[1],j=0; i < modbus_rx.data[1]+modbus_rx.data[3]; ++i,++j)
                  { 
                     if(bit_test(modbus_rx.data[5],j))
                        bit_set(coils,7-i);
                     else
                        bit_clear(coils,7-i);
                  }

                  modbus_write_multiple_coils_rsp(MODBUS_ADDRESS,
                                 make16(modbus_rx.data[0],modbus_rx.data[1]),
                                 make16(modbus_rx.data[2],modbus_rx.data[3]));
                 
                  event_count++;
               }
               break;
               
            case FUNC_WRITE_MULTIPLE_REGISTERS:
               if(modbus_rx.data[0] || modbus_rx.data[2] ||
                  modbus_rx.data[1] >= 8 || modbus_rx.data[3]+modbus_rx.data[1] > 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  int i,j;

                  for(i=0,j=5; i < modbus_rx.data[4]/2; ++i,j+=2)
                     hold_regs[i] = make16(modbus_rx.data[j+1],modbus_rx.data[j]);

                  modbus_write_multiple_registers_rsp(MODBUS_ADDRESS,
                                 make16(modbus_rx.data[0],modbus_rx.data[1]),
                                 make16(modbus_rx.data[2],modbus_rx.data[3]));
               
                  event_count++;
               }
               break;           
            default:    //We don't support the function, so return exception
               modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_FUNCTION);
         }
      }
  }
}


The program should only send the value of the variable temp1 by function code 2 modbus FUNC_READ_DISCRETE_INPUT.

The master comunication I use the USB-RS485-WE converter, the software to test comunication is Simply Modbus Master.

I need help.

Thank you very much
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Tue Jul 08, 2014 3:57 am     Reply with quote

I have to say fractionally 'hurrah'. Smile
You tell us just about everything needed.

A couple of things in your hardware may cause problems:

1) Have a pull up resistor (perhaps 10KR) on the PIC RX pin. Problem here is that this pin is floating, when the RS485 bus switches to 'transmit', and you may well get occasional bits of garbage apparently received, unless you make sure the pin stays 'high' when not driven.
2) The RS485 bus needs bias as well as termination. If it doesn't have this, when your master is not transmitting, and the slave is also listening, again the lines can float. Look at the circuit here:
<http://en.wikipedia.org/wiki/RS-485>
These don't apply if the lines are never allowed to float, but do apply with Modbus.

Now, without these you may well be getting false starts and other problems.

There is a slight 'question' over the protocol. Are you using RTU, or ASCII?. Serial is more commonly ASCII, than RTU.

Have you checked your master device is running at 9600bps?.

What happens if you run the standard example, just changed to suit your hardware?.

The input you have defined would be address 10016 is this what you are using?.
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Thu Jul 10, 2014 4:23 am     Reply with quote

Thank you for answering so fast, I connected my slave with a master display and works perfectly.

The 10k resistor you indicate me going from RX pin to VCC pin?

I need use protocol RTU and master device is running at 9600bps. The address that i have defined is 10016.

In a master screen works well, but through the rs485-usb we software master do not get to communicate.

The distance between master and slaves are are 10 cm long.

Thank you

Ttelmah wrote:
I have to say fractionally 'hurrah'. Smile
You tell us just about everything needed.

A couple of things in your hardware may cause problems:

1) Have a pull up resistor (perhaps 10KR) on the PIC RX pin. Problem here is that this pin is floating, when the RS485 bus switches to 'transmit', and you may well get occasional bits of garbage apparently received, unless you make sure the pin stays 'high' when not driven.
2) The RS485 bus needs bias as well as termination. If it doesn't have this, when your master is not transmitting, and the slave is also listening, again the lines can float. Look at the circuit here:
<http://en.wikipedia.org/wiki/RS-485>
These don't apply if the lines are never allowed to float, but do apply with Modbus.

Now, without these you may well be getting false starts and other problems.

There is a slight 'question' over the protocol. Are you using RTU, or ASCII?. Serial is more commonly ASCII, than RTU.

Have you checked your master device is running at 9600bps?.

What happens if you run the standard example, just changed to suit your hardware?.

The input you have defined would be address 10016 is this what you are using?.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Jul 10, 2014 4:57 am     Reply with quote

Quote:
2) The RS485 bus needs bias as well as termination. If it doesn't have this, when your master is not transmitting, and the slave is also listening, again the lines can float. Look at the circuit here:
<http://en.wikipedia.org/wiki/RS-485>


This is required when using the USB-RS485-WE adaptor cables.

9600 Baud, RTU mode is "normal" for Modbus over RS485.

Also there is lots of confusion over which line is A and which is B. Its common to have to connect A on the IC to B on the cable and vice versa.

It is quite easy to damage RS485 ICs, such as by shorting A or B to ground, or each other. I have damaged three USB-RS485-WE cables in just one project. :-( To sort these problems, you'll need to use an oscilloscope to check the RS485 voltages are correct.

The CCS example code you've used makes only eight of each type of Modbus data: eight coils (or output bits), eight discrete inputs (or bits), eight 16 bit input registers (not used much these days) and eight 16 bit registers (these are the ones to use for most data). This means that if you connect it to a Modbus controller, it may receive errors when it tries to talk to registers etc. that aaren't implemented by the example code. You have to edit the code to give the number of registers, coils and input bits, that you need.

I see that you have tried to edit it, but discrete inputs are single bits, not multiple bit values. The example implements eight discrete inputs as the eight bits of a byte, but they are NOT a byte, just a group of eight separate bits. To read a processed value such as temperature, use a holding register instead.
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Thu Jul 10, 2014 5:30 am     Reply with quote

Thanks for answering when I have time I will put to work on what you've told me and verify that the RS485-USB WE adapter works well.


RF_Developer wrote:
Quote:
2) The RS485 bus needs bias as well as termination. If it doesn't have this, when your master is not transmitting, and the slave is also listening, again the lines can float. Look at the circuit here:
<http://en.wikipedia.org/wiki/RS-485>


This is required when using the USB-RS485-WE adaptor cables.

9600 Baud, RTU mode is "normal" for Modbus over RS485.

Also there is lots of confusion over which line is A and which is B. Its common to have to connect A on the IC to B on the cable and vice versa.

It is quite easy to damage RS485 ICs, such as by shorting A or B to ground, or each other. I have damaged three USB-RS485-WE cables in just one project. :-( To sort these problems, you'll need to use an oscilloscope to check the RS485 voltages are correct.

The CCS example code you've used makes only eight of each type of Modbus data: eight coils (or output bits), eight discrete inputs (or bits), eight 16 bit input registers (not used much these days) and eight 16 bit registers (these are the ones to use for most data). This means that if you connect it to a Modbus controller, it may receive errors when it tries to talk to registers etc. that aaren't implemented by the example code. You have to edit the code to give the number of registers, coils and input bits, that you need.

I see that you have tried to edit it, but discrete inputs are single bits, not multiple bit values. The example implements eight discrete inputs as the eight bits of a byte, but they are NOT a byte, just a group of eight separate bits. To read a processed value such as temperature, use a holding register instead.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Thu Jul 10, 2014 7:47 pm     Reply with quote

One thing I would do is put a 0.1ufd ceramic cap across the vss/vdd lines instead of the 100pf you show - that is too small to do much good with the noise.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 11, 2014 2:20 am     Reply with quote

gpsmikey wrote:
One thing I would do is put a 0.1ufd ceramic cap across the vss/vdd lines instead of the 100pf you show - that is too small to do much good with the noise.

mikey



Hello Mikey.

I think that 0.1uf is the same that 100pf.
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Fri Jul 11, 2014 3:06 am     Reply with quote

No.....

0.1uF = 100nF

0.1nF = 100pF

Your capacitor is 1000* smaller than 0.1uF.
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Mon Jul 14, 2014 3:52 am     Reply with quote

Ttelmah wrote:
No.....

0.1uF = 100nF

0.1nF = 100pF

Your capacitor is 1000* smaller than 0.1uF.


Sorry :( hahaha
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Mon Jul 14, 2014 5:22 am     Reply with quote

I changed the capacitor as you have commented me. I have seen the datasheet RS485-WE USB converter http://www.ftdichip.com/Support/Documents/DataSheets/Cables/DS_USB_RS485_CABLES.pdf and has 6 wires: Orange A +, B-Yellow, Red 5V, 0V Black, Brown and Green for connecting a 120 ohm resistor in a multipoint system. With this information I connect the orange wire to pin 6 (A) of the MAX 485 and the yellow wire to pin 7 (B) of max 485 and the Black wire to ground the circuit, also I put a resistance of 680ohm from A vcc and B to gnd.



I check the program to poll Modbus software failure and gives timeout errors, continued without communicating well, while a master modbus screen if you connect wellor as you have commented me.
Code:
/////////////////////////////////////////////////////////////////////////
////                         Slave                                  ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////

#define USE_WITH_PC
#include <16F88.h>   

#fuses HS,NOWDT
#use delay(clock=8000000)


#ZERO_RAM

#define MODBUS_PROTOCOL  MODBUS_PROTOCOL_SERIAL
#define MODBUS_TYPE MODBUS_TYPE_SLAVE
#define MODBUS_SERIAL_TYPE MODBUS_RTU     //use MODBUS_ASCII for ASCII mode
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 9600
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#define MODBUS_SERIAL_TX_PIN PIN_B5   // Data transmit pin
#define MODBUS_SERIAL_RX_PIN PIN_B2   // Data receive pin
//The following should be defined for RS485 communication
#define MODBUS_SERIAL_ENABLE_PIN   PIN_B1   // Controls DE pin for RS485
#define MODBUS_SERIAL_RX_ENABLE    PIN_B1  // Controls RE pin for RS485

#include <modbus.c>

#define MODBUS_ADDRESS 0x01   

#define RELE_1 PIN_A3   // salida rele 1

// DIRECCION DE LECTURA DEL SENSOR1 TEMPERATURA
#define HIGH_ADDRESS_READ_TEMP1      0x00 //TEMP1
#define LOW_ADDRESS_READ_TEMP1      0x02

#define HIGH_ADDRESS_WRITE_RELE1      0x00 //RELE
#define LOW_ADDRESS_WRITE_RELE1      0x05

#BYTE TRISA = 0x85
#BYTE TRISB = 0x86    // Configuramos el TRISB
#BYTE PORTA = 0x05    // Configuramos el PORTB
#BYTE PORTB = 0x06    // Configuramos el PORTB
#USE fast_io(A)
#USE fast_io(B)
int8 temp1=200;



/*This function may come in handy for you since MODBUS uses MSB first.*/

/*This function may come in handy for you since MODBUS uses MSB first.*/
int8 swap_bits(int8 c)
{
   return ((c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0)|((c&16)?8:0)
          |((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0);
}
void main()
{
   set_tris_a(0b11110111);
   set_tris_b(0b11011100);
   port_b_pullups(true);
   int8 coils = 0b00000101;
   int8 inputs = 0b00001001;
   int16 hold_regs[] = {0x8800,0x7700,0x6600,0x5500,0x4400,0x3300,0x2200,0x1100};
   int16 input_regs[] = {0x1100,0x2200,0x3300,0x4400,0x5500,0x6600,0x7700,0x8800};
   int16 event_count = 0;
   
   setup_adc_ports(NO_ANALOGS);
   output_low(RELE_1);
   delay_ms(500);

    modbus_init();
   
   while(TRUE)
   {
      temp1=~temp1+128;
      while(!modbus_kbhit());
     
      delay_us(50);
      //check address against our address, 0 is broadcast
      if((modbus_rx.address == MODBUS_ADDRESS) || modbus_rx.address == 0)
      {
         switch(modbus_rx.func)
         {
       
            case FUNC_READ_COILS:    //read coils
            case FUNC_READ_DISCRETE_INPUT:    //read inputs
               
   
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

               
             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
               break;
            case FUNC_READ_HOLDING_REGISTERS:
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
            case FUNC_READ_INPUT_REGISTERS:
                if((modbus_rx.data[0]== HIGH_ADDRESS_READ_TEMP1) && (modbus_rx.data[1]==LOW_ADDRESS_READ_TEMP1)){
               modbus_read_discrete_input_rsp(MODBUS_ADDRESS,1,&temp1);
                }

             else
                {
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
               break;
               
            case FUNC_WRITE_SINGLE_COIL:      //write coil
           
                if((modbus_rx.data[0]== HIGH_ADDRESS_WRITE_RELE1) && (modbus_rx.data[1]==LOW_ADDRESS_WRITE_RELE1)){
                   if(modbus_rx.data[2] == 0xFF){   // desde el master enviamos un 1 que se traduce en un 0xFF en recepcion
                       output_high(RELE_1);
                   }
                   else{
                      output_low(RELE_1);}
                }
                 else{
                   modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
             }
               break;
               
            case FUNC_WRITE_SINGLE_REGISTER:
               if(modbus_rx.data[0] || modbus_rx.data[1] >= 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  //the registers are stored in little endian format
                  hold_regs[modbus_rx.data[1]] = make16(modbus_rx.data[3],modbus_rx.data[2]);

                  modbus_write_single_register_rsp(MODBUS_ADDRESS,
                               make16(modbus_rx.data[0],modbus_rx.data[1]),
                               make16(modbus_rx.data[2],modbus_rx.data[3]));
               }
               break;
               
            case FUNC_WRITE_MULTIPLE_COILS:
               if(modbus_rx.data[0] || modbus_rx.data[2] ||
                  modbus_rx.data[1] >= 8 || modbus_rx.data[3]+modbus_rx.data[1] > 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  int i,j;

                  modbus_rx.data[5] = swap_bits(modbus_rx.data[5]);

                  for(i=modbus_rx.data[1],j=0; i < modbus_rx.data[1]+modbus_rx.data[3]; ++i,++j)
                  { 
                     if(bit_test(modbus_rx.data[5],j))
                        bit_set(coils,7-i);
                     else
                        bit_clear(coils,7-i);
                  }

                  modbus_write_multiple_coils_rsp(MODBUS_ADDRESS,
                                 make16(modbus_rx.data[0],modbus_rx.data[1]),
                                 make16(modbus_rx.data[2],modbus_rx.data[3]));
                 
                  event_count++;
               }
               break;
               
            case FUNC_WRITE_MULTIPLE_REGISTERS:
               if(modbus_rx.data[0] || modbus_rx.data[2] ||
                  modbus_rx.data[1] >= 8 || modbus_rx.data[3]+modbus_rx.data[1] > 8)
                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
               else
               {
                  int i,j;

                  for(i=0,j=5; i < modbus_rx.data[4]/2; ++i,j+=2)
                     hold_regs[i] = make16(modbus_rx.data[j+1],modbus_rx.data[j]);

                  modbus_write_multiple_registers_rsp(MODBUS_ADDRESS,
                                 make16(modbus_rx.data[0],modbus_rx.data[1]),
                                 make16(modbus_rx.data[2],modbus_rx.data[3]));
               
                  event_count++;
               }
               break;           
            default:    //We don't support the function, so return exception
               modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_FUNCTION);
         }
      }
  }
}
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

Warning
PostPosted: Thu Jul 24, 2014 3:27 am     Reply with quote

In the previous program when compiled in the output shows me a warning that not anyone could help me solve



Thank you
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Jul 24, 2014 1:56 pm     Reply with quote

_Warning_. Not error.
Note 'build successful'.

It is warning you about a number of things that _might_ be worth being aware of.

One temporary variable declared, but never used.
One values not defined (a define that allows you to specify a different update time).
Two routines being used inside interrupts, _and_ outside, which results in 'interrupts disabled during call'. This only causes a problem, if the routines are long/slow.
drcastilla



Joined: 29 May 2014
Posts: 8

View user's profile Send private message

PostPosted: Fri Jul 25, 2014 2:18 am     Reply with quote

Ttelmah wrote:
_Warning_. Not error.
Note 'build successful'.

It is warning you about a number of things that _might_ be worth being aware of.

One temporary variable declared, but never used.
One values not defined (a define that allows you to specify a different update time).
Two routines being used inside interrupts, _and_ outside, which results in 'interrupts disabled during call'. This only causes a problem, if the routines are long/slow.


Thanks for answering.


I know that this well compiled, and the program can be loaded without problem but every time I try to communicate with a master in the pc, giving a timeout error.

I was wondering if it can be for some warning that is not defined as what would have to be defined in this case to fix that mistake.


For communication with the computer I am using a card rs485 with db9 connector national instrument that has always worked for me, without the protrocolo Modbus communication works perfectly
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