Int to ASCII conversion

Hello,

I'm receiving from RS232 (via MAX232) datas; the word consist of 13 bytes and it is> =xxxx.xx(kg) + CR, where 'x' is number and '.' is decimal point.

I can succesfully read the datas via Serial monitor (Serial.write (incomingByte));

Also I can display it on LCD using LCD.write (incomingByte);

I want to pick up just the numbers from message. All incoming bytes are the type int. (Serial.write or LCD.write just make ASCII representation of that ints).

So my idea was to convert the int to ASCII, make a String and then cut the String. And then maybe to convert it to float.

So I used the itoa() in the way:

int incomingByte = 0;  
char buffer[12];
String ascii_num;

void loop() {

  if (Serial.available()) {
    incomingByte = Serial.read();
    itoa (incomingByte, buffer, 10);
     // so if I understand it correctly - now in buffer should be a character (like '0' for 48)
     // optionaly I can combine those buffers into string 
     // ascii_num += buffer;
      lcd.print (buffer);
      
  } // end if

The problem is, that lcd.print (buffer) (or Serial.print (buffer)) is not showing the ASCII character, but always the number (I mean is not showing e.g. 'A', but 65);

I was searching the web, etc., but no way. I have feeling that itoa is not converting to ASCII but just into the string.

Does anybody has any idea? Can someone push me to the right direction? Is there any other way how to do this?

Thanks in advance

lcd.print ('buffer');

fela: Hello,

I'm receiving from RS232 (via MAX232) datas; the word consist of 13 bytes and it is> =xxxx.xx(kg) + CR, where 'x' is number and '.' is decimal point.

I can succesfully read the datas via Serial monitor (Serial.write (incomingByte));

Also I can display it on LCD using LCD.write (incomingByte);

I want to pick up just the numbers from message. All incoming bytes are the type int. (Serial.write or LCD.write just make ASCII representation of that ints).

So my idea was to convert the int to ASCII, make a String and then cut the String. And then maybe to convert it to float.

So I used the itoa() in the way:

int incomingByte = 0;  
char buffer[12];
String ascii_num;

void loop() {

  if (Serial.available()) {     incomingByte = Serial.read();     itoa (incomingByte, buffer, 10);      // so if I understand it correctly - now in buffer should be a character (like '0' for 48)      // optionaly I can combine those buffers into string      // ascii_num += buffer;       lcd.print (buffer);         } // end if




The problem is, that lcd.print (buffer) (or Serial.print (buffer)) is not showing the ASCII character, but always the number (I mean is not showing e.g. 'A', but 65);

I was searching the web, etc., but no way. I have feeling that itoa is not converting to ASCII but just into the string.

Does anybody has any idea?
Can someone push me to the right direction?
Is there any other way how to do this? 

Thanks in advance

It sounds like it is already being sent as a string from your first sentence. Are you trying to read all 13 bytes with a a single Serial.read?

I'm reading those 13 bytes byte after byte, the last byte is CR.

for example, when receiving: =0012.30(kg) CR

I'll get 13 bytes: 61 48 48 49 50 46 51 48 40 107 103 41 13

which are ASCII values of the message.

But with Serial.read I can receive just int,... or?

Also I'll try (as Grumpy_Mike) advised lcd.print ('buffer');.

Had the same problem… after 40 minutes of trying every combination I could possibly tried, i finally got it to work…

     float T;
     char TStr[5];
     T = 3.147;
     dtostrf(T,5, 3, &TStr[0]);
     TStr[5]=0;   
     lcd.clear();  //i2c library LCD              
     lcd.setCursor(0,0);
     lcd.print("The Temperature:");  
     lcd.setCursor(1,1);
     lcd.print(TStr);
     lcd.println(" Degrees C");  
     delay(55);

i’ve had enough coding, remember to add #include <stdlib.h>

fela: I'm reading those 13 bytes byte after byte, the last byte is CR.

for example, when receiving: =0012.30(kg) CR

I'll get 13 bytes: 61 48 48 49 50 46 51 48 40 107 103 41 13

which are ASCII values of the message.

But with Serial.read I can receive just int,... or?

You're receiving '=', '0', '0', '1', '2', '.', '3', '0', '(', 'k', 'g', ')', '\r'

which isn't an int, and cannot be stored in an int. You need to read on character at a time and store it in a char array. Once you receive a '\r', you know the transmission is complete. At that point, if you just want to print it to the LCD, you just need to null-terminate the string and pass the buffer to the lcd.print().

    itoa (incomingByte, buffer, 10);
     // so if I understand it correctly - now in buffer should be a character (like '0' for 48)

You don't understand it correctly.

Serial.read() is returning a single byte. From your description, this byte contains an ascii character. For example if the incoming byte contained the number 48 then this is the ascii code for the character '0'. When you call itoa(), you are printing the number (48) which will produce the string "48" i.e. a string containing two characters: '4' and '8'.

For the problem you describe, it would be best to buffer the incoming characters in a char array until you have received the complete message, and then extract the numbers from the char array. You could use the CR character to detect the end of the message. You could use sscanf() to extract the numbers from the buffered string. Remember to null-terminate the buffered string before passing it to sscanf().

Alternate suggestion Do the Serial.read(). If it is a numeric character, store it in an array and switch to state "after" If it is NOT a numeric character ( 0-9 . ) if we are in state "before" ignore it if we are in state "after" the number is complete. Convert (atoi) and switch state to "before".

Here is a handy “readline” function. It will read from serial and store it in a user supplied buffer (char array) until it receives a character, at which point it returns the number of characters received. From there you can then do an ASCII to float conversion on the buffer.

int readline(int readch, char *buffer, int len)
{
  static int pos = 0;
  int rpos;
  
  if (readch > 0) {
    switch (readch) {
      case '\n': // Ignore new-lines
        break;
      case '\r': // Return on CR
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
      default:
        if (pos < len-1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
        }
    }
  }
  // No end of line has been found, so return -1.
  return -1;
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  static char buffer[80];
  if (readline(Serial.read(), buffer, 80) > 0) {
    Serial.print("You entered: >");
    Serial.print(buffer);
    Serial.println("<");
  }
}

Thank you all guys... I am going to try this...

EDIT: Yes, problem solved. Thanks guys.

Finally I stored the incoming byte into char array, and then I converted it to float.