title: Mikroe RS485 Click with Azure Sphere - Issue sending and receiving byte arrays using RS485
body: <p>I am using Avnet Azure sphere board with RS485 click board connected. In RS485 Click the GPIO PWM is made high to send RS485 signal from Click board and GPIO PWM is made low to receive RS485 signal.
  The problem here is for sending the request it takes at least 2 seconds but the connected sensor returns the response within milliseconds before we switch back the GPIO PWM to low for receiving. Hence my UART return event is not firing. Any help would be appreciated.</p>
  <p>My send UART message function</p>
  <pre><code>static void SendUartMessage(int uartFd, const char* dataToSend, size_t totalBytesToSend, bool ignoreRx)
  {
      size_t totalBytesSent = 0;
      //size_t totalBytesToSend = strlen(dataToSend);
      int sendIterations = 0;
      close(r1PinFd);
      r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_High);
      while (totalBytesSent &lt; totalBytesToSend) {
          sendIterations++;
  
          // Send as much of the remaining data as possible
          size_t bytesLeftToSend = totalBytesToSend - totalBytesSent;
          const char* remainingMessageToSend = dataToSend + totalBytesSent;
          ssize_t bytesSent = write(uartFd, remainingMessageToSend, bytesLeftToSend);
          if (bytesSent == -1) {
              Log_Debug(&quot;ERROR: Could not write to UART: %s (%d).\n&quot;, strerror(errno), errno);
              exitCode = ExitCode_SendMessage_Write;
              return;
          }
  
          totalBytesSent += (size_t)bytesSent;
      }
      int c, d;
  
      delay_milliseconds(2000);
      close(r1PinFd);
      r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_Low);
  
      Log_Debug(&quot;Sent %zu bytes over UART in %d calls.\n&quot;, totalBytesSent, sendIterations);
  }
  </code></pre>
  <p>My RS485 UART return event</p>
  <pre><code>static void UartEventHandler(EventLoop* el, int fd, EventLoop_IoEvents events, void* context)
  {
      const size_t receiveBufferSize = 256;
      uint8_t receiveBuffer[receiveBufferSize + 1]; // allow extra byte for string termination
      ssize_t bytesRead;
  
      // Read incoming UART data. It is expected behavior that messages may be received in multiple
      // partial chunks.
      bytesRead = read(uartFd, receiveBuffer, receiveBufferSize);
      if (bytesRead == -1) {
          Log_Debug(&quot;ERROR: Could not read UART: %s (%d).\n&quot;, strerror(errno), errno);
          exitCode = ExitCode_UartEvent_Read;
          return;
      }
  
      if (bytesRead &gt; 0) {
          receiveBuffer[bytesRead] = 0;
          Log_Debug(&quot;UART received %d bytes: '%s'.\n&quot;, bytesRead);
          char  data_hex_str[sizeof(receiveBuffer) / sizeof(receiveBuffer[0])];
          get_hex(receiveBuffer, sizeof(receiveBuffer) / sizeof(receiveBuffer[0]), data_hex_str, sizeof(receiveBuffer) / sizeof(receiveBuffer[0]), 16);
          strcat(returnDataUart, data_hex_str);
          Log_Debug(&quot;\s&quot;, returnDataUart);
      }
  
      char* pjsonBuffer = (char*)malloc(JSON_BUFFER_SIZE);
      if (pjsonBuffer == NULL) {
          Log_Debug(&quot;ERROR: not enough memory to send telemetry&quot;);
      }
  
      snprintf(pjsonBuffer, JSON_BUFFER_SIZE,
          &quot;{\&quot;gX\&quot;:%.2lf, \&quot;gY\&quot;:%.2lf, \&quot;gZ\&quot;:%.2lf, \&quot;aX\&quot;: %.2f, \&quot;aY\&quot;: &quot;
          &quot;%.2f, \&quot;aZ\&quot;: %.2f, \&quot;pressure\&quot;: %.2f, \&quot;light_intensity\&quot;: %.2f, &quot;
          &quot;\&quot;altitude\&quot;: %.2f, \&quot;temp\&quot;: %.2f,  \&quot;rssi\&quot;: %d, \&quot;RS485\&quot;: %s}&quot;,
          acceleration_g.x, acceleration_g.y, acceleration_g.z, angular_rate_dps.x,
          angular_rate_dps.y, angular_rate_dps.z, pressure_kPa, light_sensor, altitude,
          lsm6dso_temperature, network_data.rssi, returnDataUart);
  
      Log_Debug(&quot;\n[Info] Sending telemetry: %s\n&quot;, pjsonBuffer);
      SendTelemetry(pjsonBuffer, true);
      free(pjsonBuffer);
  }
  </code></pre>