5 years, 11 months ago.

String two uint8_t as an array

Hi, This is my code

#include "mbed.h"
#include "math.h"
#include <string.h>

Serial pc(USBTX, USBRX);
Serial receive(PTC17, PTC16);
int main()
{
   pc.baud(115200);
   receive.baud(115200);
   printf("start"); 
   char command[80];
   pc.printf("The commands received are\n");
   while(1)
   {
       if (receive.readable())
        {
            receive.gets(command, 70);
            uint8_t str[100];
            uint8_t i=command[64], j=command[65];     
       }
    }

Here i and j have two values namely 2 and 7. I want to send this two values together using UART port. Like this uint8_t data[] = "27" {The receiver end will only recognize in this format.}. How would I do that? When I tried concatenating it in an array it showed me (const char *) error. Can anyone help me with this? Many thanks. Regards Niranjan

1 Answer

5 years, 11 months ago.

Hello Niranjan,

You can try

#include "mbed.h"
#include "math.h"
#include <string.h>

Serial pc(USBTX, USBRX);
Serial receive(PTC17, PTC16);
int main()
{
   pc.baud(115200);
   receive.baud(115200);
   pc.printf("start\r\n"); 
   char command[80];
   pc.printf("The commands received are\r\n");
   while(1)
   {
       if (receive.readable())
        {
            receive.gets(command, 70);
            pc.printf("%c%c", command[64], command[65]); // 'c' is format specifier for a character
            //pc.printf("%c%c\r\n",  command[64], command[65]); // appends 'carriage return' and 'new line' for serial terminal
       }
    }
}

NOTE: Global printf prints to the virtual serial port at bitrate 9600 bps. If you create an additional Serial object for printing to the virtual serial port as well (like pc(USBTX, USBRX);) but with different bitrate and adapt the bitrate of your PC serial terminal then you won't see the prints done with global printf (like printf("start");) anymore, unless you adapt the bitrate of the global printf too.