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;
}
}
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
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
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!!!