Maker Pro
Maker Pro

STM32 & NEXTION HMI SCREEN - SENDING AS CHAR READING AS STRING

turgut

Nov 8, 2022
26
Joined
Nov 8, 2022
Messages
26
Hello guys,
Im working on a project. I did a keyboard on my nextion screen. When Im pressing a character on the keyboard, nextion writing them on a text bar.
Example:
User pressing ----> 'A' , 'B' , 'C', ' ', 'D' , 'E' , 'F' (Like sending name, surname)
Text Bar Looking -> ABC DEF (Like seeing name, surname)

So how can i save them. Like that kind of table in txt format
1- name1 surname1
2- name2 surname2
3- name3 surname3

Im not good at coding. Actually Im learning to coding. I have an idea but im open for opinions.
My idea is:
I will try to write them eeprom. But how should i processing information.

Thank you all..

buttons: A-b0 B-b1 C-b2 ... F-b5
text bar: t0
 

kellys_eye

Jun 25, 2010
6,039
Joined
Jun 25, 2010
Messages
6,039
Like that kind of table in txt format
You answer your own question - specifically you use a 'table' where the required information is stored in a fixed location in memory and you then program a pointer/counter to the start of the string and 'count out' the data to the specified port (display).
 

turgut

Nov 8, 2022
26
Joined
Nov 8, 2022
Messages
26
Code is here but my brain frost. What should I change now? I tried all ways reddit, github, youtube, chatgpt..



C:
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
/* 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 ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;

/* USER CODE BEGIN PV */
uint8_t RxBuffer[20];
 uint8_t RxDataX[20];
 uint8_t TxBuffer[20];
 uint8_t Size=0;
 uint8_t SizeBuffer=0;
 uint8_t EndCheck=0;
 uint8_t EndCheckNumber=0;
 uint8_t RxData[20];
 uint8_t SizeShont=0;
 uint8_t CmdEnd[3]={255,255,255};
 uint8_t buf[50];
 uint8_t bufx[50];

 //------------------------------------------------------------CHATGPT------------------------------------------------------------
 uint8_t receivedData[50];


 /////
 uint8_t ON1[7]={101,0,2,0,255,255,255};
 uint8_t OFF1[7]={101,0,3,0,255,255,255};
 //uint8_t TOGGLE1[7]={101,0,4,0,255,255,255};

 //uint8_t NEXT[7]={101,0,4,0,255,255,255};
 //uint8_t BACK[7]={101,1,3,0,255,255,255};

 uint8_t ON2[7]={101,1,2,0,255,255,255};
 uint8_t OFF2[7]={101,1,4,0,255,255,255};

 uint8_t ON3[7]={101,2,2,0,255,255,255};
 uint8_t OFF3[7]={101,2,3,0,255,255,255};

 uint8_t A[7]={101,1,6,0,255,255,255};
 uint8_t N[7]={101,1,9,0,255,255,255};
 uint8_t I[7]={101,1,8,0,255,255,255};
 uint8_t L[7]={101,1,10,0,255,255,255};

 //------------------------------------------------------------CHATGPT------------------------------------------------------------
#define MAX_NAME_LENGTH 50

char name[MAX_NAME_LENGTH]; // İsim verisini saklamak için bir karakter dizisi

 ////
   int frq=1000;
   int counter=0;
   int pulsetime=4;
   int power=8;
   int pulse=0;
   int kadame=0;
   int Sistem=0;
   int len;
   int a=0;
   int Sisem_cnt_enable;
   uint8_t  flag=0;
   uint8_t  kademe=0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

        RxData[Size]=RxBuffer[0];
        //a=500;
        SizeShont=0;
        if (RxData[Size]==255) EndCheck++;
        else EndCheck=0;
        if(EndCheck==3)
            {
                                                                                    //memset(RxDataX,'\0',strlen(RxDataX));
                memset(RxDataX, '\0',(SizeBuffer)*sizeof(char));
                            for (int i=0;i<=(Size);i++)
                            {
                                    RxDataX=RxData;
                            }


                                                                                        //memset(RxData,'\0',Size+1);
            memset(RxData, '\0',(Size+1)*sizeof(char));


        //    sprintf(RxDataX,"%s",RxData);
        //    memset(RxDataX,'\0',10);
        //    sprintf(RxDataX,"%s",RxData);
        //    memset(RxData,'\0',SizeBuffer+1*sizeof(char));

        SizeBuffer=Size;
        Size=0;
        SizeShont=1;
        EndCheck=0;
            }
        if(SizeShont==0)Size=Size+1;
        if(Size>20)Size=0;

        HAL_UART_Receive_IT(&huart2,RxBuffer,1);
}
HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{

}

void SendNumber(char *id,uint16_t value)
{ //char buf[30];
     len= sprintf(buf,"%s.val=%u",id,value);
     buf[len]=255;
     buf[len+1]=255;
     buf[len+2]=255;
     HAL_UART_Transmit_IT(&huart2, buf, len+2);

    }
void SendData(char *data)
{ //char buf[50];
     len= sprintf(buf,"%s",data);
     HAL_UART_Transmit_IT(&huart2,buf, len);
}
/*void Nextion_SendString(char *ID,char *string)
{
    //char buf[50];
    len=sprintf(buf,"%s.txt=\"%s\"",ID,string);
    HAL_UART_Transmit_IT(&huart2,buf, len);
}*/
void ReadNextion(void)
{

int result= strcmp(RxDataX,bufx);
    if(result==0  )
    {
        for (int i=0;i<=(SizeBuffer);i++)
        {bufx=0;}
    }

    else
     {    for (int i=0;i<=(SizeBuffer);i++)
        {
         bufx=RxDataX;
         RxDataX=0;
}}}

