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

RS485 communication problem

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



Joined: 19 Mar 2021
Posts: 13

View user's profile Send private message AIM Address

RS485 communication problem
PostPosted: Tue Mar 23, 2021 3:23 am     Reply with quote

Hello friends,

I am working on a project that has 4 circuits. And they will communicate with each other. I have started from basic with two circuits. I am using standard <rs485.c> library.
I used " rs485_send_message(ID, length, data); " function to send data,
and " rs485_get_message(data, false) " function to receive data.

This is easy. But the problem is when i send the data to the slave, the slave does not understand the data. It is acting every time the same. For example when i send 0x6F, the slave is blinking one time. If i send 0x70, the slave is blinking one time again. Whatever i sent, this is same. The slave understands that something has come in but it does not know what it is.

My codes are below,

Thanks for your help.
Code:

////////        MASTER             ///////////

#include <16f877a.h>
#use delay(clock=20M)
#fuses HS, NOWDT, NOPUT, NOLVP, NOCPD, NOPROTECT, NODEBUG, NOBROWNOUT, NOWRT
#include <string.h>

#define RS485_ID 0x00

#define RS485_RX_ENABLE PIN_C5
#define RS485_ENABLE_PIN PIN_C5
#define RS485_USE_EXT_INT FALSE

#include <rs485.c>


void main()
{
  set_tris_b(0xFF);
  rs485_init();
 
  unsigned int i=0, t=0;
 
  while(true)
  { 
      if (input(pin_b1) == 1)
      {
        rs485_send_message(0x01, 6, 0x6F);    // 0X6F is sent

        output_high(PIN_C1);
        delay_ms(20);
        output_low(PIN_C1);
        delay_ms(500);
       
      }
      if (input(pin_b2) == 1)
      {
        rs485_send_message(0x01, 6, 0x70);    //0X70 is sent

        output_high(PIN_C1);
        delay_ms(20);
        output_low(PIN_C1);
        delay_ms(500);
      }
 
  delay_ms(100);
 
  }
}

Code:

////////////           SLAVE          /////////////////////7

#include <16f877A.h>
#use delay(clock=20M)
#fuses HS, NOWDT, NOPUT, NOLVP, NOCPD, NOPROTECT, NODEBUG, NOBROWNOUT, NOWRT
#include <string.h>

#define RS485_ID 0x01

#define RS485_RX_ENABLE PIN_C5
#define RS485_ENABLE_PIN PIN_C5
#define RS485_USE_EXT_INT FALSE

#include <rs485.c>


void main()
{
   rs485_init();
   delay_ms(100);
   while(true)
   {
      for(int t=0; t<50; t++)
      {
         if(rs485_get_message(0x6F, false))   // when i receive 0x6F
         {
         output_high(pin_c1);
         delay_ms(50);
         output_low(pin_c1);
         delay_ms(50);
         }
         
         if(rs485_get_message(0x70, false))    // when i receive 0x70
         {
         output_high(pin_c2);
         delay_ms(50);
         output_low(pin_c2);
         delay_ms(50);
         output_high(pin_c2);
         delay_ms(50);
         output_low(pin_c2);
         delay_ms(50);
         }
         delay_ms(50);
      }
      output_high(pin_c0);
      delay_ms(30);
      output_low(pin_c0);
      delay_ms(30);
   }     
}

Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Mar 23, 2021 4:10 am     Reply with quote

You are misunderstanding how get/send message works.

get doesn't 'look' for a specific value, it receives a message. You then have
to decode this.

The value passed are pointers to where you want the message to be stored,
and a flag to select for a timeout or not.

send_message also wants a pointer to the data.

So your master needs something like:
Code:

   char message_buff[1];

   message_buff[0]=0x6F; //to send the 6F message
   rs485_send_message(0x01, 1, message_buff);    // send single byte
   //from message_buff


Then your slave needs something like:
Code:

void main(void)
{
   char message[8]; //minimum for a one byte message is 3
   set_tris_b(0xFF);
   rs485_init();
   while (TRUE)
   {
      if (rs485_get_message(message, FALSE))
      {
         //now we only get here if a message was available
         //message[0] will be the ID that sent the message
         //message[1] will be the message length
         //message[2] will be the message byte
         if (message[2]==0x6F)
         {
            //here we have received a 0x6F message
 
         }
         else if (message[2]==0x70)
         {
             //here the 0x70 message

         }
      }
   }
}
tunatopraktan



Joined: 19 Mar 2021
Posts: 13

View user's profile Send private message AIM Address

PostPosted: Thu Mar 25, 2021 5:08 am     Reply with quote

Ttelmah wrote:
You are misunderstanding how get/send message works.

get doesn't 'look' for a specific value, it receives a message. You then have
to decode this.

The value passed are pointers to where you want the message to be stored,
and a flag to select for a timeout or not.

send_message also wants a pointer to the data.

So your master needs something like:
Code:

   char message_buff[1];

   message_buff[0]=0x6F; //to send the 6F message
   rs485_send_message(0x01, 1, message_buff);    // send single byte
   //from message_buff


Then your slave needs something like:
Code:

void main(void)
{
   char message[8]; //minimum for a one byte message is 3
   set_tris_b(0xFF);
   rs485_init();
   while (TRUE)
   {
      if (rs485_get_message(message, FALSE))
      {
         //now we only get here if a message was available
         //message[0] will be the ID that sent the message
         //message[1] will be the message length
         //message[2] will be the message byte
         if (message[2]==0x6F)
         {
            //here we have received a 0x6F message
 
         }
         else if (message[2]==0x70)
         {
             //here the 0x70 message

         }
      }
   }
}




Thank you very much for your help Ttelmah,
It was very helpful.
I have one more question?

If want to take feedback data from slave to master. Which way should i follow? And how can i understand the bus line is suitable?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Mar 25, 2021 8:46 am     Reply with quote

You just send a message back.

The whole point of this system is that provided the RS485 is properly
configured so all the transceivers are in receive mode when not sending
any device on the bus can send a message to any other device. The code
automatically handles retrying if the bus is busy with another message
when you try to send.
You'd probably want to set the message buffer size to perhaps 8 characters
in all the chips, since the receive message always has the ID and message
length also put into the buffer.
tunatopraktan



Joined: 19 Mar 2021
Posts: 13

View user's profile Send private message AIM Address

PostPosted: Mon Mar 29, 2021 4:27 am     Reply with quote

Ttelmah wrote:
You just send a message back.

The whole point of this system is that provided the RS485 is properly
configured so all the transceivers are in receive mode when not sending
any device on the bus can send a message to any other device. The code
automatically handles retrying if the bus is busy with another message
when you try to send.
You'd probably want to set the message buffer size to perhaps 8 characters
in all the chips, since the receive message always has the ID and message
length also put into the buffer.


Thanks for your help friend.
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