Skip to main content
MSchmutz
Associate II
January 10, 2022
Question

hey i want to read a temperature sensor and a humidity sensor i want to send the values via cdc to my pc and then read them in c# windows forms my question is now how can i read two adc at the same time so i have separate data ? here you have my stm

  • January 10, 2022
  • 3 replies
  • 1475 views

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  HAL_ADC_Start(&hadc2);

  HAL_ADC_Start(&hadc1);

if (HAL_ADC_PollForConversion(&hadc2, 10) == HAL_OK)

{

 adcValTemp = HAL_ADC_GetValue(&hadc2);

 // Temperature Calculation

 adcTemp = (3300 * adcValTemp) / 4096;

 Temp = (adcTemp - 2636) / -13.6;

   //itoa(adcValTemp, string_adcValTemp, 10);

   //CDC_Transmit_FS(string_adcValTemp, strlen((const char*)string_adcValTemp));

 CDC_Transmit_FS(adcValTemp, sizeof(adcValTemp));

}

  HAL_Delay(500);

if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK)

{

 adcVal = HAL_ADC_GetValue(&hadc1);

   itoa(adcVal, string_adcVal, 10);

   CDC_Transmit_FS(string_adcVal, strlen((const char*)string_adcVal));

}

  HAL_Delay(500);

 }

This topic has been closed for replies.

3 replies

MSchmutz
MSchmutzAuthor
Associate II
January 10, 2022

here i also go the c# code

  public partial class Form1 : Form

  {

    string inbuffer;

    public Form1()

    {

      InitializeComponent();

      serialPort1.Open();

    }

    private void DisplayText(object sender, EventArgs e)

    {

      label1.Text = Convert.ToString(inbuffer);

    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

    {

      inbuffer = serialPort1.ReadExisting();

      this.Invoke(new EventHandler(DisplayText));

    }

  }

}

TDK
January 10, 2022

Read both values and send them at the same time within the same USB packet. In your reception program, you will need to unpack them again in the proper order.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MSchmutz
MSchmutzAuthor
Associate II
January 10, 2022

could you do me a little example ? i dont get it thanks :D

MM..1
Chief III
January 10, 2022

In your code you mix value types ... binary data send ... ascii data send

adcValTemp = HAL_ADC_GetValue(&hadc2);
...
CDC_Transmit_FS(adcValTemp, sizeof(adcValTemp));

Too you say how can i read two adc at the same time,

but used code differ. For same moment measure you need triggered ADC not polled, and wait read result in IT or DMA mode....