cancel
Showing results for 
Search instead for 
Did you mean: 

problem with CDC_Transmit_FS ,junk data

zohbou
Associate

hello every one , I'm working with nucleo 144 F4, and im trying to send data via virtual com port using CDC_Transmit_FS, but in my Hterm im receiving junk data also i don't why ,,i saw online that there is zero length packet but i don't know how to implement it , im trying to test with this function with sending just 0

void sendvalue(void) {

   CDC_Transmit_FS(0, 8);
}

and call it in the main function and you can see in the screenshot what i receive in my pc  ,  

i will appreciate it if someone can help me thanks 

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

That sends whatever bytes are at memory address 0. Which is probably not zero.

Perhaps you wanted:

  static uint8_t buffer[8] = {0};
  CDC_Transmit_FS(buffer, sizeof(buffer));

 Note that the call is asynchronous, so the data needs to be valid even after the function returns. Making it static or global does this.

 

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

That sends whatever bytes are at memory address 0. Which is probably not zero.

Perhaps you wanted:

  static uint8_t buffer[8] = {0};
  CDC_Transmit_FS(buffer, sizeof(buffer));

 Note that the call is asynchronous, so the data needs to be valid even after the function returns. Making it static or global does this.

 

If you feel a post has answered your question, please click "Accept as Solution".

thanks for your response actually i want to transmit the ones and zeros from pdm microphone and for that i need a counter to count the ones a from the pdm data to plot in a python script but its dosent work as expected ,im giving the clock with timer using pwm and read the data with gpio in the left channel and in the output im expecting that i receive bit from 0 to 8 but in htrem i get also other value like 56 ect..  this is my code if you have a look on it thanks 

#include "main.h"
#include "usb_device.h"
#include "stdbool.h"
#include "usbd_cdc_if.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
void fillByte(bool bitValue);
void sendvalue(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define CHUNK_SIZE 1000
uint8_t binaryData[] = {0x12, 0x34, 0x56, 0x78};
static uint8_t byteToFill = 0;
//static uint8_t i = 0;
//static uint8_t bitIndex = 0;
static uint8_t counter_variable = 0;
static uint8_t onesCount = 0;
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
 
  while (1)
  {
 
  if (counter_variable >= 😎
  {
  sendvalue();
  onesCount=0;
  counter_variable=0;
  byteToFill=0;
 
  }
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}



void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim){
 
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
 
}
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
    if (GPIO_Pin == GPIO_PIN_7) {
        // Handle the falling edge event here
        fillByte(HAL_GPIO_ReadPin(PDM_Input_GPIO_Port,PDM_Input_Pin)==GPIO_PIN_SET);
    }
}
 
void fillByte(bool bitValue) {
    // Set the current bit of the byte according to the bitValue
    if (bitValue) {
 // Set the bit at bitIndex to 1
        onesCount++;
    }
    counter_variable++;
 
}
 
void sendvalue(void) {
    // This function can be left empty since we're sending averages in fillByte
    // Transmit the average values as binary data
 
 
 
CDC_Transmit_FS(onesCount, sizeof( onesCount));
 
 
}