//------------------------------------------------------------CHATGPT------------------------------------------------------------
/*void SaveNameToMemory(const char* name)
{
    // İsim verisini hafızada sakla
    strncpy(name, name, MAX_NAME_LENGTH  - 1);
    name[MAX_NAME_LENGTH - 1] = '\0';
}*/
void SaveNameToMemory(const char* inputName)
{
    char tempName[MAX_NAME_LENGTH];
    strncpy(tempName, inputName, MAX_NAME_LENGTH - 1);
    tempName[MAX_NAME_LENGTH - 1] = '\0';

    // Hafızada saklanacak olan veriyi tempName'den name dizisine kopyala
    strncpy(name, tempName, MAX_NAME_LENGTH);
}
void DisplayDataOnNextion(const char* data)
{
    char buffer[50];
    snprintf(buffer, sizeof(buffer), "t1.txt=\"%s\"", data);

    // Nextion ekranına komut gönder
    HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
}

/* 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_DMA_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_StatusTypeDef HAL_UART_AbortReceive(huart2);
    HAL_Delay(100);
    HAL_UART_Receive_IT(&huart2,RxBuffer,1);
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);

  //HAL_UART_Receive_IT(&huart2,Rx_Data,5);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */


    /* USER CODE BEGIN 3 */
      ReadNextion();
                 //PAGE-1


                   if(strcmp(ON1,bufx)==0)
                 {
                    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,1);
                    HAL_Delay(100);
                  }

                   if(strcmp(OFF1,bufx)==0)
                 {
                    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                    HAL_Delay(100);
                  }

               if(strcmp(ON2,bufx)==0)
                   {
                    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,1);
                    HAL_Delay(100);
                    }

              if(strcmp(OFF2,bufx)==0)
                   {
                   HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                   HAL_Delay(100);
                    }

              if(strcmp(ON3,bufx)==0)
                  {
                  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,1);
                  HAL_Delay(100);
                  }

              if(strcmp(OFF3,bufx)==0)
              {
                  HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                  HAL_Delay(100);
              }

              //--------------------------------KEYBOARD--------------------------------

              if(strcmp(A,bufx)==0)
                       {
                           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                           SendData('A');
                           HAL_Delay(100);
                       }
              if(strcmp(N,bufx)==0)
                       {
                           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                           SendData('N');
                           HAL_Delay(100);
                       }
              if(strcmp(I,bufx)==0)
                       {
                           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                           SendData('I');
                           HAL_Delay(100);
                       }
              if(strcmp(L,bufx)==0)
                       {
                           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15,0);
                           SendData('L');
                           HAL_Delay(100);
                       }

     //------------------------------------------------------------CHATGPT------------------------------------------------------------
              /*if (HAL_UART_Receive_IT(&huart2, (uint8_t*)receivedData, sizeof(receivedData)) == HAL_OK)
                      {
                          SaveNameToMemory(receivedData);
                          DisplayDataOnNextion(name);
                          // Kaydedilen ismi seri terminale göndermek için aşağıdaki satırı kullanabilirsiniz
                          // HAL_UART_Transmit(&huart2, (uint8_t*)name, strlen(name), HAL_MAX_DELAY);
                      }*/
             // Nextion ekranından gelen veriyi oku
                 char receivedChar;
                 HAL_UART_Receive(&huart2, (uint8_t*)&receivedChar, 1, HAL_MAX_DELAY);

                // Okunan veriyi t1 text nesnesine yazdır
                    char buffer[10];
                    sprintf(buffer, "t1.txt=\"%c\"", receivedChar);
                    HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);


  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART2 Initialization Function
  * [USER=88011]@Param[/USER] None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{

  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Stream5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
  /* DMA1_Stream6_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * [USER=88011]@Param[/USER] None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

  /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * [USER=88011]@Param[/USER]  file: pointer to the source file name
  * [USER=88011]@Param[/USER]  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
[mod edit: placed the code in a code box for better handling]
 
Last edited by a moderator:

eGuidezhan

Jun 28, 2023
1
Joined
Jun 28, 2023
Messages
1
"Hi there! It seems like you're working on a project involving the STM32 microcontroller and the Nextion HMI screen. To send data as a character and read it as a string, you can use the appropriate communication protocol and data handling techniques.

For example, if you are using a UART (Universal Asynchronous Receiver-Transmitter) communication between the STM32 and the Nextion HMI screen, you can send characters using the UART's UART_Transmit() function. To read the received data as a string, you can create a buffer and continuously append the received characters until you encounter a termination character (such as '\0').

Here's a basic code snippet to illustrate the concept:
Code:
// Send data as a character
char data = 'A';  // Example character to send
UART_Transmit(&huart1, &data, 1);

// Read data as a string
char receivedData[20];  // Buffer to store received data
uint8_t index = 0;  // Index for buffer

while (1) {
  if (UART_ReceivedDataAvailable(&huart1)) {
    char receivedChar = UART_Receive(&huart1);  // Receive character
    if (receivedChar != '\0') {
      receivedData[index] = receivedChar;  // Append character to buffer
      index++;
    } else {
      receivedData[index] = '\0';  // Terminate the string
      break;
    }
  }
}

// Process the received string as needed
// ... (your code here)

Please note that the specific implementation may vary depending on the STM32 microcontroller and the Nextion HMI screen models you are using. Make sure to refer to their respective datasheets and communication protocols for more detailed instructions.
 
Top