0

I am looking into some unexpected behavior on a terminal output regarding receiving a null char '\0'. I am sending a string which I am expecting will contain 2 \0, but this is not displayed on PuTTY.

According to this question it is implied that PuTTY does not send null under normal circumstances. However that question is approaching 10 years old and I would assume that PuTTY has received updates during that period.

I have looked into the PuTTY user manual and have mainly found documentation on PuTTY's panels and a lot on error messages and connectivity. Chapter 3.3 discusses "Altering your character set configuration" but is mainly involved with non-latin alphabets, and it links to Chapter 4.10, which is the translation panel.

My question is simply, what does PuTTY normally do when it needs to receive a \0 char over? Does it not receive it? Does it receive the char but is not displayed in the terminal?

Please let me know if more information is required.

Code used for sending data to PuTTY is in C, on an STM32 board:

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);

  memset (buffer, '\0', 64);  // clear the buffer
  uint8_t len = (uint8_t)*Len;  //Converts Len as uint32_t to len as uint8_t
  memcpy(buffer, Buf, len);  // copy the data to the buffer
  memset(Buf, '\0', len);   // clear the Buf also
  //Code used to send message back

  /*In the full version, there will be another function outside of this file.
   * The buffer will be processed and a proper message will be sent.
   * Check that other file to see how it gets processed when we eventually implement it.
   */

  uint8_t transmit_message[64] = "TOWST_FIRMV_REEEEEEE_27\r\n"; //Do we have \r\n?
  //CDC_Transmit_FS(transmit_message, sizeof(transmit_message));
  //HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
  //message_Received(buffer, len);
  process_Message(buffer, len);
  return (USBD_OK);
  /* USER CODE END 6 */
}

void process_Message(uint8_t* message, uint16_t Len){
    HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10);
    uint8_t* inputCmd[5];
    uint8_t* inputMessage[8];
    uint8_t outputCmd[5];
    uint8_t outputData[8];

//  strcpy((char*) inputCmd, (const char*)message + COMMAND_CHAR);
//  strcpy((char*) inputMessage, (const char*)message + DATA_CHAR);
    if (strcmp(inputCmd, "FIRMV") == 0){
        memcpy(outputCmd, "FIRMV", COMMAND_LENGTH);
        memcpy(outputData, "01050A00", DATA_LENGTH);
    }
    else{
        memcpy(outputCmd, "REEEE", COMMAND_LENGTH);
        memcpy(outputData, "99999999", DATA_LENGTH);
    }

//  message_Received(message, Len);
    send_Message(outputCmd, outputData);
}

void send_Message(uint8_t* cmd, uint8_t* data){
    uint8_t outputMessage[25] = "TOWST_";  //Size of array is for future uses.
    strncpy((char*) outputMessage + 8, "FIRMV", 5);
    CDC_Transmit_FS(outputMessage, sizeof(outputMessage));
}

    //Expected output: 'TOWST_\0\0FIRMV' based on this post, which per my understanding suggests that strncpy will fill in unfilled memory with null chars. https://aticleworld.com/how-to-use-strncpy-and-how-to-write-your-own-strncpy 


HFOrangefish
  • 103
  • 4
  • To me it is not clear from your question what exactly you are trying to do. Are you running a program on the remote system where you connect to using PuTTY, and the output does not look as expected? The code is incomplete. It does not show how you try to produce output. Please [edit] your question, add more details and show a minimal, reproducible example. With `uint8_t outputMessage[25] = "TOWST_";` only the first 7 elements of the array are guranteed to be initialized. The character at index 7 does not need to be `'\0'`. The `strncpy` will not append a `'\0'` after `"FIRMV"`. – Bodo Feb 02 '23 at 10:48
  • Thank you for letting me know, I have edited the question accordingly. Based on your explanation of strncpy, I now believe my expected output to be inaccurate. However, I still would like to determine what does PuTTY normally do when it needs to send a \0 char. – HFOrangefish Feb 02 '23 at 11:03
  • When _would_ PuTTY need to send a NUL in the first place? (And are you actually talking about sending – it sounds like PuTTY is _receiving_ the NUL from a device, not sending it?) – u1686_grawity Feb 02 '23 at 11:19
  • The question is still not clear to me. Do you use PuTTY to connect to a STM32 board via serial port (RS232)? Are you having problems with typing a `'\0'` character in PuTTY to be sent to the STM32 board? Or is the STM32 board supposed to send a `'\0'` character which is not received / displayed as expected by PuTTY? I would expect `CDC_Transmit_FS` in `send_Message` to send a buffer `TOWST_\0*FIRMV************` where `*` stands for an uninitialized character/byte, i.e. random data that happens to be on the stack at the time the function is called. – Bodo Feb 02 '23 at 11:47
  • `uint8_t* inputCmd[5];` is an array of 5 *pointers to* `char`. If you want to store a string of 5 characters you would need `uint8_t inputCmd[6];` (note the array size 6) as `strcmp(inputCmd, "FIRMV")` requires a C string which must be terminated by `'\0'`. It is unclear what exactly `CDC_Transmit_FS` will do. A `'\0'` character is normally not displayed on the terminal. – Bodo Feb 02 '23 at 11:52
  • Please copy & paste the output you get *as text* and show what you want to get instead. – Bodo Feb 02 '23 at 12:00

0 Answers0