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

Modbus - Exception 0C

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



Joined: 12 May 2021
Posts: 5

View user's profile Send private message

Modbus - Exception 0C
PostPosted: Wed May 12, 2021 1:58 pm     Reply with quote

Hello all,

I am new at Modbus and trying to communicate on PIC processors via RS485. I have using Modbus library. When I started the Proteus simulation this exception returns. Only this exception returns when I choose a different selection.

This is project, i used

https://github.com/worrajak/PICmodbus

This is the catalogue. But i don't understand what's wrong?

https://modbus.org/docs/PI_MBUS_300.pdf

https://preview.redd.it/fkk632qk7jy61.png?width=902&format=png&auto=webp&s=d61d8f1336f578424de9bea0f7a12182361f0068

And this is my code: (Turn on the led)

master:
Code:
#include <master_led_yak.h>
#fuses HS, NOWDT
#use delay(clock=4M)
#define MODBUS_TYPE MODBUS_TYPE_MASTER
#define MODBUS_SERIAL_TYPE MODBUS_RTU     //use MODBUS_ASCII for ASCII mode
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 2400
#use rs232(baud=9600, UART1, stream=PC, errors)
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_EXT
#define MODBUS_SERIAL_TX_PIN PIN_B1   // Data transmit pin
#define MODBUS_SERIAL_RX_PIN PIN_B0   // Data receive pin
#define DEBUG_MSG(msg) fprintf(PC, msg)
#define DEBUG_DATA(msg,data) fprintf(PC, msg, data)



#include <modbus.c>
#define MODBUS_SLAVE_ADDRESS 0x80

int i;
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 print_menu()
{
   DEBUG_MSG("\r\n1. ledi yak\r\n");
}
void turnOnLED(){
   DEBUG_MSG("LED YAKILACAK\r\n");
   if(!(modbus_write_single_coil(MODBUS_SLAVE_ADDRESS,1,TRUE))){
      DEBUG_MSG("data: ");
      for(i=0; i<(modbus_rx.len); ++i)
         DEBUG_DATA("%X",modbus_rx.data[i]);
      DEBUG_MSG("\r\n\r\n");
   }else{
      DEBUG_DATA("<-**Exception %X**->\r\n\r\n", modbus_rx.error);
   }
}
void display(char c){
   switch(c){
      case '1':
         turnOnLED();
         break;
   }
}
void main(){
   char c;
   DEBUG_MSG("\r\nInitializing...");
   modbus_init();
   DEBUG_MSG("...ready\r\n");
   
      do{
      print_menu();
      c = getc(PC);
      fprintf(PC,"\r\n");
      display(c);
   } while(TRUE);
   
}


slave:
Code:
#include <slave_led_yak.h>
#fuses HS,NOWDT
#use delay(clock=4M)
#use fast_io(d)
#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 2400
#define MODBUS_SERIAL_TX_PIN PIN_B1   // Data transmit pin
#define MODBUS_SERIAL_RX_PIN PIN_B0   // Data receive pin
#include <modbus.c>
#define MODBUS_ADDRESS 0x80
/*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()
{
//!   char c;
   
   set_tris_d(0x00);
   
   int8 coils = 0x00;
   int16 event_count = 0;
   
   modbus_init();
   
   while(TRUE){
      output_d(coils);
      while(!modbus_kbhit());
      delay_us(50);
     
      if((modbus_rx.address == MODBUS_ADDRESS) || modbus_rx.address == 0){
         switch(modbus_rx.func){
            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;
         }
      }
   }
}


https://i.hizliresim.com/6f7tzsz.png
https://i.hizliresim.com/d920p52.png
The left one is slave modbus TX and nothing going on.
The upper right one is master rx-tx. My gui for turn on the led.
the bottom right one is slave modbus RX. The information received.

What should i do with this. Can you help me?
temtronic



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

View user's profile Send private message

PostPosted: Wed May 12, 2021 2:48 pm     Reply with quote

Quite understandable as it's a Proteus 'simulation'... please read PIC101 sticky...

When you get real hardware 'up and running', let me know....
Jay
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed May 12, 2021 7:09 pm     Reply with quote

If you look at the github you posted, there are pictures there with a RS485 transceiver. I've also only ever used the modbus 485 library with the transceiver in there. I cannot be sure that without the transceiver, something is going wrong.
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