Go Down

Topic: Two mySerial.begin(9600)'s causing issues (Read 131 times) previous topic - next topic

DirkDozer

Alright, I'm dealing with this code at the moment and I have very close to working. I started with base code for a Conductivity sensor and then added bits of a pH sensor in. When I finished, It only printed pH so I backtracked and started un-commiting all of the pH parts and it started printing conductivity again. I've narrowed the problem down to a single line of code on line 21, mySerial0.begin(9600); If it's commited then the code prints pH, if it's not then it prints Conductivity. So can you not have two mySerial.begin(9600) ?

Here's the code I use:

Code: [Select]
#include <SoftwareSerial.h>
#define rx 3
#define tx 2
#define rx1 5
#define tx1 6

SoftwareSerial mySerial(rx, tx);
SoftwareSerial mySerial0(rx1, tx1);

String inputString = "";
String sensorStringOne = "";
String sensorStringTwo = "";
boolean inputStringComplete = false;
boolean sensorStringCompleteOne = false;
boolean sensorStringCompleteTwo = false;
float pH;


void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial0.begin(9600);
  inputString.reserve(10);
  sensorStringOne.reserve(30);
  sensorStringTwo.reserve(30);
}


void serialEvent() {
  inputString = Serial.readStringUntil(13);
  inputStringComplete = true;
}


void loop() {

  if (inputStringComplete == true) {
    mySerial.print(inputString);
    mySerial.print('\r');
    mySerial0.print(inputString);
    mySerial0.print('\r');
    inputString = "";
    inputStringComplete = false;
  }

  if (mySerial.available() > 0) {
    char inchar = (char)mySerial.read();
    sensorStringOne += inchar;
    if (inchar == '\r') {
      sensorStringCompleteOne = true;
    }
  }


  if (sensorStringCompleteOne == true) {
    if (isdigit(sensorStringOne[0]) == false) {
      Serial.println(sensorStringOne);
    }
    else
    {
      print_EC_data();
    }
    sensorStringOne = "";
    sensorStringCompleteOne = false;
  }
 
  // pH code kinda starts here

  ///*
 
  if (mySerial0.available() > 0) {
    char inchar0 = (char)mySerial0.read();
    sensorStringTwo += inchar0;
    if (inchar0 == '\r') {
      sensorStringCompleteTwo = true;
    }
  }

  if (sensorStringCompleteTwo == true) {
    Serial.print("pH: ");
    Serial.println(sensorStringTwo);
    sensorStringTwo = "";
    sensorStringCompleteTwo = false;
  }

  //*/
}

void print_EC_data(void) {

  char sensorstring_array[30];
  char *EC;
  char *TDS;
  char *SAL;
  char *GRAV;
  float f_ec;
 
  sensorStringOne.toCharArray(sensorstring_array, 30);
  EC = strtok(sensorstring_array, ",");
  TDS = strtok(NULL, ",");
  SAL = strtok(NULL, ",");
  GRAV = strtok(NULL, ",");

  Serial.print("EC:");
  Serial.println(EC);

  Serial.print("TDS:");
  Serial.println(TDS);

  Serial.print("SAL:");
  Serial.println(SAL);

  Serial.print("GRAV:");
  Serial.println(GRAV);
  Serial.println();
 
//f_ec= atof(EC);
}

Delta_G

Always check the documentation.

https://www.arduino.cc/en/Reference/softwareSerial

Check the part under "Limitations".  It lays this out pretty clearly on the first bullet there.  It even suggests another library to use. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

DirkDozer

Alright, I tried to use the AltSoftSerial library but that seemed to mess things up even more since then I didn't even get an output. I then learned that you *can* listen to two ports with software serial you just have to do a listen command. However, now my problem is that for some reason the mySerial0.listen(); doesn't seem to work despite it being just like the example code they provided. Here's my code again but slightly modified. It would be great if I could get a replacement that worked.

Code: [Select]
#include <SoftwareSerial.h>
#define rx 11
#define tx 10
#define rx1 8
#define tx1 9

