ASCII confusion with conversion.

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Passive
 
Posts: 4
Joined: Mon Mar 04, 2013 4:08 pm

ASCII confusion with conversion.

Post by Passive »

Hello,

A quick over view of my project before I jump into the problem. I am measuring information from Arduino #1, this then sends the measured data to Arduino #2 via power line communication, due to my BANNED shield only working on Arduino 0022 and my wireless shield connection to the internet being programmed on Arduino 1.0.3 and the shields sharing pins, this information is then sent from Arduino #2 to Arduino #3 via serial communication ready to be sent to Google docs wirelessly.

So far most of my problems have come from the BANNED shields. However through a lot of dodgy coding and banging of my head against a wall, I have managed to get the information to be received at Arduino #3.

Now the problem. Each reading has to be sent 1 digit at a time by the power line communication, so it arrives at Arduino #3. I want to turn all the digits back into one number. I have tried using an array and a string to do this, but the information received at Arduino #3 is the decimal number for the ACSII char, and I am struggling to change it back to the correct number. I have tried atoi() but that gave a conflict when trying to put it into a string. I know that a bit of the codes I have used would be helpful, but the code got to such a mess that i have deleted it all and plan to start again.

So my question is how can I change a decimal number to the acsii character it represents, and then put all the single digits received into the one number they were to begin with?

Currently I am sending the int 1821 from Arduino #1, and getting 49 56 50 49 (one at a time) at Arduino #3.

User avatar
BeerCannon
 
Posts: 85
Joined: Fri Nov 16, 2012 9:16 pm

Re: ASCII confusion with conversion.

Post by BeerCannon »

This compiles, but I haven't tried uploading it to an Arduino to see if it works as expected. Purely an idea of how to do the conversion that you need.

Note uppercase "String" - uses string object instead of char array. The long() casting function doesn't work on a String object,
so I have you convert the string to a char array before casting it.

Code: Select all


String ascIn;         // holds each digit as it comes in (after ASCII code to char conversion)
char buf[5];          // buffer for string-to-long conversion, allocate 4 digits plus a null terminator
long numericData;   // holds the final value of the combined digits

void setup() {
}

void loop() {

// loop 4 times to get each digit of a 4-character input, presumably you have some other method in place.

  for (byte bLoop=0; bLoop< 4; bLoop++) {

  //  get input through whatever mechanism you're using.
  // I'll assume you have a function getDataFromArduino1 
  // that returns the ASCII character value of the digit
  // being sent.  I will also assume you're sending the 
  // digits most-signficant first.  

    ascIn+=char(getDataFromArduino1());

  }

  // I will lastly assume that your assembled numeric 
  // value will fit into a variable of type: Long.

  ascIn.toCharArray(buf,5);
  numericData= long(buf);
}

Disclaimer: I just looked up functions that I thought would work, there may be more elegant solutions. I haven't delved very deeply into the Arduino library yet because my projects have been simple thus far.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: ASCII confusion with conversion.

Post by tldr »

Passive wrote:Currently I am sending the int 1821 from Arduino #1, and getting 49 56 50 49 (one at a time) at Arduino #3.
that is 1821 in ascii. do you also get 0 after the second 49? you kind of need it as a string terminator. then you can pass the string to atoi which will now be able to tell where it ends.

Passive
 
Posts: 4
Joined: Mon Mar 04, 2013 4:08 pm

Re: ASCII confusion with conversion.

Post by Passive »

Thank you for replies, gonna try them out and see what happens.
that is 1821 in ascii. do you also get 0 after the second 49? you kind of need it as a string terminator. then you can pass the string to atoi which will now be able to tell where it ends.
I think the NULL terminator is sent. I am currently working at it from the TX end to see if I can get it to be sent in an easier fashion as I did have a quick try with atoi but kept getting conflict errors or just really random results.

Will try some more tonight and post more information on the problem when I get it :)

Passive
 
Posts: 4
Joined: Mon Mar 04, 2013 4:08 pm

Re: ASCII confusion with conversion.

Post by Passive »

Bit of an update:

Changed my TX code to output C24067Z. The C is to show start bit, Z is to show end bit.

Below is the current RX code:

Code: Select all

void
loop( void )
{
 char currentsend[8] = {'0','0','0','0','0','0','0'};
 char cstart;
 char cend;

//Serial.println("Start of loop");


      if(plm1_rx_data_available())
      {   
          delay(100);
          plm1_rx_get_data(rx_packet);
          delay(100);
          cstart = byte(rx_packet[0]);
          Serial.print(cstart);
          delay(100);
          for (int i = 0; i < 8; i++){
            plm1_rx_get_data(rx_packet);
            delay(100);
            currentsend[i] = byte(rx_packet[0]);
            Serial.print(currentsend[i]);
            delay(100);
            //Serial.print(i);
            }
          plm1_rx_get_data(rx_packet);
          delay(100);
          cend = byte(rx_packet[0]);
          Serial.println(cend);
          Serial.println("");
          delay(100); 
          
      }   
}
This is what I get out:

Image

Seems the for loop only catches 3 of the bits before ending and going back to the beginning of the void loop. There is also random unknown chars which I can't seem to get rid of, or find the source of.

As before any suggestions welcome.

EDIT:

Changing it to 28 iterations of the for loop worked to get them all "caught" at the same time.

Image

Now I just need to sort out all the junk :S

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: ASCII confusion with conversion.

Post by tldr »

i'm having a hard time trying to guess what "plm1_rx_get_data(rx_packet)" does. return a single ascii character into the first position in an array?

is it possible that your dropped characters are nulls returned by plm1... when it doesn't have data available?

looks like you check for data available once, then just start hoping that a tenth of a second will be a long enough wait for the next character.

are you always expecting 7 characters and a null terminator?

might be better to check for data available before every read.

Passive
 
Posts: 4
Joined: Mon Mar 04, 2013 4:08 pm

Re: ASCII confusion with conversion.

Post by Passive »

tldr wrote:i'm having a hard time trying to guess what "plm1_rx_get_data(rx_packet)" does. return a single ascii character into the first position in an array?

is it possible that your dropped characters are nulls returned by plm1... when it doesn't have data available?

looks like you check for data available once, then just start hoping that a tenth of a second will be a long enough wait for the next character.

are you always expecting 7 characters and a null terminator?

might be better to check for data available before every read.
plm1_rx_get_data, is pretty much power line communications version of Serial.read().

Yea I think the unknowns are NULL characters, it is also picking up the "spaces" as a char as well.

I am always expecting same number of characters to be sent, but the number wont always be all the characters (if that makes sense).

I'll give the the data available ago. But there is a NULL before the first character C, and that is were the data is first checked to be available, so I think it might count NULL's as available.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”