No Serial.available

Hello, a newbie here reading, lurking, learning and absolutely enjoying Arduino. I'm having trouble reading a serial response from a VDIP1 USB host module. Can read response OK with HT, but always get '-1' response back to rx. Code snippet below.

Is my use of data types the problem? Can rx be jumped on top of Arduino rx (or tx) w/o altering signal? I've continue to stumble up the learning curve, bit this one has me stalled. Any help appreciated. Thanks, Bob

const int ledPin3 = 3;
char diskPresent = 'N';      // to indicate presence of USB stick

void setup()
{
    
  Serial.begin(9600);          // initialize serial communication
  Serial.print("IPA");         // sets the vdip to use ascii numbers 
  Serial.print(13, BYTE);      //  end of message
  Serial.flush();              // clear buffer
  Serial.print (13, BYTE);      // send CR to query if disk present; returns 'D:\>' if so, 'No Disk' if not
                                 // these responses show on HT
  diskPresent = Serial.read();  // intent is to read only first char ('D' or 'N')

  if (diskPresent != 'N')
     {
           digitalWrite(ledPin3, HIGH);
     }

}

void loop()
{
}

You're not waiting for a character to become available.

while (Serial.available () == 0) {;}

Before the serial read may help.

The print of the carriage return returns as soon as the character is sent to the hardware transmit buffer, so your serial read executes mere microseconds later, whereas a reply would take at least 10ms to arrive.

Thanks for the fast reply. I shouldn’t have posted that snippet; more complete t/s code below (it did have serial.available). I’m still curious if my use of data types is a problem. Also, can HT rx or tx lines be jumped to the Arduino rx (or tx) w/o altering signal, or is splitting the serial signals problematic?

Actual test code below:

char diskPresent = 'N';      // to indicate presence of USB stick
char vRespData[9];           // array for response chars
char vRespChar;              // storage location for response character
int vIndex = 0;              // array index



void setup()
{
    
  Serial.begin(9600);          // initialize serial communication
  Serial.print("IPA");         // sets the vdip to use ascii numbers 
  Serial.print(13, BYTE);      //  end of message
  Serial.flush();              // clear buffer
  Serial.print (13, BYTE);      // send CR to query if disk present; returns 'D:\>' if so, 'No Disk' if not

  if (Serial.available() > 0) 
  {
   while(vIndex < 8) // One less than the size of the array
       {
           vRespChar = Serial.read();
           vRespData[vIndex] = vRespChar;
           vIndex++; // Increment
       }

           vRespData[vIndex+1] = '\0'; // Null terminate the string - should be +1?   
             diskPresent = vRespData[0];  // sets 'diskPresent' variable.  Should only be 'D' or 'N'
             vIndex=0;
  }
  
  while(vIndex < 8) // this loop is for troubleshooting.
       {
           vRespChar = vRespData[vIndex];
             Serial.print ("vIndex = ");  
             Serial.println (vIndex, DEC);
             Serial.print ("vRespChar = ");
           Serial.println (vRespChar); 
           vIndex++; 
       }
              Serial.print ("diskPresent = ");
            Serial.println (diskPresent); 

}

void loop()
{
}
while(vIndex < 8) // One less than the size of the array
       {
           vRespChar = Serial.read();
           vRespData[vIndex] = vRespChar;
           vIndex++; // Increment
       }

If there is no character available, serial read will return -1.- you need to check available before you read a character.
For the code above, you’ll end up with one valid character, and seven -1s

Thanks again. To be sure I understand...so it's not adequate to check serial.available once, before entering the while loop. Must check each time through while loop before serial.read?

Lots to learn! Cheers.

You can wait for Serial.available to return at least as many characters as you need at the top before calling read, or you can wait for one character at a time before calling read.

What you have to remember is that at 9600 baud, a character takes just over one millisecond to be transmitted, but a loop could repeat many hundreds of times in that period.