Conversion problem

I just made this code for communication between 2 xbee. I work pretty well, except for 2 things....

In case 1, it properly recognized that RxBuffer[0] == 0x7E.
It go trough every case, but at case 3, it recognized Rxbuffer[3] == 0xFF. It should be 0x81. ----IT IS surely in reality coz it wouldnt work! so how can I see this 0x81 instead of 0xFF!!??

If I try to put TxBuffer[8] = RxBuffer [3]; to see what was was the unit, it return me 0xFF
If I try to put TxBuffer[9] = RxBuffer [9]; to see what was was the unit, it return me 0xFF just like RxBuffer[3];

***RxBuffer[9] is a value that change from 0x00 to 0x255 (Joystick in labview)

So I think it must be a conversion type that fc my result between Read.serial and Write.serial.

I print my result in HEX in labview

So is there any conversion type I should do ALWAYS to <> with HEX value????

DO you see any errors that could make that???

code
int serialCount = 0; // A count of how many bytes we receive
int n = 0;

unsigned char RxByteCount, RxSerialState, ValidMsgRxied, RxError;
unsigned char TxXsum,RxXsum,RxXSum2;
unsigned char RxBuffer[11];
unsigned char TxBuffer[11];
char rx;
int p;
int led = 13;
byte test, test2;

void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop()
{
//digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(100); // wait for a second
//digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
//delay(100); // wait for a second

//digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

switch(RxSerialState)
{
case 0: // Check for Start Delimiter
if(RxBuffer[0]== 0x7E){
RxSerialState=1;
RxXsum=0x00;
ValidMsgRxied=0;
RxError=0;
}
else
RxSerialState=0;
break;

case 1: // Read Byte Count High
RxSerialState=2;
break;

case 2: // Read Byte Count Low
RxByteCount = RxBuffer[2];
if ( (RxByteCount < 30) || (RxByteCount > 0) )
RxSerialState=3;
else
RxSerialState=0;
break;

case 3: // Read API ID
RxXsum = RxXsum + RxBuffer[3];
p=4;
RxByteCount--;
if ( RxBuffer[3] == 0xFF){ // API Type=0x81 sur XBEE;
RxSerialState=4;
}
else
RxSerialState=0;
break;

case 4: // Read Data
RxXsum=RxXsum+RxBuffer[p];
p++;
if (RxByteCount == 0)
{
RxSerialState=5;
RxXsum=255-RxXsum;
}
RxByteCount--;
break;

case 5: // Read XSUM
//if ( (RxXsum==RxBuffer[p]) && (RxByteCount == 0) )
//{
ValidMsgRxied=1;
RxSerialState=0;
//}
//else
// RxError=1;
break;
} // end switch

if(ValidMsgRxied == 1){
//digitalWrite(led, HIGH);

//GetData[9] = RxBuffer[3];

test = 10;
test2 = 20;
Serial.print(test2, HEX);

TxBuffer[0] = 0x7E;
TxBuffer[1] = 0x00;
TxBuffer[2] = 0x07;
TxBuffer[3] = 0x01;
TxBuffer[4] = 0x00;
TxBuffer[5] = 0x00;
TxBuffer[6] = 0x01;
TxBuffer[7] = 0x01;
//TxBuffer[8] = Serial.print(RxXsum, DEC);
//TxBuffer[9] = Serial.print(RxByteCount, HEX);
//TxBuffer[8] = RxXsum;
//TxBuffer[9] = RxXSum2;
TxBuffer[8] = RxBuffer[8];
TxBuffer[9] = test2; //test2 is now 0x14

TxXsum = 0xFF - ( TxBuffer[3] + TxBuffer[4] + TxBuffer[5] + TxBuffer[6] + TxBuffer[7] + TxBuffer[8] + TxBuffer[9] );

TxBuffer[10] = TxXsum;

Serial.write(TxBuffer,sizeof(TxBuffer));
serialCount = 0;
ValidMsgRxied=0;
//digitalWrite(led, LOW);
}

}

void serialEvent() {
while (Serial.available()) {

serialCount = 0 ;

for(int n=0; n <= 10; n++){

digitalWrite(led, HIGH);
RxBuffer[n] = Serial.read();
serialCount++;

digitalWrite(led, LOW);

}
}
}
/code

Please edit your post and place your code between code tags ([ code ] and [ /code ])

