Go Down

Topic: char-array (simple string) (Read 1 time) previous topic - next topic

_frank_

Hi,
i build a char-array from serial-data...
now i want to disassemble this string.

Code: [Select]
    switch(int(input[0]))
    {
      case 'D': //display DxyyText (x=row,yy=col)
      {
          int row=int(input[1]);
          int col=int(input[2])*10+int(input[3]);
          char *text=&input[4];
          ShowTextOnDisplay(row,col,text);
     }
    }


it compiles, but is this the right way (moving the pointer to shorten the string)? how do i get the length of such an array?

documentation is a bit small about this

regards Frank

robtillaart

Better post the whole code to see how the input is filled etc

it looks quite right but you must be sure the array ends with a '\0' char, then you can use strlen().
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

_frank_

generation-code:
Code: [Select]
  char input[80];
  int incount = 0;
  if ( Serial.available())
  {
    // read in a string byte by byte
    while (Serial.available())
    {
      int current=Serial.read();

      if ((current != 13) && (current != 10) && (incount<79))
        input[incount++] = current;
      else break;
      delay(100);//give time to next byte passed to serial line
    }
    input[incount] = '\0';  // puts an end on the string


do i have to include something to use strlen?

robtillaart

If the first Serial.available() fails (the one in the if ) then the array will not get filled at all.

(wait until data comes)
Code: [Select]

char input[80];
  int incount = 0;

  while (serial.available() <1 ); // forced wait until 1 char  Adapt number to your need if needed

  while (Serial.available() )
  {
      int current = Serial.read();
      if ((current != 13) && (current != 10) && (incount<79))
      {
        input[incount++] = current;
      }
      else break;
      delay(100);   // depends on how the other side sends ...
  }
input[incount] = '\0';  // puts an end on the string


This forced wait does not garantee that the array is filled correctly but it captures at least 1 char (you may adapt that nr)



as incount does reflect the length of the array allready => strlen() is not needed
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

_frank_

thanks for your suggestion...

my ShowTextOnDisplay-function...i want to change some chars to other bytecode...having problems with unicode (2 bytes). how can i solve this?

Code: [Select]
void ShowTextOnDisplay(int row,int col, char *text)
{
  lcd.setCursor(col,row);//second line, first col
  //now replacing wrong chars
 
  for (int i=0;i<strlen(text);i++)
  {
    switch(int(text[i]))
    {
      case '{': text[i]=0xFD; break;
      case '}': text[i]=0xFF; break;
      case '$': text[i]=0xA2; break;
      ....

PaulS

Code: [Select]
    switch(int(text[i]))
The switch statement requires a constant value as the argument. The text value is constant. Why are you casting it to an int?

Quote
having problems with unicode (2 bytes). how can i solve this?

That's pretty easy. Stop having problems.

On the other hand, if you explained what you are trying to accomplish, what is working, and what is not working, the answers you get would move further along the spectrum from smart ass to smart.
The art of getting good answers lies in asking good questions.

_frank_

the cast is practice from other languages ;), if you say its not needed, i can remove it :)

i want to control the µC-board from serial-line. sending commands like the display-command (beginning with D) should do something on the board. In this case show a Text on a specific position on the display. so i have to parse the string to get position (1 byte for line and 2 byte for column), after that the Text begins (modify pointer to avoid memcopy).
Some chars must changed because of different charsets of PC-OS/µC and Display...this is should be done in the function "ShowTextOnDisplay". The switch/case-structure only supports byte and some chars (e.g. german special chars) are encoded in unicode (2 byte)...maybe i have to use string-object to replace them (if it is to complicated to do with char-array).
What is the best way?

PaulS

Quote
what you are trying to accomplish, what is working, and what is not working,

Quote
Some chars must changed because of different charsets of PC-OS/µC and Display...this is should be done in the function "ShowTextOnDisplay". The switch/case-structure only supports byte and some chars (e.g. german special chars) are encoded in unicode (2 byte).

So far, so good. That is what you are trying to accomplish.

Now, what is working and what is not working?

The art of getting good answers lies in asking good questions.

_frank_


Quote
Some chars must changed because of different charsets of PC-OS/µC and Display...this is should be done in the function "ShowTextOnDisplay". The switch/case-structure only supports byte and some chars (e.g. german special chars) are encoded in unicode (2 byte).

Now, what is working and what is not working?


do i have to use the string object or can this be done with char-array (replace unicode-char in special byte [display-charset])?

PaulS

Quote
do i have to use the string object

No.

Quote
can this be done with char-array (replace unicode-char in special byte [display-charset])?

Yes.
The art of getting good answers lies in asking good questions.

_frank_

ok, how can i do this?

i assume (unicode=2 bytes) i cannot compare both bytes in the switch/case => check if first byte is a unicode entry char (imho 0xf*) and then check the next byte...coming to the main-problem...i cannot simple replace 2 bytes by 1 (have to move the string after unicode-char 1 byte to the beginning)

is it simpler to do it with string class?

regards Frank

PaulS

Quote
i assume (unicode=2 bytes) i cannot compare both bytes in the switch/case => check if first byte is a unicode entry char (imho 0xf*) and then check the next byte.

In the switch, no. But in the case for 0xF0 to 0xFF, you can deal with the nth character AND the (n+1)st character. Just move everything else up.
The art of getting good answers lies in asking good questions.

Go Up