Idil Rahman
Idil Rahman

Reputation: 1

Comparing a char pointer in an array with an integer

So I'm currently working on a project where it involves two lora devices (Arduino Mega + Dragino LoRa shield) exchanging data with each other.

How it works:

  1. Client will send the speed of motor reading to the server, it will also receive the distance reading from the server.

  2. Server will receive the speed of motor reading and also send its distance reading to the client.

I'm kinda new in C++ programming and here is the issue. The value i received from client is stored in char type buf. How can i compare it with an integer so that i can proceed with a threshold?

if ((char*)buf) < 10){ // (char*)buf contains the distance value sent by the server, I want to compare it with a certain value to trigger a buzzer
   sound = 1000;
   tone(buzzer,sound);
 }

I recieved an error message error: ISO C++ forbids comparison between pointer and integer

Here is the sending code:

char data[3];
itoa(reading, data, 10);
rf95.send((uint8_t*)data,sizeof(data));

Any idea on how i can solve this issue.

Upvotes: 0

Views: 166

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117218

Sender:

char data[3];                   // I suggest making this bigger because it is
itoa(reading, data, 10);        // now only safe if `reading` is in the range [-9, 99]
rf95.send((uint8_t*)data, strlen(data) + 1); // only send just enough

I suggest using sprintf(data, "%d", reading); instead of the non-standard itoa though.

If reading is a float as the comments suggest, you need to do this instead:

sprintf(data, "%.0f", reading); // there's no space for decimals in data

Receiver:

// read into buf until a \0 is encountered (and store that \0), then

int reading;

// if buf is an uint8_t[] as suggested in the comments, cast it:
if(sscanf(reinterpret_cast<const char*>(buf), "%d", &reading) == 1) {
    // an int was decoded
    if(reading < 10) {
        sound = 1000;
        tone(buzzer,sound);
    }
}

If you have installed Arduino STL you may have access to a lot more functions and classes that could help - but afaik, Arduino STL is very outdated so I used sscanf that I think is included in the base Arduino package.


Since you probably want to send different types over the radio and you'll have to cast between the char* and uint8* a lot, you could add two helper function templates to do that:

template<uint8_t N>    // returns true if sent ok
bool rf95send(const char(&buf)[N], uint8_t len) {
    if(N < len) return false;
    return rf95.send(reinterpret_cast<const uint8_t*>(buf), len);
}

template<uint8_t N>    // returns true if received ok
bool rf95receive(char(&buf)[N]) {
    uint8_t len = N;
    return rf95.recv(reinterpret_cast<uint8_t*>(buf), &len);
}

You can then add helper functions for the types you'd like to support:

bool rf95send_float(float f) {                // returns true if sent ok
    char buf[45];                             // room for all floats
    int len = sprintf(buf, "%.2f", f);
    return len > 0 && rf95send(buf, len + 1); // +1 for the null terminator
}

bool rf95receive_float(float& f) { // returns true if received ok
    char buf[45];
    if(rf95receive(buf)) {
        return sscanf(buf, "%f", &f) == 1;
    }
    return false;
}

Then in your sender code:

float reading;
if(rf95send_float(reading)) {    // sent ok

and the receiving side:

float reading;
if(rf95receive_float(reading)) { // received ok
    if(reading < 10) {
        sound = 1000;
        tone(buzzer,sound);
    } 
}

Upvotes: 0

Related Questions