c++ send hex over uart, leading zero's are dropped on receiving side

Hi,

over the uart port I would like to send out following hexadecimal string C5C301000000000076.
Below there is a snippet of the code I have written but at the receiving end the leading 0’s are dropped.
I receive C5C310000076 instead.
Could anyone give me a hint or a solution?

unsigned char tx_buf[9] = {0xC5,0xC3,0x01,0x00,0x00,0x00,0x00,0x00,0x76}; //unsigned is 0<>255 signed is -128<>127
unsigned char *p_tx_buf = &tx_buf[0]; //points to the first element of the tx_buf

int fd;

//open uart2 for tx/rx, not controlling device
if((fd = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
printf(“Unable to open uart2 access .\n”);
else
printf(“uart2 opened succesfully.\n”);

if(write(fd,&tx_buf,sizeof(tx_buf))<0)
printf(“ERROR : tx not send.\n”);
else
for(unsigned int i = 0; i < sizeof(tx_buf); ++i)
{
std::cout << std::hex << (int)tx_buf[i];
}
printf(" : tx send \n");

I am not sure if I understand--please clarify.

Hi,

over the uart port I would like to send out following hexadecimal string
C5C301000000000076.

...

    unsigned char tx_buf[9] =
{0xC5,0xC3,0x01,0x00,0x00,0x00,0x00,0x00,0x76}; //unsigned is 0<>255 signed
is -128<>127
    unsigned char *p_tx_buf = &tx_buf[0]; //points to the first element of
the tx_buf

   int fd;

    //open uart2 for tx/rx, not controlling device
    if((fd = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY)) < 0)
        printf("Unable to open uart2 access .\n");
    else
        printf("uart2 opened succesfully.\n");

   if(write(fd,&tx_buf,sizeof(tx_buf))<0)

Ok, so you are not sending the hex string, but rather the binary
values. I never ran it this way so I am not sure what might prevent it
from working this way, but the following stack overflow discussion
might be helpful:
http://stackoverflow.com/questions/506805/binary-data-over-serial-terminal

            printf("ERROR : tx not send.\n");
        else
            for(unsigned int i = 0; i < sizeof(tx_buf); ++i)
                {
                    std::cout << std::hex << (int)tx_buf[i];
                }
            printf(" : tx send \n");

You do realize that the 'else' block contains just the for() {} loop
and not the printf statement?
(your indentation seems to indicate otherwise) I recommend using
curly braces always even if the conditional block has just one
statement.

I only get a case with insert several zero on special platform a few years ago

Finally , I can only use ascII string convert first .then transfer over uart and convert back in receiver

I dont know if this is related with chipset , This case is only happen on QNX platform 6.4.1

Could you show us the code on the receiving side?

How many characters do you receive?

Do you convert the receive bytes to strings before printing them?

I just saw that your tx buffer is different from your expected string
char tx_buf[9] = {0xC5,0xC3,0x01,0x00,0x00,0x00,0x00,0x00,0x76};

string C5C301000000000076

0x00 doesn’t mean two times 0. If you want to see 18 characters so you need to send 18 characters.