Test if software serial port is open

Hi, I am controlling a samsung tv via rs 232 using software serial on an arduino uno with link sprite rs-232 shield. I am using the usb port for control of the board so software serial is necessary.
Everything works as it should, except when the tv has no power. I need to test if the software serial port has opened (ie if the tv has power) and display an error message if it isnt.
My software serial instance is called TVRS232
I know you can use !(Serial) for the hardware uart port but when i try (!TVRS232) i get a compilation error;

Arduino: 1.5.6-r2 (Windows 8), Board: "Arduino Uno"

Using library SoftwareSerial in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial (legacy)



C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial C:\Users\Andrew\AppData\Local\Temp\build1531246962864305934.tmp\first_run.cpp -o C:\Users\Andrew\AppData\Local\Temp\build1531246962864305934.tmp\first_run.cpp.o 

first_run.ino: In function 'void loop()':
first_run.ino:209: error: no match for 'operator!' in '!TVRS232'
first_run.ino:209: note: candidates are: operator!(bool) <built-in>

Is this possible with software serial? What other approach could i take if its not? Below is my total sketch. Please mind my sloppy programming, still quite new to this :slight_smile:

#include <SoftwareSerial.h>

SoftwareSerial TVRS232(2, 3);
const int relay1 = 4;
const int relay2 = 5;
const int relay3 = 6;
const int relay4 = 7;

byte TV_ON[6]= {0xAA,0x11,0xFF,0x01,0x01,0x12};
byte TV_OFF[6] = {0xAA,0x11,0xFF,0x01,0x00,0x11};
byte TV_CHECK_POWER_STATUS[5] = {0xAA,0x11,0xFF,0x00,0x10};
byte TV_HDMI1[6] = {0xAA,0x14,0xFF,0x01,0x21,0x35};
byte TV_HDMI2[6] = {0xAA,0x14,0xFF,0x01,0x23,0x37};
byte TV_DP[6] = {0xAA,0x14,0xFF,0x01,0x25,0x39};
byte TV_CHECK_SOURCE[5] = {0xAA,0x14,0xFF,0x00,0x13};

byte TV_IS_ON [8] = {0xAA,0xFF,0xFF,0x03,0x41,0x11,0x01,0x54};
byte TV_IS_OFF [8] = {0xAA,0xFF,0xFF,0x03,0x41,0x11,0x00,0x53};
byte TV_IS_HDMI1 [8] = {0xAA,0xFF,0xFF,0x03,0x41,0x14,0x21,0x77};
byte TV_IS_HDMI2 [8] = {0xAA,0xFF,0xFF,0x03,0x41,0x14,0x23,0x79};
byte TV_IS_DP [8] = {0xAA,0xFF,0xFF,0x03,0x41,0x14,0x25,0x7B};
byte RESPONSE [8]  = {};

String inputString = "";
boolean stringComplete = false;

void tv_on() {
  TVRS232.write(TV_ON, 6);
  Serial.println(F("Display 'on' command sent"));
  Serial.println(F("Please wait 10 seconds while display is turning on........"));
  delay(10000);
  Serial.println(F("Display is on"));
}

void tv_off() {
  TVRS232.write(TV_OFF,6);
  Serial.println(F("Display 'off' command sent"));
}

void tv_hdmi1() {
  TVRS232.write(TV_HDMI1, 6);
  Serial.println(F("Display 'HDMI 1' command sent"));
}

void tv_hdmi2() {
  TVRS232.write(TV_HDMI2, 6);
  Serial.println(F("Display 'HDMI 2' command sent"));
}
  
void tv_displayport() {
  TVRS232.write(TV_DP,6);
  Serial.println(F("Display 'display port' command sent"));
}

void setup() {
  // put your setup code here, to run once:

pinMode (relay1, OUTPUT);
pinMode (relay2, OUTPUT);
pinMode (relay3, OUTPUT);
pinMode (relay4, OUTPUT);

digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);

Serial.begin(19200);
TVRS232.begin(9600);
inputString.reserve(200);


}