SoftwareSerial mySerial(rx, tx);
SoftwareSerial mySerial0(rx1, tx1);

String inputString = "";
String sensorStringOne = "";
String sensorStringTwo = "";
boolean inputStringComplete = false;
boolean sensorStringCompleteOne = false;
boolean sensorStringCompleteTwo = false;
float pH;


void setup() {
  Serial.begin(9600);
  mySerial0.begin(9600);
  mySerial.begin(9600);
  inputString.reserve(10);
  sensorStringOne.reserve(30);
  sensorStringTwo.reserve(30);
}


void serialEvent() {
  inputString = Serial.readStringUntil(13);
  inputStringComplete = true;
}


void loop() {

  mySerial.listen();
  if (inputStringComplete == true) {
    mySerial.print(inputString);
    mySerial.print('\r');
    mySerial0.print(inputString);
    mySerial0.print('\r');
    inputString = "";
    inputStringComplete = false;
  }

  if (mySerial.available() > 0) {
    char inchar = (char)mySerial.read();
    sensorStringOne += inchar;
    if (inchar == '\r') {
      sensorStringCompleteOne = true;
    }
  }

  if (sensorStringCompleteOne == true) {
    if (isdigit(sensorStringOne[0]) == false) {
      Serial.println(sensorStringOne);
    }
    else
    {
      print_EC_data();
    }
    sensorStringOne = "";
    sensorStringCompleteOne = false;
  }
 
  // pH code kinda starts here

  ///*

  mySerial0.listen();
  if (mySerial0.available() > 0) {
    char inchar0 = (char)mySerial0.read();
    sensorStringTwo += inchar0;
    if (inchar0 == '\r') {
      sensorStringCompleteTwo = true;
    }
  }

  mySerial0.listen();
  if (sensorStringCompleteTwo == true) {
    Serial.print("pH: ");
    Serial.println(sensorStringTwo);
    sensorStringTwo = "";
    sensorStringCompleteTwo = false;
  }
  mySerial.listen();
  //*/
}

void print_EC_data(void) {

  char sensorstring_array[30];
  char *EC;
  char *TDS;
  char *SAL;
  char *GRAV;
  float f_ec;
 
  sensorStringOne.toCharArray(sensorstring_array, 30);
  EC = strtok(sensorstring_array, ",");
  TDS = strtok(NULL, ",");
  SAL = strtok(NULL, ",");
  GRAV = strtok(NULL, ",");

  Serial.print("EC:");
  Serial.println(EC);

  Serial.print("TDS:");
  Serial.println(TDS);

  Serial.print("SAL:");
  Serial.println(SAL);

  Serial.print("GRAV:");
  Serial.println(GRAV);
  Serial.println();
 
//f_ec= atof(EC);
}

Delta_G

When do the devices send data to you?  The thing is, you've got to listen when they send, not when you want to read. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

DirkDozer

Well, I have connected the Atlas Scientific pH and Conductivity sensor but they both send data to me.

Delta_G

Well, I have connected the Atlas Scientific pH and Conductivity sensor but they both send data to me.
Yes, I know they both send data to you.  The question is *when* do they send it.

What you need for this is something where you can tell the sensor that it is time to send the data, then listen to that line until you get the data and then tell the other sensor to send.  If they're both just spewing data without being told when to then you won't be able to do things this way. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jremington

#6
Oct 10, 2019, 03:06 am Last Edit: Oct 10, 2019, 03:47 am by jremington
Hardware serial ports work much better than software serial. You get an extra one with the Arduino Leonardo or other 32u4 boards, or four with the Arduino Mega boards.

DirkDozer

That's great! I'm using the Arduino Uno, does my board have two Hardware Serial ports I can use? If so I would love it if you could change my code to work with hardware serial.  :)

jremington

#8
Oct 10, 2019, 03:46 am Last Edit: Oct 10, 2019, 03:47 am by jremington
An Uno, or any Arduino based on an ATmega328 (Pro Mini, etc.), has only one hardware serial port.

Go Up