Go Down

Topic: Hardware switch to switch the software serial pins being "transceived" on. (Read 879 times) previous topic - next topic

Charles_Gibbs

Hi Robin2,

Yes, the data was ONLY veiwable in the serial monitor. The Victron Software could NOT sens the devices with this code, the likely hood that I screwed the code up is VERY good.

Code: [Select]

#include <SoftwareSerial.h>
 
SoftwareSerial MPPT(3, 5); //Naming Pins And Input
SoftwareSerial BMV(6, 9); //Naming Pins And Input
const int threshold = 800; // Tells when to change state

void setup() {
  // initialize both serial ports:
  Serial.begin(19200);
  MPPT.begin(19200); // RX 3 TX 5
  BMV.begin(19200); // RX 6 TX 9
}

void loop() {
   int analogValue = analogRead(A0);
  // read from port 1, send to port 0:
   if (analogValue > threshold)
   if (MPPT.available()) {
   int inByte = MPPT.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
   MPPT.write(inByte);
  }

       analogValue = analogRead(A0);
   if (analogValue < threshold)
   if (BMV.available()) {
   int inByte = BMV.read();
    Serial.write(inByte);
  }

 
  // read from port 0, send to port 1:
    if (Serial.available()) {
    int inByte = Serial.read();
   BMV.write(inByte);
  }}


Hope this is clear enough, let me know if not and I will do my best to re-phrase.

Kind Regards
Charles.

Robin2

Your code is very confusing because it is creating local variables called inByte all over the place. And it is not using .listen() to change the serial inputs

Try this version
Code: [Select]

#include <SoftwareSerial.h>
 
SoftwareSerial MPPT(3, 5); //Naming Pins And Input
SoftwareSerial BMV(6, 9); //Naming Pins And Input
const int threshold = 800; // Tells when to change state

byte ssByte; // for the bytes that come via SoftwareSerial
byte pcByte; // for the bytes that come from the PC

void setup() {
    // initialize both serial ports:
    Serial.begin(19200);
    MPPT.begin(19200); // RX 3 TX 5
    BMV.begin(19200); // RX 6 TX 9
}

void loop() {
    int analogValue = analogRead(A0);
    // read from port 1, send to port 0:
    if (analogValue > threshold) {
        MPPT.listen();
    }
    else {
        BMV.listen();
    }
       
       
    if (MPPT.available()) {
        ssByte = MPPT.read();
        Serial.write(ssByte);
    }
    else if (BMV.available()) {
        ssByte = BMV.read();
        Serial.write(ssByte);
    }

    if (Serial.available()) {
        pcByte = Serial.read();
        if (MPPT.isListening()) {
            MPPT.write(pcByte);
        }
        else if (BMV.isListening()) {
            BMV.write(pcByte);
        }

    }
}


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

Charles_Gibbs

Hi Robin 2.

Like I said programming and I, not really good, I HAVE ALLOT!!!!! To learn, that was code that I pulled from a wiki page and just renamed serial1 to softwareSerial.

I then also renamed the devices......

And then in my crudeness the switch code......

Thanks Robin for all your help AND understanding. I am working three late shifts from tonight until friday night so more than 12 hours a peice, if I do get a chance to test  before that I will let you know.

Where can I find a manual for the Arduino code not just bits and peices but the whole manual maybe in PDF?


Robin2

Where can I find a manual for the Arduino code not just bits and peices but the whole manual maybe in PDF?
I don't think there is such a manual.

Arduinos are programmed with standard C++ code

All of the standard Arduino specific features such as digitalWrite() are documented in Reference pages of this website.

Documentation for the features in additional libraries depends on the author of the library and, unfortunately, many Open Source programmers are not interested in, or are not good at, writing documentation.

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

Go Up