cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32 to Python

MFara.4
Associate III

I would be happy if you share with me some example codes about transmitting data from stm32 to python via serial!

1 ACCEPTED SOLUTION

Accepted Solutions
RomainR.
ST Employee

Sorry.

I made a typo error in line below (/r/n vs \r\n):

adc_len = sprintf(adc_buf, "ADC Value=%d/r/n", readValue);

Change it as follow:

adc_len = sprintf(adc_buf, "ADC Value=%d\r\n", readValue);

Otherwise this code works very well (see screenshot below)

Check your hardware connection.

Check your terminal settings baudrate, word length, parity and stop bit must be aligned between USART Initialization and your terminal.

0693W00000WKunlQAD.png 

Best regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

14 REPLIES 14
RomainR.
ST Employee

Hello MFara.4 (Community Member)

You are looking for code examples for transferring from STM32 to Python by serial, do you mean by a UART device of the STM32 or other like USB, SPI or I2C ? 

Is the code you are looking for is in C/C++ language? 

Have you looked in the HAL library of the device you are using for examples related to USART transfer?

Can you be more precise in your question?

Best regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

MFara.4
Associate III

Thanks for the reply!

Yes I need examples which are related to USAERT transfer.

MFara.4
Associate III

one more question! I use this code to get the data ADC and transmit it but I can't see my data on Tera Term, I see my data in live Expression but not in Tera Term so most probably problem comes from Transmit part!! I would be happy if you take a look at the code that which part might be wrong.(STMG01291)

ADC1 - IN9 (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Enable USART1 asynchronous

Parameter Settings --> Basic Parameters --> Baud rate 9600

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_ADC1_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint16_t readValue;

char string[10];

/* 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_ADC1_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_ADC_Start(&hadc1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 HAL_ADC_PollForConversion(&hadc1,1000);

 readValue = HAL_ADC_GetValue(&hadc1);

 HAL_UART_Transmit(&huart1, (uint8_t *)&readValue, sizeof(readValue), 100);

Thanks for the reply!

Yes I need examples which are related to USAERT transfer.

one more question! I use this code to get the data ADC and transmit it but I can't see my data on Tera Term, I see my data in live Expression but not in Tera Term so most probably problem comes from Transmit part!! I would be happy if you take a look at the code that which part might be wrong.(STMG01291)

ADC1 - IN9 (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Enable USART1 asynchronous

Parameter Settings --> Basic Parameters --> Baud rate 9600

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_ADC1_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint16_t readValue;

char string[10];

/* 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_ADC1_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_ADC_Start(&hadc1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 HAL_ADC_PollForConversion(&hadc1,1000);

 readValue = HAL_ADC_GetValue(&hadc1);

 HAL_UART_Transmit(&huart1, (uint8_t *)&readValue, sizeof(readValue), 100);

RomainR.
ST Employee

Hi MFara.4 (Community Member)

You must convert your variable readValue in Ascii Char to display it in any terminal.

Before transmit it with HAL_UART_Transmit.

As an example below:

Best regards

#include <stdio.h> // for sprintf usage
static char adc_buf[16] = {0};
static uint32_t adc_len = 0;
 
/* Convert in array of ASCII char and compute array lenght */
adc_len = sprintf(adc_buf, "ADC Value=%d/r/n",  readValue);
 
/* Transmit Array */
if (HAL_UART_Transmit(&huart1, (uint8_t *)adc_buf, adc_len, HAL_MAX_DELAY) != HAL_OK)
{
  Error_Handler();
}

In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks again for the reply!

I changed the codes but again I am unable to see my data on it! I am sure a small thing is missing. could you look agin the revised code and give me idea.

#include <stdio.h> // for sprintf usage

/* 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 ---------------------------------------------------------*/

ADC_HandleTypeDef hadc1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_ADC1_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint16_t readValue;

static char adc_buf[16] = {0};

static uint32_t adc_len = 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_ADC1_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_ADC_Start(&hadc1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

 HAL_ADC_PollForConversion(&hadc1,1000);

 readValue = HAL_ADC_GetValue(&hadc1);

 adc_len = sprintf(adc_buf, "ADC Value=%d/r/n", readValue);

 if (HAL_UART_Transmit(&huart1, (uint8_t *)adc_buf, adc_len,HAL_MAX_DELAY) != HAL_OK);

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

RomainR.
ST Employee

Sorry.

I made a typo error in line below (/r/n vs \r\n):

adc_len = sprintf(adc_buf, "ADC Value=%d/r/n", readValue);

Change it as follow:

adc_len = sprintf(adc_buf, "ADC Value=%d\r\n", readValue);

Otherwise this code works very well (see screenshot below)

Check your hardware connection.

Check your terminal settings baudrate, word length, parity and stop bit must be aligned between USART Initialization and your terminal.

0693W00000WKunlQAD.png 

Best regards,

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

On the Python side, something along the lines of

import serial
 
s = serial.Serial('COM23', baudrate=115200)  # Open serial port
 
    while (s.in_waiting != 0):
        data = s.readline().decode('ascii', errors='replace')
 
# process / convert 'data' into variables

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..