Nick Gammon Rs485 Hardware Serial library problem

I am using arduino uno (atmega328), my code doesn't work properly.

my code is,

: Master

#include "RS485_protocol.h"

 
const byte ENABLE_PIN = 7;

void fWrite (const byte what)
  {
 // rs485.write (what); 
  Serial.write (what); 
  }
 
int fAvailable ()
  {
  //rs485.available (); 
  Serial.available (); 
  }

int fRead ()
  {
  //return rs485.read (); 
  return Serial.read (); 
  }

void setup() 
{
  //Serial connection.
  Serial.begin(9600););
 pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
}
void loop()
{
byte s=1;
byte r=5;
byte dir=1;
       byte msg[] = {
           s,    // device id
           r,    
           dir // direction
};    
        // send to slave 
        digitalWrite (ENABLE_PIN, HIGH);  // enable sending
        sendMsg (fWrite, msg, sizeof msg);
        digitalWrite (ENABLE_PIN, LOW);  // disable sending
          delay(10);
         byte buf [10];
          byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
        if(received)
        {
          if(buf[0]==0)
          {
            Serial.println("Reporting ");
            Serial.println(" ");
            Serial.print("Slave ID ");
            Serial.println(buf[1]);
            Serial.println(" ");
            Serial.print("STATUS ");
            if(buf[2]==1)
            {
              Serial.println("Operation Completed ");
              Serial.println(" ");
            }
            else{
              Serial.println("Operation Not Completed ");
              Serial.println(" ");
            }
          }
        }
          
    clearbuf1(buf);
}

Master code end[/b]

: Slave


#include "RS485_protocol.h"

const byte ENABLE_PIN = 4;


void fWrite (const byte what)
{
Serial.write (what);
}
int fAvailable ()
{
return Serial.available ();
}
int fRead ()
{
return Serial.read ();
}


