
我正在研究有關接收空字元“\0”的終端輸出上的一些意外行為。我正在發送一個字串,預計將包含 2 \0,但這不會顯示在 PuTTY 上。
根據這個問題這意味著 PuTTY 在正常情況下不會發送 null。然而這個問題已經快有 10 年歷史了,我認為 PuTTY 在此期間已經收到了更新。
我已經調查過PuTTY 使用手冊主要找到了有關 PuTTY 面板的文檔以及大量有關錯誤訊息和連接的文件。第3.3章討論“改變你的字符集配置”,但主要涉及非拉丁字母,它鏈接到第4.10章,這是翻譯面板。
我的問題很簡單,當 PuTTY 需要接收 \0 字元時,它通常會做什麼?難道它沒有收到嗎?它是否收到字元但未顯示在終端中?
如果需要更多信息,請告訴我。
在 STM32 板上,用於向 PuTTY 發送資料的代碼是 C 語言:
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