0

I would like to send the value of the timestamp of a simulation as int. To be more clear, if the mote output is:

00:39.841   ID:6    unicast message ready to be sent

I want to be able to put in my message the value 00:39.841 in milliseconds. How can I do?

Thank you.

rick87
  • 51
  • 9
  • I've tried with RTIMER_NOW(), but it returns a value of rtimer_clock_t type and I need int. My goal is to be able to distinguish at the Receiver which message has been prepared later than the other – rick87 Aug 16 '17 at 13:11

1 Answers1

0

I would recommend you to use function strtok() to find string you need i.e. 00 or 39.

and strtol() to conversion from string to long integer. Anyway, stay away from atoi().

Why shouldn't one use atoi().

Here is example, convertion from minutes/seconds to miliseconds is up to you.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char input[] = "00:39.841   ID:6    unicast message ready to be sent";
    char * ptr;


    long minutes  = 0,
         seconds  = 0,
         milisecs = 0;

    ptr     = strtok(input, ":");
    if (ptr == NULL) { fprintf(stderr, "Wrong input minutes."); return 1; }
    minutes = strtol(ptr, (char **)NULL, 10);

    ptr     = strtok(&input[3], ".");
    if (ptr == NULL) { fprintf(stderr, "Wrong input seconds."); return 1; }
    seconds = strtol(ptr, (char **)NULL, 10);

    ptr     = strtok(&input[6], " ");
    if (ptr == NULL) { fprintf(stderr, "Wrong input milisecs."); return 1; }
    milisecs = strtol(ptr, (char **)NULL, 10);

    printf("%ld - %ld - %ld\n", minutes, seconds, milisecs);

    return 0;
}
kocica
  • 6,412
  • 2
  • 14
  • 35
  • OK, but I don't need this, the mote output comes from Cooja and I want to be able to access the time – rick87 Aug 16 '17 at 13:38
  • So get output from Cooja, store it and then convert it like i did ? `char * input = CoojaOutput` Thats exactly what you asked for. – kocica Aug 16 '17 at 13:48
  • Yes, but in this way I have to access the text script, and I want to avoid, I would like to use functions like RTIMER_NOW() ad so on. Thank you – rick87 Aug 16 '17 at 14:23
  • maybe a function like clock_second() but getting millisec could be better.. – rick87 Aug 16 '17 at 14:33
  • Then multiply seconds * 1000 and youve got miliseconds. – kocica Aug 16 '17 at 14:44
  • So maybe its time to create [MCVE](https://stackoverflow.com/help/mcve) since your question doesnt contain much informations. – kocica Aug 16 '17 at 15:09