This is not right. If there is something in the serial buffer, it returns that character. If not, it returns an integer -1. That would be 0xFFFF.

void serialEvent() {
  while (Serial.available()) {

    serialCount = 0 ;

    // here is the problem. You are reading 11 characters
    // and there probably isn't 11 in the buffer yet
    // By the sound of your problem, there were only three in the buffer.
    for(int n=0; n <= 10; n++){

      digitalWrite(led, HIGH);
      RxBuffer[n] = Serial.read();
      serialCount++;

      digitalWrite(led, LOW);

    } 
  } 
}
while (Serial.available()) {

You should use this to check if the number of characters in the buffer is at least the number you want to read:

if(Serial.available()>=11) {

You haven't made the suggested changes to your serialEvent

majenko:

while (Serial.available()) {

You should use this to check if the number of characters in the buffer is at least the number you want to read:

if(Serial.available()>=11) {

Thanks!! That was all about!

Now, the only problem left is that when the TxBuffer is sent, I received it in an aleatory order. Sometime its correct, sometimes my start delimiter (0x7E) is somewhere between [0] and [10]. Is there a command I should use before or during my serial.write???

 Serial.write(TxBuffer,sizeof(TxBuffer)); 
    serialCount = 0;
    ValidMsgRxied=0;

Now, the only problem left is that when the TxBuffer is sent, I received it in an aleatory order

You received it in what order?

I would use something like this.

// this should be global.
int serialCount = 0;

void serialEvent() {
  while (Serial.available()) {
      char ch = Serial.read();
      // If it is the start character, reset the index
      if(ch == 0x7E) serialCount = 0;

      if(serialCount < 11)
      {
         RxBuffer[serialCount] = ch;
         serialCount++;
      }
    } 
  } 
}

Warning: I have not tested this.

a·le·a·to·ry
adj.

  1. Dependent on chance, luck, or an uncertain outcome: an aleatory contract between an oil prospector and a landowner.
  2. Of or characterized by gambling: aleatory contests.
  3. also a·le·a·to·ric: Music Using or consisting of sounds to be chosen by the performer or left to chance; indeterminate: An object placed inside the piano added an aleatory element to the piece.

PaulS:

Now, the only problem left is that when the TxBuffer is sent, I received it in an aleatory order

You received it in what order?

I mean, sometimes my start delimiter (0x7E) is somewhere between RxBuffer [0] and [10]. ---I am now talking about the RxBuffer at the computer when my arduino sent it.

Serial.write(TxBuffer,sizeof(TxBuffer)); 
    serialCount = 0;
    ValidMsgRxied=0;

This code is sending my TxBuffer (in the arduino) and I cant received like I put the value in the arduino from TxBuffer[0] to [10]

TxBuffer[0] is 0x7E at serial.write, but when I received it in my labview console, the order isnt the same.

TxBuffer[0] is 0x7E at serial.write, but when I received it in my labview console, the order isnt the same.

Is it the order, or the position in the array? Is everything after 0x7E in the correct order? If so, you have the same challenge there. You must reset the array index in the labview code when you receive the 0x7E character.

SurferTim:

TxBuffer[0] is 0x7E at serial.write, but when I received it in my labview console, the order isnt the same.

Is it the order, or the position in the array? Is everything after 0x7E in the correct order? If so, you have the same challenge there. You must reset the array index in the labview code when you receive the 0x7E character.

after 0x7E everything is correct! But its not even an array in labview. I do a VISA read than display it in string indicator.....

If it is in the correct order, and just not in the correct position in the array, then the labview code would be where the challenge is. It needs to empty the string or reset the array index when it gets the start character. Once you get that corrected, all should be ok.

SurferTim:
If it is in the correct order, and just not in the correct position in the array, then the labview code would be where the challenge is. It needs to empty the string or reset the array index when it gets the start character. Once you get that corrected, all should be ok.

I just found the problem!!!!

I realize my arduino my ALWAYS sending packets, even if it wasnt receiving data.

I resolve my problem by RxBuffer[0] = 0x00;

So after receiving 1 valid message, it wait for another because it cant no more go in the case.

Would it be possible to put my SWITCH CASE in the serial event? This way I would not need to do RxBuffer[0] = 0x00;???

Serial.write(TxBuffer,sizeof(TxBuffer)); 
    serialCount = 0;
    ValidMsgRxied=0;
    RxBuffer[0] = 0x00;