void setup()
{
//rs485.begin (9600);
pinMode (ENABLE_PIN, OUTPUT);

Serial.begin(9600);
}
void loop()
{
byte n=0;
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
if(buf[0]==1
{
   Serial.println("Iam slave 1");
n=1;
}


byte msg [] = {
    0, // device 0 (master)
    s,  // slave id
    n, // operation command
};
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(2000);
// end if something received
}

Slave End//

the problem is communication between two arduino's not done.

the connections are shown below attachment

Talk to this guy - he has a very similar problem and and very identical IP address.

BTW, his name is "Gammon"

Manidhar:
the problem is communication between two arduino's not done.

the connections are shown below attachment

I'm a little surprised this compiled:

if(buf[0]==1
{
   Serial.println("Iam slave 1");
n=1;
}

Oh wait, it didn't.

sketch_jun17a.ino: In function 'void loop()':
sketch_jun17a.ino:35:1: error: expected ')' before '{' token
sketch_jun17a.ino:48:18: error: 'msg' was not declared in this scope
sketch_jun17a.ino:52:1: error: expected '}' at end of input
expected ')' before '{' token

You should talk to user SMAHESHGUPTA - he has a very similar problem to yours.

If I fix the problem with the "if" I then get:

sketch_jun17a.ino: In function 'void loop()':
sketch_jun17a.ino:43:5: error: 's' was not declared in this scope
sketch_jun17a.ino:52:1: error: expected '}' at end of input
's' was not declared in this scope

What is "s"?

You are just time-wasting, aren't you? You aren't running the code, you aren't even compiling it.

Sorry sir I am new to arduino forum ,

my code is

Master:

#include "RS485_protocol.h"

void fWrite (const byte what)
{
Serial.write (what);
}
int fAvailable ()
{
return Serial.available ();
}
int fRead ()
{
return Serial.read ();
}

const byte ENABLE_PIN = 4;


void clearbuf(byte *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}

void setup()
{
    Serial.begin (9600);
    Serial.println("Master");
    pinMode (ENABLE_PIN, OUTPUT); // driver output enable
} // end of setup


void loop()
{
    
    byte msg [] = {
        3, 
        1, 
        2, 
    };
    delay (1); // give the master a moment to prepare to receive
    digitalWrite (ENABLE_PIN, HIGH); // enable sending
    sendMsg (fWrite, msg, sizeof msg);
    digitalWrite (ENABLE_PIN, LOW); // disable sending
    
    byte buf [10];
    byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
    if (received)
    {
      Serial.print("buf[0]");
      Serial.println(buf[0]);
      Serial.print("buf[1]");
      Serial.println(buf[1]);
      Serial.print("buf[2]");
      Serial.println(buf[2]);
    }
    
    delay(2000);
    // end if something received
    clearbuf(buf);
}

Slave:

#include "RS485_protocol.h"

void fWrite (const byte what)
{
Serial.write (what);
}
int fAvailable ()
{
return Serial.available ();
}
int fRead ()
{
return Serial.read ();
}

const byte ENABLE_PIN = 4;


void clearbuf(byte *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}

void setup()
{
    Serial.begin (9600);
    pinMode (ENABLE_PIN, OUTPUT); // driver output enable
    Serial.println("Slave");
} // end of setup


void loop()
{
    byte buf [10];
    byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
    if (received)
    {
      Serial.print("buf[0]");
      Serial.println(buf[0]);
      Serial.print("buf[1]");
      Serial.println(buf[1]);
      Serial.print("buf[2]");
      Serial.println(buf[2]);
    
    
    byte msg [] = {
        0, 
        1, 
        2, 
    };
    delay (1); // give the master a moment to prepare to receive
    digitalWrite (ENABLE_PIN, HIGH); // enable sending
    sendMsg (fWrite, msg, sizeof msg);
    digitalWrite (ENABLE_PIN, LOW); // disable sending
    delay(2000);
    // end if something received
    clearbuf(buf);
}
}

the output is printed garbage data.

the output screen shorts are shown attachment

Thanks in advance

slave.png

Please show your wiring.

Hi,
have you got the gnd of both controllers connected together?
I notice your serial sends println a few times but only one is decoded on the monitor input.
Have you got the speed on the monitor 9600?
I'm not familiar with the monitor you are using, have you used the monitor in the arduino IDE?

Tom.... :slight_smile:

void fWrite (const byte what)
{
Serial.write (what);
}

...

      Serial.print("buf[0]");

You seem to be trying to use the same serial port for both talking to another Arduino, and also sending debugging to the Serial Monitor. I cannot conceive of how that might work.

My examples on my RS485 page used SoftwareSerial to avoid this issue.

Sir actually I am using GSM and RS485 Modules, the both are run using software serial.

if I can use both modules at master side, some problems are occurred.

the problem is if master received data from slave ,then the GSM is not work ,

that is, it prints 'No data received' continuously.

the code is

Master:

#include "SIM900.h"
#include <SoftwareSerial.h>

#include "RS485_protocol.h"

SoftwareSerial rs485(5,6);

#include<string.h>

#include<stdlib.h>


#include "sms.h"
SMSGSM sms;
char num[20],tex[160];
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
 
char number[20];
char sende[20]="+919642463489"; // Athenticator mob
char message[180];
 
char pos;
char d[10],c[10];
 int r,p,j=0,i=0,k,dir,s,m;
 
 /**********  RS485 functions   *********/
 
 
const byte ENABLE_PIN = 7;
 
void fWrite (const byte what)
  {
  rs485.write (what); 
  }
 
int fAvailable ()
  {
  rs485.available (); 
  }

int fRead ()
  {
  return rs485.read (); 
  }

/*******************************************/

void setup() 
{
  //Serial connection.
  Serial.begin(9600);
  Serial.println("GSM Based Stepper motor Controling\n");
  
  rs485.begin(9600);
  pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
  
  if (gsm.begin(9600)){
    Serial.println("\nstatus=READY");
    started=true;  
  }
  else Serial.println("\nstatus=IDLE");
};//setup()

void clearbuf(char *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}

void clearbuf1(byte *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}

void loop() 
{
   int r;
   pos=sms.IsSMSPresent(SMS_UNREAD);// we can check msg recvd or not
 
   delay(1000);
 
   if((int)pos>=0&&(int)pos<=20){

   Serial.print("POS=");
 
   Serial.println((int)pos);
 
   message[0]='\0';
 
   sms.GetSMS((int)pos,number,message,180);// getting the num&mesg
   delay(500);
   delay(3000);
   
   if(number[0])// if number recived or not checking
   {
     p=strcmp(number,sende); //checking the number 
   
     if(!p)// if known number
     {
        Serial.println("Authorised Person\n"); 
        Serial.print("Mobile Number: ");
        Serial.println(number);   
        Serial.println(" ");        
        Serial.print("Message: ");
        Serial.println(message); 
        Serial.println(" "); 
       
        
        while(message[i]!='@')
        {
            c[j]=message[i];
            i++;
            j++;
        }
        i++;
        c[j]='\0';
        while(message[i]!='\0')
        {
            d[k]=message[i];
            i++;
            k++;
        }
        d[k]='\0';
        
        s=atoi(c);
        r=atoi(d);
        
        if(r>=0)// checking Clockwise
        {
           dir=1;//clockwise
        }   
        else// checking AntiClockwise checking
        {
           dir=2;//anticlockwise
        }
        i=0,j=0,k=0;
   rs485.listen();
        
       byte msg[] = {
           s,    // device id
           r,    // no of rotations
           dir // direction
        };
        
        
        // send to slave 
        digitalWrite (ENABLE_PIN, HIGH);  // enable sending
        sendMsg (fWrite, msg, sizeof msg);
        digitalWrite (ENABLE_PIN, LOW);  // disable sending
        
        // rs485.listen();
         byte buf [10];
         byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);

          if(buf[0]==0)
          {
            Serial.println("Reporting ");
            Serial.println(" ");
            Serial.print("Slave ID ");
            Serial.println(buf[1]);
            Serial.println(" ");
            Serial.print("STATUS ");
            if(buf[2]==1)
            {
              Serial.println("Operation Completed ");
              Serial.println(" ");
            }
            else{
              Serial.println("Operation Not Completed ");
              Serial.println(" ");
            }
          }
          
    clearbuf1(buf);
        
     }//if(!p)
     else//if unknown number recived
     {
        Serial.println("Un Authorised Person\n");
        Serial.print("Mobile Number: ");
        Serial.println(number);   
        Serial.println(" ");        
        Serial.print("Message: ");
        Serial.println(message); 
        Serial.println(" ");  
        Serial.println("Operation not Alloted\n");
     }
   }//if(number[0])
   
   sms.DeleteSMS((int)pos);
   Serial.println("Message Deleted\n");
  }//if(pos)
  
   delay(1000); 
   
   /**************************************/
   /// clearing all variables 
   clearbuf(message);
   clearbuf(number);
   clearbuf(d);
   clearbuf(c);
   s='\0';
   m='\0';
   r='\0';
   delay(2000);
}//loop()

i am using the library is GSM Shield , show the attachment .

GSMSHIELD.zip (50.6 KB)

Slave:

#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (2, 3);
// receive pin, transmit pin
const byte ENABLE_PIN = 4;

 /***   stepper motor pins   *********/
const int motorPin1 = 8;        // Blue   - 28BYJ48 pin 1
const int motorPin2 = 9;        // Pink   - 28BYJ48 pin 2
const int motorPin3 = 10;        // Yellow - 28BYJ48 pin 3
const int motorPin4 = 11;        // Orange - 28BYJ48 pin 4
                                // Red    - 28BYJ48 pin 5 VCC
int motorSpeed=3000;            // set stepper speed
float err=0;

int count=0;
   int r,m;

byte n=0;
byte s;

void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}

