This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Send Hex commands with UART?



Hello everyone,

I'm trying to use my StellarisWare LM4F232 Evaluation kit to visualize some data on a touchscreen.

The touchscreenpart is not a problem, I used a terminal to check if the visualize part is ok by manually sending the hex-codes.

Now I want to replace the terminal by my kit and I use the uart_echo.c example as a start. The problem is that UARTSend characters sends.

So how can I modify the function to send pure hex-data? Or is there another function maybe?

Thanks in advance

  • If you need simple values you can use ANSI C \x escape sequence. 

    http://stackoverflow.com/questions/10057258/how-does-x-work-in-a-string


    If you open your uart_echo.c you will see: 

    void
    UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
    {
        //
        // Loop while there are more characters to send.
        //
        while(ulCount--)
        {
            //
            // Write the next character to the UART.
            //
            SoftUARTCharPut(&g_sUART, *pucBuffer++);
        }
    }

    All you need now is to call SoftUARTCharPut with appropriate parameters.

  • Thanks for your fast response.

    So for example:

    UARTSend((unsigned char *)"\x01",2);

    But I don't really understand how to call SoftUARTCharPut

    And I have: 

    ROM_UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++); instead of SoftUARTCharPut

  • Sorry I have opened softuart_echo.c instead of uart_echo.c. It is good that you checked it yourself. 

    All you need to use is ROM_UARTCharPutNonBlocking(UART0_BASE, 0xAB); where 0xAB is a hex value to be sent. Remember to include appropriate header files (/uart.h and /rom.h) to the file where you call the function.

  • No probs. I used the function like you said but the screen is still not responding:

    ROM_UARTCharPutNonBlocking(UART0_BASE,0x01);                                            // Write Cmd
    ROM_UARTCharPutNonBlocking(UART0_BASE,0x0F);                                            // Object Type
    ROM_UARTCharPutNonBlocking(UART0_BASE,0x00);                                            // Object ID
    ROM_UARTCharPutNonBlocking(UART0_BASE,0x00);                                            // MSB Data
    ROM_UARTCharPutNonBlocking(UART0_BASE,0xBD);                                           // LSB Data
    ROM_UARTCharPutNonBlocking(UART0_BASE,0xB3);                                            // Checksum

    The Hex-codes are right, I'm sure. 

  • You can try to use function UARTCharPut instead. When you use ROM_UARTCharPutNonBlocking you should check if it returns false. In that case buffer is full and you need to wait for it to empty. Please refer to Stellaris driverlib documentation: http://www.ti.com/lit/ug/spmu019o/spmu019o.pdf


    Please tell me if I understand your application correctly.

    • You have screen that you connect to PC via RS232. You can send some data over COM to screen to activate some functions.
    • Now, you want to replace PC with embedded device.


    If UARTCharPut fails you have some hardware or baud rate problem. What data you can see when you connect your Stellaris to PC?

    Can you share more details about the hardware?

    Regards,
    Maciej

  • Dear Maciej,

    Thanks for all the help, there was a minor error in the program. Now everything works!

    Regards,

    Ilias

  • i have the same problem, i want send commands HEX to my RFID.

        while(1) // Bucle: para siempre haciéndose eco de los datos a través de la UART.
        {
            ROM_UARTCharPutNonBlocking(UART0_BASE, '0x61');    //Envia el caracter "a" por la UART0
            ROM_UARTCharPutNonBlocking(UART0_BASE, '0x35');    //Envia el caracter "5" por la UART0

    }

    but my RFID not responds

  • Jorge Alejandro said:

    i have the same problem, i want send commands HEX to my RFID.

        while(1) // Bucle: para siempre haciéndose eco de los datos a través de la UART.
        {
            ROM_UARTCharPutNonBlocking(UART0_BASE, '0x61');    //Envia el caracter "a" por la UART0
            ROM_UARTCharPutNonBlocking(UART0_BASE, '0x35');    //Envia el caracter "5" por la UART0

    }

    but my RFID not responds

    The first mistake is that you are using the non-blocking versions of the UARTCharPut functions and not checking the return values.

    When using the non-blocking functions, you must check the return values and take appropriate action.  In most code, the non-blocking functions are not the correct ones to call, you should call the regular versions of the functions instead.

    The non-blocking functions will always return right away, whether or not they can perform the desired action.  This is useful when used in a polling loop where you can retry the action later if it doesn't succeed the first time, but until you need to do this and understand exactly what and why you are doing it, just call the regular functions.

    It's really annoying that the example programs use the non-blocking functions inappropriately and incorrectly and end up causing a lot of people to write bad code because of it.

    I'm guessing that the next mistake is that you don't want to actually send the letter 'a' followed by the digit '5', but actually want to send the byte value 0xa5 - but I don't know what your RFID is expecting.

    You don't say if you are successfully sending data at all, have you hooked up a scope to the lines?  Are your electrical connections correct?  Are you enabling and configuring the UART and its pins correctly?

  • thnks it worked, now the problem is my receiver but my programming problem.

  • Note that the UART itself just send bytes - it neither knows nor cares wheter these bytes represent ASCII characters, binary data, or whatever.

    So, as far as the UART is concerned, there is absolutely no difference whatsoever between sending "text" or "characters" or any form of "binary" data.

    The difference lies only in the 'C' language code used to manage the transmission; specifically:

    • 'C' language strings use the byte value zero (the ASCII NUL code) to mark the end of the string;
      therefore 'C' strings cannot be used to handle any data where zero is a valid value.
       
    • Consequently, any 'C' functions which rely upon 'C' strings cannot be used to handle any data where zero is a valid value.
       
    • In addition, some 'C' implementations of functions like printf, putchar, etc, may perform special processing on certain special characters - particularly, newline sequences;
      therefore, these should not be used with binary data!

    But, again, none of this has anything to do with the UART itself. It doesn't even have anything specifically to do with TI or Tiva - these are general 'C' programming issues!

  • And one more UART related point - if/when using a Terminal program to test/monitor - only a, "Displayable Character" will appear (or will appear correctly).  Usually - but not always - these are confined to the values 0x20 (space) thru 0x7F.

    When you seek to "send" a character outside of this range (again - using a terminal program) - it is likely that you'll have to first create a small text file - and transmit that whole file - rather than direct keyboard entry.  (some programs may accept & enable use of "special keys" - to work-around this displayable character restriction... i.e. Alt 248 may yield the degree ° symbol)