void loop() {
  // put your main code here, to run repeatedly:

serialEvent();

if (stringComplete) {
 
  
      
  
    if (inputString == "do") {
     tv_on();
    }
    if (inputString == "df") {
     tv_off();
    }
    if (inputString == "h1") {
      tv_hdmi1();
    } 
    if (inputString == "h2") {
      tv_hdmi2();
    }
    if (inputString == "dp") {
      tv_displayport();
    }
    
    if (inputString == "rc") {
      digitalWrite(relay1, HIGH);
      Serial.print(F("please wait, pc resetting....."));
      delay(10000);
      digitalWrite(relay1,LOW);
      Serial.println(F("pc reset"));
      Serial.println(F("Please allow time for pc to boot"));
    }
    if (inputString == "rp") {
      digitalWrite(relay2, HIGH);
      Serial.print(F("please wait, raspberry pi resetting....."));
      delay(10000);
      digitalWrite(relay2,LOW);
      Serial.println(F("raspberry pi reset"));
      Serial.println(F("Please allow time for raspberry pi to boot"));
    }

    if (inputString == "r3") {
      digitalWrite(relay3, HIGH);
      Serial.println(F("please wait, auxillary relay 3 resetting....."));
      delay(10000);
      digitalWrite(relay3, LOW);
      Serial.println(F("auxillary relay 3 reset"));
    }
    
    if (inputString == "r4") {
      digitalWrite(relay4, HIGH);
      Serial.println(F("please wait, auxillary relay 4 resetting....."));
      delay(10000);
      digitalWrite(relay4,LOW);
      Serial.println(F("auxillary relay 4 reset"));
    }
    
    if (inputString == "co") {
      digitalWrite(relay1, LOW);
      Serial.println(F("pc turned on"));
    }
    
    if (inputString == "cf") {
      digitalWrite(relay1, HIGH);
      Serial.println(F("pc turned off"));
    }
    
    if (inputString == "po") {
      digitalWrite(relay2, LOW);
      Serial.println(F("raspberry pi turned on"));
    }
    
    if (inputString == "pf") {
      digitalWrite(relay2, HIGH);
      Serial.println(F("raspberry pi turned off"));
    }
          
    if (inputString == "3o") {
      digitalWrite(relay3, LOW);
      Serial.println(F("Relay 3 turned on"));
    }      
    
    if (inputString == "3f") {
      digitalWrite(relay3, HIGH);
      Serial.println(F("Relay 3 turned off"));
    }
    
    if (inputString == "4o") {
      digitalWrite(relay4, LOW);
      Serial.println(F("Relay 4 turned on"));
    }
    
    if (inputString == "4f") {
      digitalWrite(relay4, HIGH);
      Serial.println(F("Relay 4 turned off"));
    }

    if(inputString == "qs" && (!TVRS232)) {
      Serial.println("Display unresponsive, please check power");
    }
    
    if (inputString == "qs") {
      
      while (TVRS232.available()) {
       TVRS232.read();
      }
      TVRS232.write(TV_CHECK_POWER_STATUS,5);
      delay(200);
      while (TVRS232.available()<8) {} // Wait 'till there are 9 Bytes waiting
      for(int n=0; n<8; n++)
      RESPONSE[n] = TVRS232.read(); // Then: Get them.
      //Serial.write(RESPONSE,8);
      if (RESPONSE[7] == TV_IS_OFF[7]) {
        Serial.println("Display is off");
      }
      if (RESPONSE[7] == TV_IS_ON[7]) {
        Serial.println("Display is on");
      }
    }
  
    
      if (inputString == "qs" && RESPONSE[7] == TV_IS_ON[7]) { 
        while (TVRS232.available()) {
       TVRS232.read();
      }
       TVRS232.write(TV_CHECK_SOURCE,5);
       delay(200);
       while (TVRS232.available()<8) {} // Wait 'till there are 9 Bytes waiting
       for(int n=0; n<8; n++)
       RESPONSE[n] = TVRS232.read(); // Then: Get them.
       //Serial.write(RESPONSE,8);
       if (RESPONSE[7] == TV_IS_HDMI1[7]) {
        Serial.println("HDMI 1 is selected");
        }
       if (RESPONSE[7] == TV_IS_HDMI2[7]) {
        Serial.println("HDMI 2 is selected");
        }
       if (RESPONSE[7] == TV_IS_DP[7]) {
        Serial.println("Display port is selected");
        }
      
      
    } 
    
   // }
 
     if (inputString != "h" && inputString != "do" && inputString != "df" && inputString != "h1" && inputString != "h2" && inputString != "dp" && 
         inputString != "rc" && inputString != "rp" && inputString != "r3" && inputString != "r4" && inputString != "co" && inputString != "cf" && 
         inputString != "po" && inputString != "pf" && inputString != "3o" && inputString != "3f" && inputString != "4o" && inputString != "4f" && 
         inputString != "qs") {
       Serial.println(inputString);
       Serial.println("Command not recognised");
    }
    
    inputString = "";
    stringComplete = false;
    }

}


void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    
    if (inChar == '\r') {
      stringComplete = true;
    }
    else {
      inputString += inChar;
    
    }
  }
}

Thank you

Everything works as it should, except when the tv has no power.

That is to be expected.

I need to test if the software serial port has opened (ie if the tv has power) and display an error message if it isnt.

You need to detect comm loss and display an error message. One reason for comm loss would be that the TV has no power. Presumably the serial communication is two way and you can detect the lack of response from the TV?

Hi have worked out a very simple solution. I know exactly when my data will arrive. So, if it doesnt arrive after a certain time, its not going to arrive. Added a small delay and a serial available call. Works great. Thank you for your answer