Serial.write has problem when i try to send 0x00 and PC can't get right message

Dear all:

I need help.

I try to send HEX message to my PC software debugging port.

send message: 0xF7,0xF8,0x01,0x38,0x06,0x08,0x01,0x00,0x08,0x00

arduino code:

Serial.write(0xF7);
Serial.write(0xF8);
Serial.write(0x01);
Serial.write(0x38);
Serial.write(0x06);
Serial.write(0x08);
Serial.write(0x01);
Serial.write(0x00);
Serial.write(0x08);
Serial.write(0x00);

But my PC software debugging show:

F7 F8 01 38 06 08 01 00 48 00
F7 F8 01 38 06 08 01 00 48 02
F7 F8 01 38 06 08 01 00 08 10
F7 F8 01 38 06 08 01 00 08 00
........

the received is sometimes wrong and sometimes correct. why? and how to fix it?
please help...

First, what happens if you remove the write for binary zero? Second, place the values in an array and use Serial.write(array, length) to write it out, but also check the return value to see how many bytes were written:

// all of the normal code...

byte myArray[] = {0xF7,0xF8,0x01,0x38,0x06,0x08,0x01,0x00,0x08,0x00};
int written;

written = Serial.write(myArray, sizeof(myArray));
Serial.print("bytes written = ");
Serial.println(written);

interesting.

I try to change the code to like below. the communication is better. PC part alway get the correct bytes. but i don't know why...

serialOutside.write((byte)0xF7);
serialOutside.write((byte)0xF8);
serialOutside.write((byte)0x01);
delay(20);
serialOutside.write((byte)0x38);
serialOutside.write((byte)0x06);
serialOutside.write((byte)0x08);
delay(20);
serialOutside.write((byte)0x01);
serialOutside.write((byte)0x00);
serialOutside.write((byte)0x08);
delay(20);
serialOutside.write((byte)0x00);

First thought is that you have a bug in your debugging software on the PC. So what is your debugging software? And what is a "PC software debugging port"?

If you suspect the Arduino code to be the issue, you will have to post the complete Arduino code (or a representative example that exhibits the behaviour).

If you wrote the debugging software for the PC yourself and you suspect that to be the culprit I would if I wrote it myself), it would be advisable to post the code for that.

@q164129345

The following program sends the characters/digits of an array from UNO to the Serial Monitor of the IDE:

byte myArray[] = {0xF7,0xF8,0x01,0x38,0x06,0x08,0x01,0x00,0x08,0x00};
byte y;

void setup()
{
  Serial.begin(9600);
  for (int i=0; i<10; i++)
  {
    y = myArray[i];
    y= y >> 4;
    sendCharacter();  //1st charctaer of a byte 
    //----------------
    y = myArray[i];
    y = y & 0x0F;
    sendCharacter(); //2nd charcater of a byte    
  }
  Serial.println();
}

void loop()
{

}

void sendCharacter()
{
  {
    if (y <=9)
      y = y + 0x30;
    else
      y = y + 0x37;
  }
  Serial.write(y);   //prints: F7 F8 01 38 06 08 01 00 08 00
}

q164129345:
interesting.

I try to change the code to like below. the communication is better. PC part alway get the correct bytes. but i don't know why...

I reckon the problem is in the program that is receiving the data. The Arduino code in your Original Post should work fine.

...R