void clearbuf(byte *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}


void cw(int j)
{
  ;
}

void ccw(int l)
{
;
}

void off(){
  ;
}


void setup()
{
rs485.begin (9600);
pinMode (ENABLE_PIN, OUTPUT);

  /****  stepper motor pins activating********/
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

Serial.begin(9600);
//Serial.println("SLAVE 1");
}
void loop()
{
byte buf [10],dir;
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
 m=buf[1];
switch(buf[0])
{
  case 1: Serial.println("I am Slave 1");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=1;
          break;
          
  case 2: Serial.println("I am Slave 2");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=2;
          break;
          
  case 3: Serial.println("I am Slave 3");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=3;
          break;
          
  case 4: Serial.println("I am Slave 4");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=4;
          break;
          
  case 5: Serial.println("I am Slave 5");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=5;
          break;
          
  case 6: Serial.println("I am Slave 6");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=6;
          break;
          
  case 7: Serial.println("I am Slave 7");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          break;
}//switch(buf[0])

byte msg [] = {
    0, // device 0 (master)
    s,  // slave id
    n, // operation command
};

delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(2000);
// end if something received
clearbuf(buf);
n=0;
}// if(recevied)
}// end of loop

 void degrpm(boolean bcw, long deg100, int rpm100) 
  {  
    //max 64 turns or 23,000 deg or 2,300,000 deg100 long is bigger
    //max 3500 rpm100 with 12v
    int steps=510;//deg100*64*8/360/100; //rounded down
    r=m*steps;
    
    if(rpm100<50)rpm100=50; //minimum should use degrpmslow()
    rpm100=long(1463600)/rpm100-20;  //see stepper.xls
    if(bcw==1) cwss(rpm100);
    else   ccwss(rpm100);
    float movedeg=float(steps)*360/64/8;  //float library adds 2K size to sketch
    Serial.println(movedeg);
    Serial.println(movedeg-(float)deg100/100);  //moved too little only?
  }
  void ccwss(int speed)
  {
    motorSpeed=speed; //for(int i=0;i<steps;i++)
    ccw(r);  //64*8 is 1 rev
  }
  void cwss(int speed) 
  {
    motorSpeed=speed; 
    cw(r);  //64*8 is 1 rev
  }

