Search code examples
stm32esp8266at-commandkeilmessagebroker

sending null with HAL_UART_Transmit


I'm using HAL driver for STM32103F in Keil IDE. I need to transmit a code to an ESP which is connected to my STM32 via serial port. this code makes ESP publish whatever is in the command (CMD2) to the broker that I'm using, but the problem occurs when the command (CMD2) contains 0x00 (NULL), so the ESP does not publish anything to the broker. this is the code that I'm using:

char Appendix[8] = "\",0,0\r\n";

// DataLength = the length of the data stored in CMD2
// CMD2 might contain 0x00 in it
for(int i = 0; i < 7; i++) CMD2[DataLength+i]   = Appendix[i]; 
CMD2[DataLength+7] = 0x00;

sprintf(PubTopic, "AT+MQTTPUB=0,\"Data/%s\",\"", SerialNumber);
        
SizeOfPub = strlen(PubTopic);
        
for(int i = SizeOfPub; i < SizeOfPub+DataLength+8; i++) PubTopic[i] = CMD2[i-SizeOfPub];
        
HAL_UART_Transmit(huart, (uint8_t *) PubTopic, SizeOfPub+DataLength+7, 10);

when my command (CMD2) contains 0x00 (NULL), ESP does not act correctly to the last line, but this code works fine when there is no 0x00 (NULL) in the command(CMD2). for example:

1)CMD2 = 0x45 0x55 0x53

2)CMD2 = 0x45 0x00 0x53

in the first case, there is no problem with the code, but in the second case, ESP does not publish anything.


Solution

  • Thanks to , I figured out that I have followed the wrong path. The best way of publishing a code using AT+COMMAND that may contain the NULL character is to use AT+MQTTPUBRAW. this is my code:

    sprintf(PubTopic, "AT+MQTTPUBRAW=0,\"Data/%s\",%d,0,0", SerialNumber, DataLength);
            
    StrPrintln(&huart1, PubTopic);
        
    HAL_Delay(50);
    
    /*
    * SendCommand is a uint8_t variable that contains commands
    * that need to be published
    */
    HAL_UART_Transmit(&huart1, SendCommand, DataLength, 10);