Serial Read Bluetooth

I am reading in via Bluetooth serial. The first two bytes provide the length of the message to follow. Once I calculate the length then I add the successive bytes(chars) to an input string until the total message is complete. I then resent the message using the same technique, two bytes for length and then the message as a test program. T

Irrespective of the number of chars sent the first char read after the length is known is always 0xFF. I cant see why this should be so. In this example I am just sending 500 digits.
Any suggestions.

void loop() 
  {
  while (SerialBT.available())
    {
      if (LengthKnown==false)
        {
        byte Low  = SerialBT.read();
        Serial.print("Low  ");Serial.println(Low,HEX);
        byte High = SerialBT.read();
        Serial.print("High ");Serial.println(High,HEX);
        MessageLength = Low + High*256;   
        Serial.print("Message Length ");Serial.println(MessageLength);
        SerialCounter=0;
        LengthKnown=true;
        }

      if (LengthKnown==true)                            // start reading rest of transmission
        {
        char inChar= SerialBT.read();
        inputString += inChar;                                   // add it to the inputString:
        Serial.print("Counter ");Serial.print(SerialCounter); Serial.print("  Char ");Serial.println(inChar, HEX);
        SerialCounter++;
        }
    }     
    
    if ((SerialCounter>=MessageLength-1)&&(SerialCounter!=0))
      {
      ResendString(inputString);      // resend the string for test prurposes
      SerialCounter = 0;
      inputString = "";
      MessageLength = 0;
      LengthKnown = false;
      }

}

This is the readout

      if (LengthKnown==true)                            // start reading rest of transmission
        {
        char inChar= SerialBT.read();

Are you sure that the data is available when you read it ?

Note that if no data is available then the value returned by the read() will be -1 which is 0xFF

or
0xFFFFFFFF?

or 0xFFFF
or 0xFFFFFFFFFFFFFFFF

depending on the size of your variable

Data type of the return value of Serial.read() is int. So, Serial.print(Serial.read(), HEX) should show: 0xFFFFFFFF?

My sending program is in Java and writes the high and low byte and then the whole word. There could be a transmitted break, but I have no way to find it as the send routine is within a not accessible library. I'll try adding the high and low sending byte to the front of the message and see it that makes a difference.

As to read RETURN the arduino definition says that the 'first byte of incoming serial data (of -1 if no data available). This doesn't really line up with a Data type of int

Serual.read() returns an unsigned int. If it returned a byte then it could not return -1

As to your problem, you really should be testing whether there is anything to read before reading each of the bytes, or testing whether the whole message (number of bytes) is available before reading it

Which it does.


void setup()
{
  Serial.begin(115200);
  Serial.println(Serial.read());
  Serial.println(Serial.read(), HEX);
  Serial.println("-------------------");
  byte aByte = Serial.read();
  Serial.println(aByte);
  Serial.println(aByte, HEX);
  Serial.println("-------------------");
  int anInt = Serial.read();
  Serial.println(anInt);
  Serial.println(anInt, HEX);
}

void loop()
{
}
-1
FFFFFFFF
-------------------
255
FF
-------------------
-1
FFFFFFFF

So what is your point ?

do you have a source of the Java program?
what is it running on, e.g. a PC, Raspberry Pi, Smartphone, ?
could you get a hex dump of the serial data using a terminal emulator such as RealTerm?

My point is:
return type of Serial.read() is int.
Why do the following codes show: 0xFFFFFFFF and not 0xFFFF?

whil(Serial.available() == 0)
{
   int y = Serial.read(); //y holds FFFF in hex as 2's complement = -1 in decimal
   Serial.println(y, HEX);  //shows: 0xFFFFFFFF
}

as stated by @UKHeliBob you need to check if data is available before attempting to read, e.g. as a quick test create a function to read a byte

byte SerialBTread() {
  while (!SerialBT.available());
    return SerialBT.read();
  }

and replace your calls to SerialBT.read() with calls to SerialBTread()

if I have a simple Java program which sends a test arrray

           byte data[] = new byte[] {0,0,'A','B','C'};
           int length=data.length-2;
           data[1]=(byte)(length>>8);data[0]=(byte)(length&0xff);
           comPorts[port].writeBytes(data,data.length);
           System.out.print("sending ");
           for(int i=0;i<data.length;i++)
             System.out.print(" " + data[i]);

the Java console displays

F:\Programming\JAVA\TerminalEmulators\JSerialComm>java -cp F:\Programming\JAVA\TerminalEmulators\JSerialComm\jSerialComm-1.3.11.jar;.  SimpleTerminalSendArray
List COM ports
comPorts[0] = Communications Port (COM1)
comPorts[1] = USB Serial Port (COM13)
comPorts[2] = Intel(R) Active Management Technology - SOL (COM10)
comPorts[3] = Silicon Labs CP210x USB to UART Bridge (COM4)
comPorts[4] = Standard Serial over Bluetooth link (COM16)
comPorts[5] = Standard Serial over Bluetooth link (COM17)

Enter array index to select COM port ?
4
open port comPorts[4]  Standard Serial over Bluetooth link (COM16)

sending  3 0 65 66 67

and the Arduino Serial Monitor displays

Low  3
High 0
Message Length 3
Counter 0  Char 41
Counter 1  Char 42
Counter 2  Char 43

Thanks all. I will try Horaces' method

I put the high and low byte onto the front of the message and sent the whole message with good results up to 383 characters (7F, 1) at 384 the low/High bytes read (3F,1) instead of (80,1).. Now I am confused again!!!

Tried Horaces' idea and worked without a fault. You really do have to look after each byte!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.