data sending from master to slave is ok but the problem is data transfer from slave to master is not ok.

Manidhar:
I
the connections are shown below attachment

You have no termination resistors! You must have them at each end of the line.

Manidhar:
Sir actually I am using GSM and RS485 Modules, the both are run using software serial.

I am puzzled as to why you posted code which you aren't actually using.

This is some sort of test, right? Are we passing?

void cw(int j)
{
  ;
}

void ccw(int l)
{
;
}

The mind boggles.

sir actually am deleting that , because the code limit is exceed.

sir I want two way communication between two arduino's.

sir the master sending data but not receiving data.

the modifying code is

Master : same code,

Slave:

#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (2, 3);
// receive pin, transmit pin
const byte ENABLE_PIN = 4;

int count=0;
   int r,m;

byte n=0;
byte s;

void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}

void clearbuf(byte *b)// clearing the buffer
{
  while(*b)
    *b++='\0';
}



void setup()
{
rs485.begin (9600);
pinMode (ENABLE_PIN, OUTPUT);

Serial.begin(9600);
//Serial.println("SLAVE 1");
}
void loop()
{
byte buf [10],dir;
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
 m=buf[1];
switch(buf[0])
{
  case 1: Serial.println("I am Slave 1");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=1;
          break;
          
  case 2: Serial.println("I am Slave 2");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=2;
          break;
          
  case 3: Serial.println("I am Slave 3");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=3;
          break;
          
  case 4: Serial.println("I am Slave 4");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=4;
          break;
          
  case 5: Serial.println("I am Slave 5");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=5;
          break;
          
  case 6: Serial.println("I am Slave 6");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          n=1;
          s=6;
          break;
          
  case 7: Serial.println("I am Slave 7");
          Serial.print("msg: ");
          Serial.println(buf[1]);
          Serial.print("Dir: ");
          dir=1;
          break;
}//switch(buf[0])

byte msg [] = {
    0, // device 0 (master)
    s,  // slave id
    n, // operation command
};

delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(2000);
// end if something received
clearbuf(buf);
n=0;
}// if(recevied)
}// end of loop

MarkT:
You have no termination resistors! You must have them at each end of the line.

sir please refer any example sketch.

It's not a "sketch" thing, it's hardware.

my hardware connections are correct the data is transferred master to slave correctly but the slave to master is not.

I think the code is problem. I can't notice that.