Thread: Understanding the sprintf function

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Understanding the sprintf function

    I want to transmit a variable over UART and also LCD, the UART is a generated function with argument of uint8_t, the data i need to transmit is both signed and unsigned and it is 16 bit. How do i transmit the signed value if the argument is unsigned value?
    Code:
    void USARTWrite(uint8_t txData)
    {
    }
    My array is of size of 100 of bit16. To transmit the data i have used the sprintf function and i have made mistake in that as well. I have written something like this
    Code:
    void Send(int16_t a_data_s16)
    {
        int16_t l_buffer_a[10];
        uint8_t l_index_u8=0;
        sprintf(l_buffer_a,"%d",a_data_s16);
        while(l_buffer_a[l_index_u8] != 0)
        {
           USARTWrite((uint8_t)l_buffer_a[l_index_u8]);
           l_index_u8++;
        }
    }
    I realized that int sprintf(char *str, const char *string,...);
    , so i made a mistake of making it as int16_t. I have completely messed up all the code. What is the best approach now. Please help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The UART is just a transport between two end points.

    How you send "My array is of size of 100 of bit16." depends entirely on what the other end of the UART (the receiver) expects to see.

    > void Send(int16_t a_data_s16)
    Despite your naming suggesting this is an array, it isn't.

    > so i made a mistake of making it as int16_t.
    Well you already seem to know that the sprintf buffer should be an array of char, so what's the problem with making it so?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    I have multiple doubts, hopefully i can get clarified here

    Code:
    int16 datatotransmit[100];
    
    int main()
    {
      while (index <= 90)   /*transmitting 90 elements of datatotransmit array */
      {
         send(datatotransmit[index]);
         index++;
      }
    }
    
    void send(int16 data)
    {
    char buffer[10]; 
    sprintf(buffer,"%d",data);
    while(buffer[index] != 0)
     {
        UartSend((uint8)buffer[index]);
         index++;
     }
    
    }
    
    /*UartSend is the function generated by the library which accepts uint8*/
    UartSend(uint8 data)
    {
       /*Function to send data over UART*/
    }
    datatotransmit is an array filled by other function, let us say it has filled with the below values

    Code:
    datatotransmit[0] = 123;
    datatotransmit[1] = 512;
    datatotransmit[2] = -400;
    datatotransmit[3] = -512;
    Q1. Is the send function correct?
    Q2. For example if the datatotransmit[0] = 123;
    then input to UartSend will be
    Ascii value of '1', '2', '3' is it correct? and for negative values it will be for example -512
    Ascii value of '-', '5','1','2' ? created by Send() function.

    Please help.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks OK, does it work?
    Do you get anything at the receiver?

    > sprintf(buffer,"%d",data);
    You might want to add a space at the end.
    sprintf(buffer,"%d ",data);

    Otherwise your receiver will get strings like
    "123512-400-512"

    I'm guessing you'll be wanting to do something with the string to extract the original integers.

    "123 512 -400 -512 "

    Is going to be a lot easier to parse with separators between each field.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    35
    Yes I receive data, I had problems receiving for higher values, then I figured one mistake of argument, I defined as uint instead of the signed. I am having difficulty in transmitting new line hence inserted comma between numbers.
    The other thing I want to try is to do integer multiplication and insert a dot at a fixed place. For example if it is 412, I want to display 4.12V. I am trying on Uart and LCD as well. May be I will attempt and show the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault from sprintf function
    By stdio in forum C Programming
    Replies: 2
    Last Post: 03-11-2012, 03:30 AM
  2. Sprintf function
    By chasesg in forum C Programming
    Replies: 7
    Last Post: 10-13-2007, 04:38 PM
  3. Use of a sprintf ( ) function ?
    By bigjoke in forum C Programming
    Replies: 3
    Last Post: 01-15-2006, 02:09 PM
  4. sprintf-like function
    By Pea in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:02 PM
  5. alternate function to sprintf
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2002, 10:38 PM

Tags for this Thread