0

I have one integer value which I should pass through Serial1, Serial2, Serial3, and Serial4 after performing some algebraic operation on this integer. For this algebraic operation, I made a function and input arguments is an integer and Serial number. Simple code is given below: (I deleted every extra part of my code to make easy to understand)

String stringG; int d1; int d;

#include<SoftwareSerial.h>
//Software Serial #1: Rx = digital pin 10, Tx = digital pin 11
SoftwareSerial Serial4(10, 11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  Serial4.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0)
  {
     String  XX = Serial.readString();
    int d1 = XX.toInt();
    Serial.println(d1);
    int yy;
    yy = compareFunction (d1, Serial4);
    Serial.println("Now the final values are:");
    Serial.println(d1);
    Serial.println(yy);
  }

}
int compareFunction (int d, String Serialx)
{

  int ZZ = Serialx.readString(); // I will receive some values from motor using serial4
  //to avoid long code I deleted unnecessary parts
  // I will use ZZ and d values and output will be yy
  return yy;
}

As you see in compareFunction I receive value using Serial (it may be 1,2,3 or 4) So I have to pass Serial as an input argument in this function. If I can't pass Serial then I have to write 4 functions each for Serial1, 2, 3 and 4 which I want to avoid. Is it possible? How can I do it? when I tried with above code it shows error

exit status 1 could not convert 'Serial4' from 'SoftwareSerial' to 'String'

  • 1
    It's obviously not a String. Did you try to find out what type it actually is? – Ignacio Vazquez-Abrams Nov 3 '17 at 19:06
7

The Serial object is the end of a chain of classes, each with a different set of functionality.

Print -> Stream -> HardwareSerial => [Serial]

Since you are reading from it the best class to use is Stream since that is the one that is the one that contains the reading functionality.

You should pass it by reference or as a pointer since you don't want to copy the object.

int compareFunction (int d, Stream &Serialx)
{

  int ZZ = Serialx.readString(); // I will receive some values from motor using serial4
  //to avoid long code I deleted unnecessary parts
  // I will use ZZ and d values and output will be yy
  return yy;
}

By using Stream you can use any object type that also uses Stream in its class inheritance chain - even things like a WiFi socket connection.

This behaviour is called polymorphism and is one of the more powerful features of classes in C++ (and other OO languages).

  • Thanks @Majenko. It works i.e. now I am able to use Serial as input argument in function. But there is one little problem. When, I used Serial1 or 2 or 3, in comapreFunction, in codeString ZZ = Serialx.readString(); Serial.println(ZZ); it gives accurate value like P214 or P417 etc. But when I used Serial4, it gives values like P‚’j or “‚j. What is wrong with Serial4. I used Serialx.println("P"); to get current position of motor. – Naseeb Gill Nov 4 '17 at 9:46
  • Well, Serial4 seems to be SoftwareSerial - and it's any one's guess what would happen with that. I never used it. Ever. It's crap. Try one of the replacements, like AltSoftSerial. – Majenko Nov 4 '17 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.