通过 i2c 从 Arduino 向 STM32 发送数组或 int 时出现问题

问题描述 投票:0回答:1

我必须在 Arduino 和 STM32 之间建立连接(使用 C 和 HAL 编程)。 Arduino 用于从模式,而 STM32 用于主模式。
在这种状态下,我可以轻松地进行这些传输:

  • 从STM32到Arduino的整数;

  • 从STM32到Arduino的整数数组;

  • uint8_t 数组(“字符串”)从 STM32 到 Arduino;

  • 从 Arduino 到 STM32 的字符串;
    通过使用 uart 在终端上显示接收到的内容来检查交换。

这样从STM32到Arduino的传输就按预期工作了。但我在接待方面遇到了一些困难。即使要从 Arduino 接收字符串,我也必须使用 Arduino 上的指令

Wire.write(Message.c_string());
来完成此操作。但如果我尝试接收一个整数,STM32 终端上什么也没有发生。如果我尝试接收整数数组,我会在终端上看到 N 个空字符,然后是光标(N 是数组的大小)。
因此我确信我收到了一些东西......但我无法确定是否:

  1. 我收到错误的数据
  2. 我无法正确显示

有人知道导致这种行为的原因吗?您可以在下面找到我的代码。

    /* On STM32. I'm just sharing the main function to avoid to flood the topic. Please be assured that all the other stuff, as UART or I2C configuration, is done properly*/
    
    int main(void)
    {
        HAL_StatusTypeDef retour_i2c;
    
        uint8_t message_envoye[] = "Hello World !!";
        uint8_t entier_envoye = 32;
        uint8_t tableau_envoye[] = {1, 2, 3, 4};
    
        uint8_t message_recu[10];
        uint8_t entier_recu;
        uint8_t tableau_recu[4]; // My Arduino is sending an array of 4 integers
    
      HAL_Init();
      SystemClock_Config();
      MX_GPIO_Init();
      MX_USART2_UART_Init();
      MX_I2C1_Init();
    
      I2C_Verif_Addresses();
    
      while (1)
      {
          // Transmition part is OK
          retour_i2c = HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(I2C_SLAVE_ADDR << 1), tableau_envoye, sizeof(tableau_envoye), 1000);
          if(retour_i2c != HAL_OK){
              I2C_Error_Handler(&hi2c1);
          }
          else{
              HAL_Delay(1000);
              HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
          }
    
    
          // Reception part is not OK
          // retour_i2c = HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(I2C_SLAVE_ADDR << 1), message_recu, sizeof(message_recu), 1000); // "strings" are working
          retour_i2c = HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(I2C_SLAVE_ADDR << 1), tableau_recu, sizeof(tableau_recu), 1000); // Receiving an array cause invisible characters to be printed
          // retour_i2c = HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(I2C_SLAVE_ADDR << 1), entier_recu, sizeof(entier_recu), 1000);
          if(retour_i2c != HAL_OK){
              I2C_Error_Handler(&hi2c1);
          }
          else{
              HAL_Delay(1000);
              HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
              HAL_UART_Transmit(&huart2, tableau_recu, sizeof(tableau_recu), 10);
          }
      }
    }
c arduino embedded stm32 i2c
1个回答
0
投票

通过 UART 发送二进制数据不会在终端程序中显示可读的结果。

如果这些值对应于可打印字符的编码,您将看到一些内容,但看不到数字的值,例如二进制值

0x31 = 49
将打印为
1

您可以使用

sprintf
将数字转换为字符串表示形式或编写自己的转换函数。或者使用可以将二进制数据显示为十六进制转储的程序来检查值。

© www.soinside.com 2019 - 2024. All rights reserved.