In need of a watchdog-style SoftSerial timer

I was wondering if there is any way to time out a for loop if I don’t get data back from a softserial.read().

#include <SoftwareSerial.h>
#include "LedControl.h"

#define rxPin 2                                                // Define the receive pin on the arduino (this pin should connect to the TX pin of the xPort)
#define txPin 3                                                // Define the transmit pin on the arduino (this pin should connect to the RX pin of the xPort)
#define rstPin 4                                               // Define the reset pin on the arduino (this pin should connect to the RST pin of the xPort)
#define GETCommand ("GET /displayfiles/da.php")                // This is the page that we want to connect to on the server (the server is defined in the xPort setup)

SoftwareSerial SoftxPort = SoftwareSerial(rxPin, txPin);       // Define out softwareserial port as SoftxPort, it will use rxPin and txPin
LedControl lc=LedControl(12,11,10,1);                          /* Pin 12 is connected to the DataIn 
                                                                  Pin 11 is connected to the CLK
                                                                  Pin 10 is connected to LOAD 
                                                                  We have only a single MAX7219.*/

byte pinState = 0;

void setup()   {
  pinMode(rxPin, INPUT);                                       // Set the receive pin as an input
  pinMode(txPin, OUTPUT);                                      // Set the transmit pin as an output
  pinMode(rstPin, OUTPUT);                                     // Set the reset pin as an output
  
  SoftxPort.begin(9600);                                       // Start the softserial port at 9600
  Serial.begin(9600);                                          // Start the serial port at 9600
  
  lc.shutdown(0,false);                                        // The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
  lc.setIntensity(0,15);                                       // Set the brightness to a medium values
  lc.clearDisplay(0);                                          // And clear the display
}  

void loop()   {
  
digitalWrite(rstPin, LOW);                                     // Ground the reset of the xPort causing it to reset
delay(100);                                                    // Wait for 100ms before releasing the reset pin
digitalWrite(rstPin, HIGH);                                    // Release the reset pin from ground
delay(10000);                                                  // Wait for 10 seconds before going any further, this allows the xPort time to reboot, and connect to the server

SoftxPort.print(GETCommand);                                   // Send the GET command specified in the begining of the sketch
SoftxPort.print("\n");                                         // Send a carriage return
SoftxPort.print("\n");                                         // Send a carriage return (you need to send two carriage returns to actually send the command)

char rdata = 0;                                                // Our variable, rdata should equal zero for now
char response[2] = {0,0};                                      // We want this variable to have two digits since the number returned from the server could be a two digit number


[glow]for (int x = 0; x < 2; x++)[/glow]
 [glow]{[/glow]
 [glow]rdata = SoftxPort.read();[/glow]                                     // Specify that rdata's value should be what the SoftxPort reads; what the xPort receives from the server
 [glow]response[x] = rdata;[/glow]                                          // Specify that response should equal the same as rdata
 [glow]continue;[/glow]
 [glow]}[/glow]

  lc.clearDisplay(0);                                          // Clear the display
  lc.setChar(0,0,response[0],false);                           // Send the first character in the array (response) to the first max (0), the first display (0)
  lc.setChar(0,1,response[1],false);                           // Send the second character in the array (response) to the first max (0), the second display (1)

for (int x = 0; x < 2; x++)                                    // This will just show us what we received
 {                                                             // We don't need this but it's a good way to make sure that everything is working
 Serial.print(response[x]);      
  }

Serial.println();
delay(5000);
}

In the first for loop, I need to read data from the softserial port. Sometimes, for whatever reason, I do not get a response and the sketch locks up right there. I need a kind of timer that these commands can run under so that if I don’t get anything from the softserial read, it will just reset the arduino. Any suggestions?

That's one of the major limitations of the SoftwareSerial library. Instead use a newer and better software serial library like NewSoftSerial. Then you'll have access to interrupt-based buffered receives, faster speeds, and the available() function.

Have you tried using:

char c;

 if (Serial.available() > 0)
   c = Serial.read();

This way you might run into less trouble since it only reads when something is actually available.

He's using the SoftwareSerial library - not the hardware UART. There is no available() function because SoftwareSerial can't receive data asynchronously and buffer it.

Good deal, thanks I'll give that a shot and let you know how it goes.