Go Down

Topic: Sending multiple sets of information on SoftwareSerial (Read 149 times) previous topic - next topic

Harshit_Tomar

Jul 20, 2018, 03:57 pm Last Edit: Jul 20, 2018, 04:07 pm by Harshit_Tomar
I am making a transmitter and receiver from hc-12s (wireless serial communication modules) with SoftwareSerial library, I want to send 2 variables at a time. I can't find a way to send them, I am quite a beginner

I tried something like this,
HC12.send(ab, cd); and HC12.read(ab, cd);

Please help!

Thanks for any answer

pylon

Quote
I tried something like this,
HC12.send(ab, cd); and HC12.read(ab, cd);
And what was the problem with these commands? Post the code you have, links to the used libraries and post the serial output you might get or the error messages the compiler gives you. And don't forget to use code tags (the </> button in the editor) to post code!

Harshit_Tomar

This is the code for the transmitter (just for testing the code)
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial hc(10, 11); // HC-12 TX Pin, HC-12 RX Pin

double first;
double second;

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

void loop() {
  // put your main code here, to run repeatedly:
  hc.write(first, second); //the  compiler shows "no matching function for call to 'SoftwareSerial::write(double&, double&)'"

  delay(5);
}

And this is the code for the receiver (just for testing the code)
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial hc(10, 11); // HC-12 TX Pin, HC-12 RX Pin

double first;
double second;

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

void loop() {
  // put your main code here, to run repeatedly:
  if(hc.available()){
    hc.read(first, second);  //the  compiler shows no matching function for call to 'SoftwareSerial::read(double&, double&)'

    Serial.write(first);
    Serial.write("is the first value and");
    Serial.write(second);
    Serial.println("is the second value");
    delay(250);
  }
}


I also tried in the receiver code,
hc.read() = first, second;
but the  compiler shows "lvalue required as left operand of assignment"

MorganS

You can't just make up your own functions. The compiler is telling you that there is no function to write 2 things like that.

If you want to write 2 things then use two writes. Write the first one, followed by the second.

There is a certain difficulty on the receiving side: how do you know the next byte is part of the first number or the second? There must be some kind of synchronization to tell the reader which one is which.

Read Serial Input Basics
"The problem is in the code you didn't post."

Robin2

Have a look at the 3rd example in Serial Input Basics

You can send data in a compatible format with code like this
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up