This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Cannot output 0x00 (null) character

I am trying to write a very simple program to output a serial string including hex codes when I press a button.

It works fine if the string does not include 0x00, but if it does this is treated as 'end of string' and does not output.

printf ("\x01\x02\x00\x03\x04")
only outputs up to 0x02.

similarly for;
SBUF =0x01
SBUF =0x02
SBUF =0x00
SBUF =0x03
SBUF =0x04

The program is based on the hello.c sample.

Help please

Parents
  • Getting back to Trevor's original question.

    I am trying to write a very simple program to output a serial string including hex codes when I press a button.

    Well, as has already been pointed out, the fundemental problem is that what you are trying to put out is not a string. Remember that string is just a convenient contraction of null terminated string and you will not fall into the same trap again.

    Since printf deals with null terminated strings, this is not the solution for you. A simple solution would be to write a loop something like this:

    loop = length_of_string;
    p    = address_of_string;
    
    do
    {
        putchar( *p++ );
    }while( --loop != 0 )
    

    I am not quite clear what you mean by:

    similarly for;
    SBUF =0x01
    SBUF =0x02
    SBUF =0x00
    SBUF =0x03
    SBUF =0x04
    
    If I understand this to be taken literaly, then it will not work. The minimum requirement for writing to the SBUF register is that before each write it is necessary to wait for the previous character to have been transmitted. i.e. check the TI flag.

Reply
  • Getting back to Trevor's original question.

    I am trying to write a very simple program to output a serial string including hex codes when I press a button.

    Well, as has already been pointed out, the fundemental problem is that what you are trying to put out is not a string. Remember that string is just a convenient contraction of null terminated string and you will not fall into the same trap again.

    Since printf deals with null terminated strings, this is not the solution for you. A simple solution would be to write a loop something like this:

    loop = length_of_string;
    p    = address_of_string;
    
    do
    {
        putchar( *p++ );
    }while( --loop != 0 )
    

    I am not quite clear what you mean by:

    similarly for;
    SBUF =0x01
    SBUF =0x02
    SBUF =0x00
    SBUF =0x03
    SBUF =0x04
    
    If I understand this to be taken literaly, then it will not work. The minimum requirement for writing to the SBUF register is that before each write it is necessary to wait for the previous character to have been transmitted. i.e. check the TI flag.

Children
  • "Remember that string is just a convenient contraction of null terminated string and you will not fall into the same trap again."

    However, if you try sending this down an RS232 link, you could fall into another trap:

    ASCII codes below 0x20 are control codes; eg
    0x0D is Carriage Return;
    0x0A is Line Feed;
    0x13 is XOFF;
    0x11 is XON.

    If you need to send pure binary data down an RS232 link, you need to check very carefully that the entire link supports it - an won't go interpreting some of your data as control codes!

    This is why we have things like Intel Hex, Motorola S-Records, MIME, etc for sending binary data...

  • loop = length_of_string;
    p    = address_of_string;
    
    do
    {
        putchar( *p++ );
    }while( --loop != 0 )

    This will fail if loop is ever zero, e.g. the string is zero bytes long. Safe coding practices would re-write this with a while (do-while's border on evil anyhow), e.g.
    while (loop--) { putchar(*p++); }