Search code examples
formatasciistm32transmitfile

Transmit commands in ASCII format STM32


I'm using STM32 discovery board to communicate to a device that takes ASCII commands.

I use HAL_UART_TRANSMIT_IT to send data, that works fine.

I want to send ASCII command XM3 to the device. When I use virtual port programs as realterm, I just click on ascii and put the baudrate, databitc etc etc and when I type in XM3 and click on +CR it send the command and it works fine, if CR is not included it doesent work.

When I try to do that from my MCU I use this code and it does not work, any ideas how to send ASCII commands in C via serial port?

char txD[3]="XM3";
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
HAL_UART_Transmit_IT(&huart1, (uint8_t *)txD ,3);

When I send this to realterm it shows XM3 but when I send this to the device nothing happens.

I need to know how I send XM3 and a CR to the device.


Solution

  • If you send the command via Realterm and check the +CR option, Realterm does append a Carriage Return, i.e. ASCII Code 13.

    In order to reproduce this behavior in your code, you should define the command as follows:

    char txD[4]="XM3\r";
    

    Respectively, if the receiver also expects to receive a Newline, i.e. ASCII Code 10, you should define it as follows:

    char txD[5]="XM3\r\n";