C ++ doubts

15 Dec 2010

hello!

I´m trying to connect my mbed microcontrollerto the pc, using the serial interface, and to another device, using the ethernet interface.

I need to send to this device commands that are composed by 8 bytes (for example "00 02  08 ff 63 47 a5 e3".

My problem is that I´m using the "Serial" library to ask for the command, and I need to convert the char that the user put on the keyboard into a string like the example.

I need some help because I have not many experience working in c++.

Thank you so much!

16 Dec 2010

If you would type that like the eth MAC for windows, you could
write(read) it without spaces like ..
     char your_string[24]; 
     printf("Enter string (without spaces) :\n");
     scanf("%23s", your string);

or if you want the string with the spaces use scanf like this :
      scanf("(”%[^\n]s”, your string);
witch waits for Enter (new line) to finish the reading of the string

if you want to see what you're typing in try this way:
int i;
for(i=0; i<23; i++)
  { your_string[i]= getc();
    putc(your_string[i];
  }

18 Jan 2011

Thank you so much!

18 Jan 2011

But now, I have another problem:

My string has different length each time so I would like to define it like char my_string[256], for example, but when I want to do this:

for (i=0; i<length(my_string); i++)

{

...

}

I can´t use "length".

How can I solve it?

Thank you so much!

18 Jan 2011 . Edited: 18 Jan 2011

I think "string.h" has  strlen(my_string);   for returning the length of a string

or use ... for (i=0; my_string[i] != NULL; i++) to check for last char in string

18 Jan 2011

And can I do the same but with the condition  i different of carriage return?

If I put "NULL" it inderstands that there is no characters in my_string and the for doesn´t runn.

Because I write i != 'CR' and it doesn´t works...

18 Jan 2011

And can I do the same but with the condition  i different of carriage return?

If I put "NULL" it inderstands that there is no characters in my_string and the for doesn´t runn.

Because I write i != 'CR' and it doesn´t works...

18 Jan 2011

Ok, I'm stupid, forget this "problem".

But I have another one:

If I define my_string like a char[256] (due to it can have different lengths), how can I convert it later in a char with the exact length?

I mean, for example: I have char my_string[256]; but once it only have [18], how can I do put this 18 bytes in another char??

Thank you!

18 Jan 2011

You can define a new string without dim like char new_string[ ] and initailize it with your "exact length" string.

18 Jan 2011

If I do that ( char new_string[ ]) I recieve an error "Incomplete Type is not allowed (E70))

18 Jan 2011

Why do you need a copy of 18 chars long? Why can't you use the old buffer? What is the big task you're trying to accomplish?

18 Jan 2011

What I have to do is an interface that allows an  user select an option and, for example, if he select option 1, I have to create a command (char of diferent lengths deppending of the option, sometimes 18 bytes, sometimes 22, etc)

I do this connecting the microcontroller with the USB/COM interface to the PC.

Then, I have to convert the command in something I can send using the ETH interface to another device connected to the microcontroller.

I changed the program and now I use a switch structure in oder to create different strings for each case.

I don´t know if it´s the eaisiest form to do that, but I´m trying it...

The second part, convert the command to something that I can send using the ethernet library will bring more problems, I´m sure :S

18 Jan 2011

But I have a problen using switch structure, "case" needs a constant and I´ve got a char because I have to use getc from the serial library...

18 Jan 2011

I use an "if" instead of "case".

If a condition is true , I create: char comando[18]={0x02, 0x00, 0x09, 0xff, 0xb0, 0x01, 0x00, 0x18, 0x43};.

How can I show this in the Tera Term if I´m connecting the microcontroller with the serial interface?

19 Jan 2011

You have to convert all bytes (char type) in ASCII form in order to printf them.Example : first "char" 0x02 must be converted in string "0x02".

If all your bytes have a meaning as HEX values you can also printf them as hex integer.

19 Jan 2011

I have problems to do the cast:

char command[18]={0x02, 0x00, 0x09, 0xff, 0xb0, 0x01, 0x00, 0x18, 0x43};

int command_integer = static_cast<int>(command);

It doesn´t works

19 Jan 2011

Ok, I was totally blocked, it´s solved now (I feel very stupid )

15 Feb 2011

how to do a program that convert serial to ethernet? please.....