10 years, 7 months ago.

whats the meaning of 0x30, 0x31 in this code??

the code is to get the RSSI value from xbee .. its like we get the value of rssi in Hexadecimal...we convert it into decimal and multiply it with -1 to get the actual value but i m not able to get what is being done in the SWITCH CASE can anybody explain wat is the meaning of (0*30)*16 and 0*31 and also the second switch statement condition... or can anybody expain me both the switch statements

#include "XBeeGetRssi.h"
#include "mbed.h"
//#include <string.h>


XBeeGetRssi::XBeeGetRssi(PinName tx,PinName rx):Serial(tx,rx)
{
    _tx=tx;
    _rx=rx;
}

char* XBeeGetRssi::_getRssi(/*int n*/){
    //int judge_counter=0;
    int judge_counter=0;
    int rssi_counter=2;
    //int xbee_message_counter=0;
    
    Serial pc(USBTX,USBRX); //for debugging.
    Serial _MyXbee(_tx,_rx);
    
    _rssi[0]='0';
    _rssi[1]='0';
    
    for(int i=2;i<20;i++){
        _rssi[i]='0'; //initial value
    }
    
    _MyXbee.putc('T');
    
    wait(1.0);
    _MyXbee.putc('+');
    wait_ms(1);
    _MyXbee.putc('+');
    wait_ms(1);
    _MyXbee.putc('+');
    wait_ms(1);

    //judge_counter=0; //reset counter for later use

    wait(1.0);
    
    _MyXbee.putc('A');
    wait_ms(5);
    _MyXbee.putc('T');
    wait_ms(10);
    _MyXbee.putc('D');
    wait_ms(10);
    _MyXbee.putc('B');
    wait_ms(10);
    _MyXbee.putc('\r');
    wait_ms(10);
    
    while(_rssi[judge_counter++]!='\0'){
        while(_MyXbee.readable()){
            _rssi[rssi_counter++]=_MyXbee.getc();
        }
    }
    if(_rssi[5]=='T'){
        _rssi_address=&_rssi[9];
    }else{
        _rssi_address=&_rssi[0];
    }
    
    _MyXbee.putc('A');
    wait_ms(5);
    _MyXbee.putc('T');
    wait_ms(5);
    _MyXbee.putc('C');
    wait_ms(5);
    _MyXbee.putc('N');
    wait_ms(5);
    _MyXbee.putc('\r');
    
    return _rssi_address;
    
}

int XBeeGetRssi::getRssi(/*int n*/){
    int _rssi_value=0;
    _returned_rssi=_getRssi();
    
    switch(*_returned_rssi){
        case '0':
            _rssi_value+=0;
            break;
        case '1':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '2':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '3':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '4':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '5':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '6':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '7':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '8':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case '9':
            _rssi_value+=(int)(*_returned_rssi-0x30)*16;
            break;
        case 'A':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        case 'B':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        case 'C':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        case 'D':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        case 'E':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        case 'F':
            _rssi_value+=(int)(*_returned_rssi-0x31)*16;
            break;
        default:
            _rssi_value+=0;
            break;
    }
    switch(*(_returned_rssi+1)){
        case '0':
            _rssi_value+=0;
            break;
        case '1':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '2':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '3':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '4':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '5':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '6':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '7':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '8':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case '9':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x30);
            break;
        case 'A':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        case 'B':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        case 'C':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        case 'D':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        case 'E':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        case 'F':
            _rssi_value+=(int)(*(_returned_rssi+1)-0x31);
            break;
        default:
            _rssi_value+=0;
            break;
    }

#ifndef XBEEGETRSSI20130718
#define XBEEGETRSSI20130718

#include "mbed.h"

class XBeeGetRssi{
    public:
        XBeeGetRssi(PinName tx,PinName rx); //select the oneshot mode or repeat mode.
        int getRssi(/*int n*/); //method for getting rssi.
        
    private:
        PinName _tx;
        PinName _rx;
        char _rssi[256]; //arrangement for containing the rssi value.
        char* _rssi_address; //value before caluculate.
        char* _getRssi();
        char* _returned_rssi; //return value after calculate
        //char _return_rssi_value[10];
        //int _rssi_value;
};

#endif

and also what is the meaning of these line "rssi_value+=(int)(*_returned_rssi-0x30)*16;" and _rssi_value+=(int)(*(_returned_rssi+1)-0x30);

if 16 is multiplied to convert back to decimal why isnt it mention in the second case??

posted by ritika krishna 17 Apr 2014

what changes should i make to make this code work....can u please suggest...its kinda a urgent

posted by ritika krishna 17 Apr 2014

Just done some maths,

you need to subtract 0x37, NOT 0x31 for A to F.

Ceri

posted by ceri clatworthy 17 Apr 2014

1 Answer

10 years, 7 months ago.

This is a conversion from ASCII to number (I think)

BUT ...

'0' = 0x30
'1' = 0x31
..
'9' = 0x39

'A' = 0x41
..
'F' = 0x46

therfor the code snippit should be .... - 0x41 * 16

have a look at my post in RFID http://mbed.org/cookbook/ID12-RFID-Reader

which also converts ASCII to a NUMBER.

Ceri

u mean to say that the code is wrong (SWITCH case)?? i mean for CASE '1" i hv to write 0*31 for CASE '2' i have to write 0x31 and so om for both the SWITCH CASE??

posted by ritika krishna 17 Apr 2014

The 'String' from your device is in HEXADECIMAL, (base 16, as TEXT)

So you will need to UN-Convert it,

I am 98% sure your code will not function correctly for the HEX part of the HEXADECIMAL number.

which ranges from 0 to 255 (0x00 .. 0xFF)

posted by ceri clatworthy 17 Apr 2014

what changes should i make to make this code work....can u please suggest...its kinda a urgent?? also what is the meaning of these line "rssi_value+=(int)(*_returned_rssi-0x30)*16;" and _rssi_value+=(int)(*(_returned_rssi+1)-0x30);

if 16 is multiplied to convert back to decimal why isnt it mention in the second case??

posted by ritika krishna 17 Apr 2014

OK,

for example: input = 0x46 = 70 in decimal.

the first CASE statment converts the '4' (which is TEXT, not a number)

'4' = 0x34 = 52

so ..

0x34 - 0x30 = 0x04, ....0x04 * 16 = 64

and the second block , deals with '6' (text)

'6' = 0x36 = 54, (decimal)

'6' = 0x36, - 0x30 = 0x06

SO: 64 + 6 = 70 (decimal)

furtremore:

Input of text 'C0' (192 Dec) .... or [0x43] [0x30] as text

Just done some MATHS,

you need to subtract 0x37 NOT 0x31 for 'A' to 'F'

you need to multiply by 16, because it is effectively the 'TENS' and the second is effectively the 'UNITS'

posted by ceri clatworthy 17 Apr 2014